Thursday, January 23, 2014


Every month, we install the latest Windows OS patches to all of our servers. After patching, this requires that we reboot each server. I use this basic little script to monitor the server reboot. It's sort of like running a PING -t <servername> but it will tell you when the RDP service is back up and running again.

For Example:
.\Ping-alive.ps1 Server1
DOWN:..TS UP....TS UP........8:00PM
SVC:................................Server1 8:08PM

   Report when can RDP into server after Reboot.
   Pings server until available, then does a WMI call until Remote Desktop service is running..
   Servername to check. 
   .\Ping-Alive.ps1 -identity SERVER1

 [parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true,HelpMessage="Servername to watch")]
$TSDown = $false

write-host "DOWN:" -NoNewline
do {
 $test = Test-Connection -ComputerName $identity -Quiet 
 if (!$TSDown) {
  $TSDown = (gwmi win32_service -ComputerName $identity -filter "state = 'running' and name = 'TermService' " -erroraction silentlycontinue) -eq $null
  if (!$TSDown) { Write-Host "..TS UP.." -NoNewline } 
 Write-Host "." -NoNewline
} while ($test)
Write-Host (get-date).toshorttimestring()

write-host "PING:" -NoNewline
do {
 $test = Test-Connection -ComputerName $identity -Quiet 
 Write-Host "." -NoNewline
} while (!$test)
if ($test) {Write-Host (get-date).toshorttimestring()}

Write-Host "SVC:" -NoNewline

Do { 
 $testname = (gwmi win32_service -ErrorAction SilentlyContinue -ComputerName $identity -filter "state = 'running' and name = 'TermService' ") -ne $null
 Write-Host "." -NoNewline 
 Start-Sleep -Milliseconds 500
} while (!$testname)

if ($testname) {Write-Host $identity,(get-date).toshorttimestring()}

No comments:

Post a Comment