User Tools

Site Tools


exchange:reporting:wmi_vbscript

VB Script to return Exchange Mailbox data via WMI

'the main work was taken from Microsoft's sample code


On Error Resume Next

Dim aArgs
Dim strUsage
Dim strArgs
Dim server_arg

strUsage = "Usage: cscript.exe " & wscript.ScriptName & " ExchangeServerName [ServerName2 ServerName3...]"

Set aArgs = wscript.Arguments

If aArgs.count = 0 Then
wscript.echo strUsage
wscript.quit(1)
End If

'If aArgs.count > 0 Then
'	If aArgs.Count > 1 Then
'	 wscript.echo strUsage
'	 wscript.Quit(1)
'	End If

strArg = aArgs(0)

 'some error handling
 On Error Resume Next

'End If

	For Each server_arg in aArgs
	
	WMIGetMailboxList(server_arg)

	Next


Function WMIGetMailboxList(server_name)
Dim cComputerName
Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_Mailbox"
'cComputerName = "cuit-x1"
'cComputerName = aArgs(0)
cComputerName = server_arg

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

' Create the object string, indicating WMI (winmgmts), using the
' current user credentials (impersonationLevel=impersonate),
' on the computer specified in the constant cComputerName, and
' using the CIM namespace for the Exchange provider.
strWinMgmts = ""
strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//"&cComputerName&"/"&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: " & strWinMgmts & "error number: " & err.Number
Else
  '
  ' The Resources that currently exist appear as a list of
  ' Exchange_Mailbox instances in the Exchange namespace.
  Set listExchange_Mailboxes = objWMIExchange.InstancesOf(cWMIInstance)
  '
  ' Were any Exchange_Mailbox Instances returned?
  If (listExchange_Mailboxes.count > 0) Then
    ' If yes, do the following:
    ' Iterate through the list of Exchange_Mailbox objects.

  WScript.echo "MailboxDisplayName,Size,LastLogonTime,ServerName,StorageGroupName,StoreName,TotalItems,DeletedMessageSizeExtended "

    For Each objExchange_Mailbox in listExchange_Mailboxes

'Two things of note:
'objExchange_Mailbox.LegacyDN
'objExchange_Mailbox.MailboxDisplayName

	'Return the mailbox fields we want...
	WScript.echo """" & objExchange_Mailbox.LegacyDN & """,""" & objExchange_Mailbox.Size & """,""" & _
	objExchange_Mailbox.LastLogonTime & """,""" & objExchange_Mailbox.ServerName & """,""" & _
	objExchange_Mailbox.StorageGroupName & """,""" & _
	objExchange_Mailbox.StoreName & """,""" & objExchange_Mailbox.TotalItems & """,""" & _
	objExchange_Mailbox.DeletedMessageSizeExtended & """"


    Next
  Else
    ' If no Exchange_Mailbox instances were returned,
    ' display that.
    WScript.Echo "WARNING: No Exchange_Mailbox instances were returned."
  End If
End If

End Function
exchange/reporting/wmi_vbscript.txt · Last modified: 2010/03/06 04:33 by ben