12 November 2013

Check OWA page with login and report when fails

After the millionth time the helpdesk come complaining that OWA wasn't working, I made this script that checks the status of the OWA webpage by logging in and reporting back if the login attempt fails.

The script:

A few things are necessary;
login.txt:      the user that logs in to OWA needs to have a mailbox and the password file needs to be generated every time the password is renewed.


# Check if Outlook Webapp is online
#
# Use the 3 lines below to generate an encrypted password file for the user that will be logging into the site for this script
# $MyPswd = "password"
# ConvertTo-SecureString $MyPswd -AsPlainText -Force | `
# ConvertFrom-SecureString | Out-File -FilePath "D:\Scripts\Login.txt"


$username = "domain\username"  # user needs a mailbox
$password = Get-Content "D:\Scripts\Login.txt" | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$logfile = "D:\Scripts\WebPageLoginResult.txt"

if (Test-Path($logfile))
 {
  remove-item $logfile
 }

Test-OWAConnectivity -url https://webmail.domain.com/owa -MailboxCredential:(Get-credential $cred) | fl url,scenario,result | out-file $logfile
Test-OWAConnectivity -url https://webmail.domain.lan/owa -trustanysslcertificate -MailboxCredential:(Get-credential $cred) | fl url,scenario,result | out-file -append $logfile
  
$Body = Get-Content $logfile

Get-Content $logfile | where {$_ -match "skipped"} | foreach {Send-MailMessage -SmtpServer smtp.domain.lan -From OWA_Check@domain.com -To username@domain.com,username2@domain.com -Subject "Webmail is not working" -Body ( $Body | out-string )}
Get-Content $logfile | where {$_ -match "failed"} | foreach {Send-MailMessage -SmtpServer smtp.domain.lan -From OWA_Check@domain.com -To username@domain.com,username2@domain.com -Subject "Webmail is not working" -Body ( $Body | out-string )}

No comments:

Post a Comment