Created
December 15, 2021 16:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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