Share via


Monitoring Web URL using PowerShell

Summary

There are many monitoring tools available in the market to trigger email alerts. Unfortunately the module we use don't support WEB URL. The site is IIS 7.5 with NTLM V2.

Considerations

Why don't you enable IIS logging and use log parsing tools till you get a fix? Suggesting to enable IIS logs and monitor your event viewer this way it will be more precise.

Solution

We discussed this in our PowerShell session and decided to consume .NET class to achieve this. Just to ensure we have work around in place which helps small environment. Why should we consume .NET classes why not to use Invoke-webRequest. Well and Good but it wont work in PS 2.0. So we made two different work around using PowerShell.

PowerShell 2.0 Code

 $mailparameter = @{ 
FROM = 'mailid'; 
TO = 'mailid'; 
SUBJECT = 'Your Website is not accessible...'; 
ATTACHMENT = "C:\Temp\Error.txt"; 
SMTP = 'SMTP FQDN'; 
}  
try{ 
$request = [System.Net.WebRequest]::Create("https://test.com") 
$request.UseDefaultCredentials = $true 
$response = $request.GetResponse() 
}Catch [System.Net.WebException] {$_.Exception | Out-File C:\Temp\Error.txt ; Send-MailMessage @mailparameter} 
finally{ 
$response.Close() 

PowerShell 3.0 Code

Invoke-WebRequest -Uri 'http://test.com' -UseDefaultCredentials | Select StatusCode , StatusDescription | fl

Help

help Invoke-WebRequest -Detailed

help Invoke-WebRequest -Examples