Friday, March 14, 2014

Monitoring Exchange Functionality (OWA/ActiveSync) from Internet

Following is the way to test Exchange Health from Internet.  You will need the pc with Exchange Management Tools installed.

  • 2 batch file to be run on the Task Scheduler every desired number of minutes - one batch (running Powershell Script) will be testing the Exchange Functionality; another (running vbs script) will be parsing the output file and send email to the phone of on-call person if the word "Failure" appears in the script.
  • Note - credentials are supplied for the script in the secure way
  • Powershell Script:
#Set-ExecutionPolicy
Set-ExecutionPolicy -ExecutionPolicy:Unrestricted

# specify acct used on Exchange 2010
$username = "TestAccount@yourdomain.com"
$password = cat C:\admin\securestring.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

Test-OwaConnectivity -URL https://yourURL.yourdomain.com/owa -MailboxCredential $cred | Out-File c:\admin\emailtestresult.txt -Append -Encoding utf8

Test-ActiveSyncConnectivity -URL https://YourURL.yourdomain.com/Microsoft-Server-ActiveSync -MailboxCredential $cred -LightMode | Out-File c:\admin\emailtestresult.txt -Append -Encoding utf8

  • Note - there is a file  C:\admin\securestring.txt used as the encrypted password file; to generate it run the following:
read-host -assecurestring | convertfrom-securestring | out-file C:\admin\securestring.txt

and type the password of the account used.

  • VBS Script:

Dim objFS
Dim objResult
Dim numEvent
Dim numAlert
numEvent = 0
numAlert = 0
Set objFS = CreateObject("Scripting.FileSystemObject")
strFileIn = "c:\admin\emailtestresult.txt"
'Set inFile = objFS.OpenTextFile(strFileIn, ForReading)

Set inFile = objFS.OpenTextFile(strFileIn, 1, TristateTrue)

Do Until inFile.AtEndOfStream
                strLine = lTrim(inFile.ReadLine)
  
'if the word 'Failure' found in the result file - numAlert == 1

                If InStr(strLine,"Failure") > 0 Then
                                numAlert = 1
                End If
Loop

                If numAlert > 0 Then
                                Set objShell = WScript.CreateObject("WScript.Shell")    
'send alert using blat
                                objResult = objShell.Run("blat " & strFileIn & " -subject ""Your friendly alert message is here"" -to yourphone@phonedomain.com")

                                Set objShell = Nothing
               
                End If

inFile.Close

  • Create .bat file to run on the Task Scheduler
  • For Powershell:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; "PathToPSFile\PSFileName.ps1"
  • For VBS Script:
cscript PathtoVBSFile\VBSFileName.vbs

No comments:

Post a Comment