Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
If you want to check a port on a remote computer every few seconds until it comes back online this will work. A sound will play in a loop on a windows box once the machine comes back online as well. CTRL-C to stop it.
$computerName = "some.computer.name"
$portNumber = 3389
while ((Test-NetConnection -ComputerName $computerName -Port $portNumber).TcpTestSucceeded -EQ $false){
Write-Host "Checking TCP port $portNumber on $computerName..."
Start-Sleep -Seconds 5
Get-Date -Format "yyyy-MM-ddTHH:mm:ss (K)"
if ((Test-NetConnection -ComputerName $computerName -Port $portNumber).TcpTestSucceeded -EQ $true){
Write-Host -ForegroundColor Magenta "Yay!! TCP port $portNumber on $computerName is back online!"
while($true){(New-Object System.Media.SoundPlayer $((Get-ChildItem -Path "$env:windir\Media\tada.wav").FullName)).Play(); Start-Sleep -Seconds 2}
}
}
# Feel free to turn it into a function for extra credit. Or not! It works as is.
Write-Host -ForegroundColor Magenta "TCP port $portNumber on $computerName is already online"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment