<# .Synopsis original script http://poshtips.com/2013/02/12/check-rdp-availability-using-watch-rebootstatus-script/ Test and Monitor machine availability via Network Ping and Port Query RDP2.ps1 contains changes by Alan Kaplan 3/1/13 3/2/13 Changed the MsgBox to SystemModal .Description Original Description Check Windows Machine Network accessibility by testing: (1) Machine responds to PING (2) Machine shows specified port(s) are open default is to only test standard RDP Port(3389) Intended purpose is to monitor machine status following a reboot Kaplan modifications: Original Script will terminate when PING and RDP Port tests are both successful This will ping a remote system then wait for RDP port availablity After port is live, it launches MS Remote Desktop Connection to specified computer. .Notes Original Author: xb90@poshtips.com date : 01-FEB-2013 .Parameter HostName HostName for which data is needed .Parameter Port This defaults to the standard RDP port number (3389) Multiple ports can be specified; e.g. -Port 3389,22,1096 .Parameter WaitForReboot Assumes that host is currently UP and will be rebooted This option will cause script to wait for host to go offline (no ping response) before proceeding with testing for online status #> param ( [string]$ComputerName,[switch]$WaitForReboot) function ChkPort{ # param ([string]$ComputerName,[int]$Port) -- these are already set $sock = new-object System.Net.Sockets.Socket ` -ArgumentList $([System.Net.Sockets.AddressFamily]::InterNetwork), ` $([System.Net.Sockets.SocketType]::Stream), ` $([System.Net.Sockets.ProtocolType]::Tcp) try { $sock.Connect($ComputerName,$Port) $sock.Connected $sock.Close() } catch { ChkPort = $false } } cls # this is the default RDP port [int]$Port=3389 [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') [string]$ScriptName =$MyInvocation.MyCommand.Name if(-not($ComputerName)) { [string]$strCommand = $env:computername + ':' + $Port $message = "Commandline Syntax:`n$ScriptName Computername[:port] -WaitForReboot`n` Use the optional -WaitForReboot switch if the system has not yet rebooted.`n`n` Enter the name of the computer[:optional port]" $ComputerName = [Microsoft.VisualBasic.Interaction]::InputBox("$message", "Computer Name", "$strCommand") if(-not($ComputerName)) { Write-Host "Quitting" Exit } $retval = [Microsoft.VisualBasic.Interaction]::MsgBox("If the system is up do you want to` wait for a reboot?",'YesNoCancel,defaultbutton2,systemmodal,Question', "Wait for Reboot") if ($retval -eq 'Yes') {$WaitForReboot = $TRUE } if ($retval -eq "Cancel") { Write-Host "Quitting" Exit } } #lame, but I am just learning $hasport= $ComputerName.contains(":") if ($hasport) { $iSplit = $ComputerName.indexof(":") [int]$Port= $ComputerName.SubString($iSplit+1) $ComputerName = $ComputerName.SubString(0,$iSplit) } $pingOk = $false $portOk = $false $ComputerName = $ComputerName.toUpper() write-host ("Script : {0,-60}" -f $ScriptName) -back cyan -for black Write-Host ("Started at : {0,-60}" -f $(get-date)) -back cyan -for black Write-Host ("Computer : {0,-60}" -f $ComputerName) -back cyan -for black write-host ("RDP Port : {0,-60}" -f $Port) -back cyan -for black write-host ("WaitForReboot : {0,-60}" -f $WaitForReboot) -back cyan -for black Write-Host "" if ($WaitForReboot){ write-host "Waiting for $ComputerName to reboot... " -nonewline do { $pingOk = Test-Connection $ComputerName -quiet if ($pingOk) { write-host "." -nonewline -fore red } } until (!$pingOk) Write-Host "`n$ComputerName is down." } write-host "`nPinging $ComputerName ... " -nonewline do{ $pingOk = Test-Connection $ComputerName -erroraction silentlyContinue if ($pingOk){ write-host "`n$ComputerName replied." } else{ write-host "." -nonewline -fore red #too slow to add sleep #Start-Sleep -milliseconds 500 } } until ($pingOk) write-host "`nWaiting for port $Port ... " -nonewline do{ $portOk = ChkPort if ($portOk){ Write-Host "`nPort $Port has opened" } else{ #Start-Sleep -milliseconds 500 Write-Host "." -nonewline -fore red } }until ($portOk) Write-Host "`n$Computername is ready for RDP connection`n`a`a" # Kaplan mods Write-Host "Launching RDP to $Computername on port $Port" #build command string [string]$strCommand = $ComputerName + ':' + $Port mstsc.exe /v:$strCommand