Port Scanner using PowerShell with Email Notification

Few days back I was asked to create a script, which will check RDP port status of multiple servers and send the result as an email notification. I have searched for few, but unfortunately there was no ready script for that. I decided to build one using PowerShell, and came out with following script.

Hope this script might help you as well.

powershellPortScanner
#Which port to scan
$port = 3389
#Path of the input server list
$hostns=Get-Content D:Servers.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 = "[email protected]"
    To = "[email protected]"
    Subject = "Server Status Check"
    SMTPServer = "YOUR SMTP SERVER"
    Priority = "High"
    Body = $body
    }
Send-MailMessage @NotiEmail

 

Leave a Reply

Your email address will not be published. Required fields are marked *