I have a lot of websites to monitor their up/down status, possible errors, ping and the other things that I managed to get with a script. My idea is the following: This scri
Since you're using Powershell v3, you should be using Send-MailMessage
instead of dealing with System.Net
.
$URLListFile = "C:\URLList.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = @()
Foreach($Uri in $URLList) {
$time = try{
$request = $null
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliSeconds
}
catch
{
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] @{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
if($result -ne $null)
{
$Outputreport = "Website Report Status Website Report Status
URL Code Status Duration MS (Ping) "
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += ""
}
else
{
$Outputreport += " "
}
$Outputreport += "$($Entry.uri) $($Entry.StatusCode) $($Entry.StatusDescription) $($Entry.ResponseLength) $($Entry.timetaken) "
}
$Outputreport += "
"
}
$Outputreport | out-file C:\URLReport.htm
Invoke-Item C:\URLReport.htm
$EmailFrom = "noreply@domain.com"
$EmailTo = "destinyemail@domain.com"
$EmailSubject = "URL Report"
$emailbody = " body message "
$SMTPServer = "smtpserver.company.com"
$emailattachment = "C:\URLReport.htm"
Send-MailMessage -Port 587 -SmtpServer $SMTPServer -From $EmailFrom -To $EmailTo -Attachments $emailattachment -Subject $EmailSubject -Body $emailbody -Bodyashtml;