' ********************************************************************* ' ' Name : Verify Mailbox Last Access.vbs ' Author : 3Ds (UK) Limited ' Description : VBScript to check for Exchgange mailboxes that have not ' been accessed for a given time. ' Script Type : Monitoring. ' Arguments : 1 - Name of your Exchange server ' 2 - No. of days of non-use before mailbox should be reported. ' Returns : 0 - Success, 1 - failed to run, 2 - one or more mailboxes found. ' Mailbox details are listed in the output. ' Notes : Requires Windows Scripting Host (for VBScript). ' ' WIZARD:PARAMS=1 = Name or address of Exchange Server|2 = no. days of non-use to check for ' WIZARD:PARAMEXAMPLE=YourExchangeServer 30 ' WIZARD:RESULTS=Return code ...||0 = OK|1 = An error prevented the script from running|2 = One or more mailboxes were found & output by the script.||Script output also indicates "OK" for success, "Warning" or "Error" with details of the faults found. ' WIZARD:REMOTE ' ' ********************************************************************* ' Enable in-line error handling On Error Resume Next ' ------ Local declarations ------ Const cWMIPath = "root/MicrosoftExchangeV2" Const cWMIPathInstance = "Exchange_Mailbox" Dim bFound ' True if found Dim strServer ' Our server name Dim intMaxDaysOld ' No. days to check for Dim strDays ' Conversion value Dim strConvertedDate ' Conversted date value Dim strWMIConnection ' Connection string for WMI Dim cWMIPathExchange ' Exchange Namespace WMI object Dim colMailboxes ' Exchange mailbox collection Dim objMailbox ' A single mailbox Dim objArgs ' Inbound command-line arguments ' ------ Main Logic ------ Set objArgs = WScript.Arguments ' Retrieve command line arguments If objArgs.Count <> 2 Then ' Insufficient arguments WScript.Echo "Error. Either one or more arguments are missing or too many specified" WScript.Quit(1) End If ' Set up values strServer = objArgs.Item(0) ' Your Exchange server's name intMaxDaysOld = Cint(objArgs.Item(1)) ' No. days If intMaxDaysOld = 0 Then ' Invalid parameter WScript.Echo "Error. The number of days to check must be numeric & greater than 0" WScript.Quit(1) End If ' Set up the connection to WMI strWMIConnection = "winmgmts:{impersonationLevel=impersonate}!//" & strServer & "/" & cWMIPath Set cWMIPathExchange = GetObject(strWMIConnection) If Err.Number <> 0 Then ' Invalid parameter WScript.Echo "Error. Unable to access WMI. " & Err.Description Set objArgs = Nothing WScript.Quit(1) End If ' Retrieve mailbox details Set colMailboxes = cWMIPathExchange.InstancesOf(cWMIPathInstance) bFound = False If (colMailboxes.count > 0) Then ' We have one or more mailboxes so loop through checking them For Each objMailbox in colMailboxes ' TODO: Add any mailboxes you want to ignore here If objMailbox.MailboxDisplayName = "Mailbox to ignore" Then ' Ignore it Else ' Check the last accessed date - retrieve up to the days strDays = Left(objMailbox.LastLogonTime, 8) ' Find how many days since now strConvertedDate = Mid (strDays, 5, 2) & "/" & Right (strDays,2) & "/" & Left (strDays, 4) intDays = DateDiff("d", strConvertedDate, Now) ' Check it against the inbound value If (CInt(intDays) > CInt (intMaxDaysOld)) Then ' Output details bFound = True WScript.echo "Warning. Mailbox for " & objMailbox.MailboxDisplayName & " has not been accessed for: " & intDays & " days. Size: " & objMailbox.Size & "Kb, (" & objMailbox.TotalItems & " items)" End If End If Next End If If bFound = True Then ' One or more mailboxes reported Set colMailboxes = Nothing Set objMailbox = Nothing Set objArgs= Nothing WScript.Quit(2) End If ' ------ Cleanup ------ ' No problems Set colMailboxes = Nothing Set objMailbox = Nothing Set objArgs= Nothing Wscript.Echo "OK. No mailboxes found where last access date exceeds given value" WScript.Quit(0) ' ------ End of Script ------