Thursday, January 23, 2014

Ping-alive.ps1

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
PING:......8:02PM
SVC:................................Server1 8:08PM




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

[CmdLetBinding()]
param(
 [parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true,HelpMessage="Servername to watch")]
 [string]$Identity
)
$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