Skip to content

Instantly share code, notes, and snippets.

@ctolkien
Created March 13, 2014 03:37
Show Gist options
  • Save ctolkien/9521545 to your computer and use it in GitHub Desktop.
Save ctolkien/9521545 to your computer and use it in GitHub Desktop.
Tests if machines respond on port 80
$machines = get-content "machinescsv.txt" #assumes machine names are each on a new line
$fileToLogTo = "logOfIIS.txt" #this is where we'll log machines that respond
set-content $fileToLogTo $null #reset the contents of the file to nothing
$machines | ForEach-Object { #for each machine that we have
$socket = (new-object Net.Sockets.TcpClient) #reach into .net and make a TcpClient
try {
$socket.Connect($_, 80) #attempt to connect
Add-Content $fileToLogTo ($_ + "`n") #write to log file
Write-Host "Open Port on" $_ -BackgroundColor Green #post to console
}
catch [System.Exception] {
Write-Host "Failed:" $_ -BackgroundColor Red # no iis found!
}
finally {
$socket.Dispose() #cleanup, close socket, etc.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment