Sub GetMailboxAccessInfo()
Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_Logon"
cComputerName = "inex" ' Exchange Server NETBIOS Name
Dim strWinMgmts ' Connection string for WMI
Dim objWMIExchange ' Exchange Namespace WMI object
Dim listExchange_Logons ' ExchangeLogons collection
Dim objExchange_Logon ' 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 = "winmgmts:{impersonationLevel=impersonate}!//" & cComputerName & "/" & cWMINameSpace
On Error Resume Next
Set objWMIExchange = GetObject(strWinMgmts)
' Verify we were able to correctly set the object.
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
MsgBox "ERROR: Unable to connect to the WMI namespace."
Else
' The Resources that currently exist appear as a list of
' Exchange_Logon instances in the Exchange namespace.
Set listExchange_Logons = objWMIExchange.InstancesOf(cWMIInstance)
' Were any Exchange_Logon Instances returned?
If (listExchange_Logons.Count > 0) Then
' Set CSV Headers
'strResults = """Client IP"",""Client Name"",""Logged On User"",""Mailbox Display Name"""
Cells(1, "A").Value = "Client IP"
Cells(1, "B").Value = "Client Name"
Cells(1, "C").Value = "Logged On User"
Cells(1, "D").Value = "Mailbox Display Name"
' If yes, do the following:
' Iterate through the list of Exchange_Logon objects.
For Each objExchange_Logon In listExchange_Logons
' Display some values of each Exchange_Logon object.
'strResults = strResults & VbCrLf & """" & objExchange_Logon.ClientIP & """,""" & _
' """" & objExchange_Logon.ClientName & """,""" & _
' """" & objExchange_Logon.LoggedOnUserAccount & """,""" & _
' """" & objExchange_Logon.MailboxDisplayName & """"
Cells(Cells(65536, "A").End(xlUp).Row + 1, "A").Value = objExchange_Logon.ClientIP
Cells(Cells(65536, "B").End(xlUp).Row + 1, "B").Value = objExchange_Logon.ClientName
Cells(Cells(65536, "C").End(xlUp).Row + 1, "C").Value = objExchange_Logon.LoggedOnUserAccount
Cells(Cells(65536, "D").End(xlUp).Row + 1, "D").Value = objExchange_Logon.MailboxDisplayName
Next
Else
' If no Exchange_Logon instances were returned, ' display that.
MsgBox "WARNING: No Exchange_Logon instances were returned."
End If
End If
MsgBox "Done."
End Sub
|