Here is another small script from the "Quick & Dirty" series. The script can restart a server and sends a mail with the status:
#Server restart
$servername = "SERVER01"
$to = "frank@frankysweb.de"
$from = "rebooter@frankysweb.de"
$smtpserver = "smtp.frankysweb.local"Send-MailMessage -To $to -From $from -SmtpServer $smtpserver -Subject "Restart for server $servername" -body "The server $servername is now being restarted"
Restart-Computer -ComputerName $servername -Forcedo {$ping = Test-Connection $servername -Count 1 -Quiet}
while ($ping -match "True")Send-MailMessage -To $to -From $from -SmtpServer $smtpserver -Subject "Server $servername is offline" -body "The server $servername is in the restart phase"
do {$ping = Test-Connection $servername -Count 1 -Quiet}
while ($ping -match "False")sleep 30
$servicelist = get-wmiobject win32_service -computername $servername | where {$_.Startmode -match "Auto" -and $_.State -match "Stopped"}
foreach ($service in $servicelist)
{
$servicename = $service.name
$displayname = get-service $servicename -computername $servername
$displayname = $displayname.displayname
$displayname = [string]$displayname + ", "
$downservice += $displayname
}Send-MailMessage -To $to -From $from -SmtpServer $smtpserver -Subject "Server $servername is online" -body "The server $servername is restarted The following services are not running $downservice"
The script can be started from another server or computer via task scheduling. It sends a mail when the server is restarted, a mail when it is offline and a mail when it is available again. The last mail also shows the services that have "Automatic" set as the start type but have not been started.
Hab’s etwas verbessert. Das hier wartet bei Windows Updates, bis der Server wirklich wieder oben ist und eine WMI-Abfrage anspricht.
do {
sleep 30
$servicelist = get-wmiobject win32_service -computername $servername | where {$_.Startmode -match „Auto“ -and $_.State -match „Stopped“}
} while ($servicelist -match „False“)