Powershell Script that asks to Rerun or close
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.
June 18th, 2015 1:08pm

Here's one way:

Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 1:26pm

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
  }
 }
}

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

June 20th, 2015 4:29pm

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
  }
 }
}

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

Free Windows Admin Tool Kit Click here and download it now
June 20th, 2015 8:29pm

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.

June 21st, 2015 9:21am

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'.
Free Windows Admin Tool Kit Click here and download it now
June 21st, 2015 10:38am

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

June 21st, 2015 1:08pm

Oh I see now, thank you sir.
Free Windows Admin Tool Kit Click here and download it now
June 21st, 2015 5:36pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics