Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Restart servers with check of services?

$
0
0

The Restart-Computer cmdlet has some of this functionality built-in, as of PowerShell 3.0. See http://technet.microsoft.com/en-us/library/hh849837.aspx for details. You could do something like this:

$serviceToCheck = 'bits'

foreach ($server in $servers)
{
    Restart-Computer -ComputerName $server -Wait -For Wmi

    while ((Get-WmiObject -ComputerName $server -Class Win32_Service -Filter "Name = '$serviceToCheck'").State -ne 'Running')
    {
        Start-Sleep -Seconds 5
    }
}

This sample code is not very robust, though. It will wait forever for the computers to restart, or for the service to be running; there’s no code to time out or handle other errors.


Viewing all articles
Browse latest Browse all 13067

Trending Articles