Skip to content

Instantly share code, notes, and snippets.

@nanoDBA
Created December 15, 2021 16:40
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 nanoDBA/b897e71bd2bdcf3209e60cb2dd8fc988 to your computer and use it in GitHub Desktop.
Save nanoDBA/b897e71bd2bdcf3209e60cb2dd8fc988 to your computer and use it in GitHub Desktop.
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