PowerShell - Check Port from List of Servers with email results
- Jon Boyette
- Jan 7, 2022
- 1 min read
#Which port to scan
$port = 445
#Path of the input server list
$hostns=Get-Content c:\temp\yourtextfile.txt
$body = @()
foreach ($hostn in $hostns)
{
$PortCon = new-object System.Net.Sockets.TcpClient($hostn, $port)
if ($PortCon.Connected -eq 'True')
{
$body +="$hostn Server listening to port $port"
$PortCon.Close()
}
else
{
$body +="$hostn Server not responding to $port"
}
}
$body = $body | Out-String
$NotiEmail = @{
From = "add email address"
To = "add email address"
Subject = "Server Status Check"
SMTPServer = "Add SMTP server"
Priority = "High"
Body = $body
}
Send-MailMessage @NotiEmail
Comments