'Script to get total number of MBX in exchange server, uses wmi
'Modified to be used by Nagios
'Author: Felipe Ferreira
'Version: 1.0

'Check: Does it go thru all existing Storage Groups and DBs??

    On Error Resume Next
    Dim ComputerName 	            ' Dynamicly gets local computer name
    Dim iCount : iCount = 0         ' Do the count of total mailboxes
    Const cWMINameSpace = "root/MicrosoftExchangeV2"
    Const cWMIInstance = "Exchange_Mailbox"
    Dim strWinMgmts            ' Connection string for WMI
    Dim objWMIExchange   ' Exchange Namespace WMI object
    Dim listExchange_Mailboxs  ' ExchangeLogons collection
    Dim objExchange_Mailbox           ' A single ExchangeLogon WMI object

    call getHost()	
    strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//"& _
    ComputerName&"/"&cWMINameSpace
    Set objWMIExchange =  GetObject(strWinMgmts)
    ' Verify we were able to correctly set the object.
    If Err.Number <> 0 Then
      WScript.Echo "ERROR: Unable to connect to the WMI namespace."
      wscript.quit(1)
    Else
      Set listExchange_Mailboxs = objWMIExchange.InstancesOf(cWMIInstance)
      ' Were any Exchange_Mailbox Instances returned?
      If (listExchange_Mailboxs.count > 0) Then
        For Each objExchange_Mailbox in listExchange_Mailboxs
		icount = icount + 1
        Next
      Else
        WScript.Echo "WARNING: No Exchange_Mailbox instances were returned."
        wscript.quit(1)
      End If
    End If
wscript.echo "Total Mailboxes: " & iCount
wscript.quit(0)

function getHost()
Dim objShell
Set objShell = CreateObject("WScript.Shell")   ' Run cmds
Set objExecObject = objShell.Exec("cmd /c hostname")
Do While Not objExecObject.StdOut.AtEndOfStream
    	    ComputerName = objExecObject.StdOut.ReadLine()  
			trim(ComputerName)
loop
end function
