Hi again, I'm having trouble sending an email when it detects a timeout or destination host unreachable.
The script I have will get the content of a text file with all the IP address of my servers, it will then loop and ping each and everyone of them to see if it's "online". If it is online then it will just write-host "Server are all Online", If not then it should send an email out with the Domain Name. (instead of IP)
I think i need a timer to report back and i think i need a way to get the domain name to report back. I was considering a function but im unsure, any help would be greatful
$ips = Get-Content "\Path\serverlist.txt"
foreach ($ip in $ips) {
$online = Test-Connection -computername $ip
if (-not($online)) {
$hostn = [System.Net.Dns]::GetHostEntry($ip.ipaddress).HostName
New-Object -TypeName PSObject -Property @{'Host'=$hostn;'IP'=$ip.ipaddress}
}
}
#--------Send Email---------
$smtpServer = "192.168.X.X"
$ReportSender = "user@email.com"
$users = "user@email.com"
$MailSubject = "Server Offline"
foreach($user in $users){
if(!$online){
Write-Host "Sending Email notification to $user"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From = $ReportSender
$msg.Subject = $MailSubject
$msg.IsBodyHTML = $True
$msg.Body = $server
$smtp.Send($msg)
}
else{
Write-Host "Servers are all Online"
}
}