0

I have a powershell script I created to query services that are running on servers in my environment. As it stands now, the script will work, returning running services then stop. I am trying to figure out a way to have the script go back to the beginning and prompt for the next server name so I can repeatedly execute it

$Server = Read-Host -Prompt 'Enter Server Name'
Get-Service -ComputerName $Server | Where Status -eq "Running"
Out-GridView
Read-host "press enter to continue.."
1
  • while ($true) { Read-Host "You'll need Ctrl-C to stop this." } Commented Aug 24, 2020 at 14:10

2 Answers 2

2

Don't forget to pipe the objects returned to Out-GridView.. ;)

I'd probably do something like this:

while ($true) { 
    $Server = Read-Host -Prompt 'Enter Server Name'
    if([string]::IsNullOrWhiteSpace($Server)) { break }
    Get-Service -ComputerName $Server | 
    Where Status -eq "Running" |
    Out-GridView
}
Sign up to request clarification or add additional context in comments.

Comments

0

This will run until you just press enter instead of providing a servername.

$Server = Read-Host -Prompt 'Enter Server Name'

while ($Server -ne "")  
{
    Get-Service -ComputerName $Server | Where Status -eq "Running" | Out-GridView
    $Server = Read-Host -Prompt 'Enter Server Name'
}

2 Comments

@ITninja777 I updated my code with a prettier solution
Thanks Daniel, the while statement in the loop made all the difference, it works great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.