locked
Simple script to retreivePC information RRS feed

  • Question

  • Pardon my ignorance, I am very new to Powershell scripting.

    I am trying to run a Powershell.exe script (Not Powershell ISE) to simply retrieve user information for a specific PC. I would like

    to input the PC name on the keyboard and then have the script display the user information.

    My script looks like this:

    $Machine = read-host "What is the system name or IP address?"

    Get-WmiObject -Class Win32_ComputerSystem -computername $Machine | Select-Object -Property caption, username

    However, when I run it, it never stops to allow input. I can run it 1 statement at a time and it works OK, but when I try

    to run it as a script, it does nothing.

    What am I doing wrong?

    Tuesday, July 22, 2014 2:17 PM

Answers

  • To create a script, you have to put the lines of code in a text file with a .ps1 file extension.

    Then you type the script's name at the PowerShell prompt to run the script.

    In addition, PowerShell's execution policy must be configured to allow scripts to run.

    See the following article for more details:

    Windows IT Pro: Running PowerShell Scripts Is as Easy as 1-2-3


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Tuesday, July 22, 2014 3:31 PM
    • Marked as answer by Bill_Stewart Saturday, August 16, 2014 3:32 PM
    Tuesday, July 22, 2014 2:55 PM

All replies

  • To create a script, you have to put the lines of code in a text file with a .ps1 file extension.

    Then you type the script's name at the PowerShell prompt to run the script.

    In addition, PowerShell's execution policy must be configured to allow scripts to run.

    See the following article for more details:

    Windows IT Pro: Running PowerShell Scripts Is as Easy as 1-2-3


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Tuesday, July 22, 2014 3:31 PM
    • Marked as answer by Bill_Stewart Saturday, August 16, 2014 3:32 PM
    Tuesday, July 22, 2014 2:55 PM
  • Try this.  It helps when pasting into a console but it will not be needed when running from a file.

    function Run-Code{
         $host.ui.Rawui.FlushInputBuffer()
         $Machine = read-host "What is the system name or IP address?" 
         Get-WmiObject -Class Win32_ComputerSystem -computername $Machine | Select-Object -Property caption, username
    }
    Run-Code
    
    


    ¯\_(ツ)_/¯

    Tuesday, July 22, 2014 2:58 PM
  • Open PowerShell

    Set-ExecutionPolicy RemoteSigned -ErrorAction SilentlyContinue

    Open Notepad and copy paste your code

    $Machine = read-host "What is the system name or IP address?"
    
    Get-WmiObject -Class Win32_ComputerSystem -computername $Machine | Select-Object -Property caption, username
    Save file as .PS1 extension. Example PCInfo.PS1


    Go to the File Location. save the file in TEMP 'C:\Temp\PCInfo.PS1'

    In PowerShell C:\Temp>

    Now type .\PCInfo.PS1

    let us know if you have any issues. Happy Scripting !!!


    Regards Chen V [MCTS SharePoint 2010]


    • Edited by Chen VMVP Tuesday, July 22, 2014 3:46 PM
    Tuesday, July 22, 2014 3:26 PM
  • You do not need to run as administrator to do any of this. Why?


    ¯\_(ツ)_/¯

    Tuesday, July 22, 2014 3:31 PM
  • You do not need to run as administrator to do any of this. Why?


    ¯\_(ツ)_/¯

    Indeed I copy pasted my PS document. This is not required as you said. I changed it.

    I made in my document because at times we get RPC server unavailable error. So I made that statement


    Regards Chen V [MCTS SharePoint 2010]


    • Edited by Chen VMVP Tuesday, July 22, 2014 3:51 PM
    Tuesday, July 22, 2014 3:48 PM
  • You do not need to run as administrator to do any of this. Why?


    ¯\_(ツ)_/¯


    Indeed I copy pasted my PS document. This is not required as you said. I changed it.

    Regards Chen V [MCTS SharePoint 2010]

    Yeah - I do that all of the time.


    ¯\_(ツ)_/¯

    Tuesday, July 22, 2014 3:52 PM