Skip to content

Instantly share code, notes, and snippets.

@tostka
Last active March 28, 2018 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tostka/09ea8fbc73067337c50c80db5f8f5256 to your computer and use it in GitHub Desktop.
Save tostka/09ea8fbc73067337c50c80db5f8f5256 to your computer and use it in GitHub Desktop.
<# This gist is TRIMMED to just the core pieces #>
#*------v Function Test-Port() v------
function Test-Port {
# attempt to open a port (telnet xxx.yyy.zzz nnn)
# call: Test-Port $server $port
PARAM([parameter(Mandatory=$true)] [alias("s")] [string]$Server, [parameter(Mandatory=$true)][alias("p")]
[int]$port
) ;
$ErrorActionPreference = “SilentlyContinue” ;
$socket = new-object Net.Sockets.TcpClient ;
$socket.Connect($Server, $port) ;
if ($socket.Connected) {
write-verbose -verbose:$true "Successful connection to $($Server):$($port)"
$socket.Close()
return $True;
} else {
write-verbose -verbose:$true "Failed to connect to $($Server):$($port)"
return $False;
} # if-block end
$socket = $null
}#*------^ END Function Test-Port() ^------
#*======v SUB MAIN v======
$tsrvr=read-host -prompt "Server to be pinged until RDP available" ;
Do {write-host "." -NoNewLine;Start-Sleep -m (1000 * 5)} Until ((test-port $tsrvr 3389)) ;
write-host "`a" ; # beep
write-verbose -verbose:$true "$((get-date).ToString('HH:mm:ss')):Launching rdp to $($tsrvr)..." ;
mstsc.exe /v:$($tsrvr) ;
#*======^ END SUB MAIN ^======
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment