none
Powershell Script that asks to Rerun or close RRS feed

  • Question

  • I have a Powershell script that I found on the net and have customized to my own liking that spits out select WMI information on a remote host. Originally, you had to edit the script to include the machines you wanted information on but I have modified it to ask for the hostname of the machine you want information on. What I can't figure out, which is probably pretty straightforward is, at the end of the script I'd like for it to ask "To close press Enter, to lookup another host, press Y" and then obviously, if they hit Enter, it closes the PS window or goes back to the prompt. Pressing Y causes the script to execute from Line 1 and start all over again.
    Thursday, June 18, 2015 5:04 PM

Answers

  • Take out the foreach loop and just repeat for one computer at a time.


    $choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&Y","&N")
    while ( $true ) {
      $computer = Read-Host "Input a host name"
      $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
      $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
      $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
      $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
      write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
      "-------------------------------------------------------"
      "Manufacturer: " + $computerSystem.Manufacturer
      "Model: " + $computerSystem.Model
      "Serial Number: " + $computerBIOS.SerialNumber
      "CPU: " + $computerCPU.Name
      "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
      "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
      "User logged In: " + $computerSystem.UserName
      "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
      ""
      "-------------------------------------------------------"
      $choice = $Host.UI.PromptForChoice("Repeat the script?","",$choices,0)
      if ( $choice -ne 0 ) {
        break
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by USMCGrunt0307 Sunday, June 21, 2015 9:35 PM
    Sunday, June 21, 2015 5:06 PM
    Moderator

All replies

  • Here's one way:


    $choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&Y","&N")
    while ( $true ) {
      "Run your script here"
      $choice = $Host.UI.PromptForChoice("Repeat the script?","",$choices,0)
      if ( $choice -ne 0 ) {
        break
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by USMCGrunt0307 Saturday, June 20, 2015 8:28 PM
    • Unmarked as answer by USMCGrunt0307 Saturday, June 20, 2015 8:28 PM
    Thursday, June 18, 2015 5:22 PM
    Moderator
  • So that worked except it doesn't matter if I hit Y or N, it repeats the script. I replaced "Run yours script here" with my script and that's it.

    In fact, here's the script I'm inserting:

    -----------------------------------------------------------------------------------------------

    $choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&Y","&N")
    while ( $true ){
      $userinput = Read-Host "Input Hostname"

    $ArrComputers =  $userinput

    #Specify the list of PC names in the line above. "." means local system

    foreach ($Computer in $ArrComputers)
    {
        $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
        $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
        $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
        $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
            write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
            "-------------------------------------------------------"
            "Manufacturer: " + $computerSystem.Manufacturer
            "Model: " + $computerSystem.Model
            "Serial Number: " + $computerBIOS.SerialNumber
            "CPU: " + $computerCPU.Name
            "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
            "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
            "User logged In: " + $computerSystem.UserName
            "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
            ""
            "-------------------------------------------------------"
      $choice = $Host.UI.PromptForChoice("Repeat the script?","",$choices,0)
      if ( $choice -ne 0 ) {
        break
      }
     }
    }

    --------------------------------------------------------------------------------------------------------

    Saturday, June 20, 2015 8:29 PM
  • That is because the break statement terminates your foreach loop, not the while loop.

    You don't need a foreach loop in your script, unless you also want to create code that splits the input into an array.


    -- Bill Stewart [Bill_Stewart]

    Sunday, June 21, 2015 1:19 PM
    Moderator
  • I'm sorry, I'm not that familiar with scripting, if I take either function, the break or foreach, it breaks the script and I can't seem to figure out where these functions should be in the script. My understanding is that the foreach statement is needed to run the information in the variable through a process at each line and that the break is needed to terminate the script loop if the user selects 'N'.
    Sunday, June 21, 2015 2:36 PM
  • Take out the foreach loop and just repeat for one computer at a time.


    $choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&Y","&N")
    while ( $true ) {
      $computer = Read-Host "Input a host name"
      $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
      $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
      $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
      $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
      write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
      "-------------------------------------------------------"
      "Manufacturer: " + $computerSystem.Manufacturer
      "Model: " + $computerSystem.Model
      "Serial Number: " + $computerBIOS.SerialNumber
      "CPU: " + $computerCPU.Name
      "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
      "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
      "User logged In: " + $computerSystem.UserName
      "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
      ""
      "-------------------------------------------------------"
      $choice = $Host.UI.PromptForChoice("Repeat the script?","",$choices,0)
      if ( $choice -ne 0 ) {
        break
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by USMCGrunt0307 Sunday, June 21, 2015 9:35 PM
    Sunday, June 21, 2015 5:06 PM
    Moderator
  • Oh I see now, thank you sir.
    Sunday, June 21, 2015 9:35 PM