检查用户是否拥有 Exchange 邮箱





4.00/5 (2投票s)
2003年9月24日

157233
在命令文件中检查用户是否拥有 Exchange 邮箱
引言
这个 VBScript 用于检查用户是否拥有 Exchange 邮箱。结果存储在 Errorlevel
变量中。该脚本可以在命令行文件中调用。例如,您可以仅在用户拥有邮箱的情况下运行 Outlook。
解决方案
解决方案是检查 Active Directory 中是否存在该用户的 Exchange 信息。
如您所见,用户名 (logonname
) 是可选的。如果您不提供用户名,则将从环境变量 USERNAME
中获取名称。
Errorlevel
即可。REM *** Run Outlook when user has an exchange account ***
HasMailBox.vbs
If not errorlevel==1 outlook.exe
更改域控制器信息,将 MyMainDC
替换为您要查询的域控制器。(LDAP)
DCServer = "MyMainDC"
'***********************************************
'
' HasMailbox.vbs
' (c) 2003 Computech
' Written by Peter Verijke
' Checks is user has a mailbox in Exchange
'
' Usage : HasMailbox [LogonName]
' Returns : Errorlevel==1 : not found
'
'***********************************************
Dim ArgObj
Dim WshShell ' as object
Dim objEnv ' as collection
Dim objUser 'As IADsUser
Dim objMailbox 'As CDOEXM.IMailboxStore
Dim sUserLDAPName 'As String
Dim DCServer 'As String
' Get the Arguments object
Set ArgObj = WScript.Arguments
If ArgObj.Count < 1 Then
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEnv = WshShell.Environment("PROCESS")
sUserName = objEnv("USERNAME")
else
sUserName = UCase(ArgObj(0))
End If
DCServer = "MyMainDC"
sUserLDAPName = QueryActiveDirectory(sUserName)
Set objUser = GetObject("LDAP://" & DCServer + "/" & sUserLDAPName)
Set objMailbox = objUser
'check if user is mailbox enabled
If objMailbox.HomeMDB = "" Then
WScript.Quit 1
Else
WScript.Quit 0
End If
Public Function QueryActiveDirectory(sUserName)
'Function: QueryActiveDirectory
'Purpose: Search the Active Directory's Global Catalog for users
'Parameters: UserName - user to search for
'Return: The user's distinguished name
Dim oAD 'As IADs
Dim oGlobalCatalog 'As IADs
Dim oRecordSet 'As Recordset
Dim oConnection 'As New Connection
Dim strADsPath 'As String
Dim strQuery 'As String
Dim strUPN 'As String
set oRecordSet = CreateObject("ADODB.Recordset")
set oConnection = CreateObject("ADODB.Connection")
'Determine the global catalog path
Set oAD = GetObject("GC:")
For Each oGlobalCatalog In oAD
strADsPath = oGlobalCatalog.AdsPath
Next
'Initialize the ADO object
oConnection.Provider = "ADsDSOObject"
'The ADSI OLE-DB provider
oConnection.Open "ADs Provider"
'Create the search string
strQuery = "<" & strADsPath & _
">;(&(objectClass=user)(objectCategory=person)(samaccountName=" & _
sUserName & "));userPrincipalName,cn,distinguishedName;subtree"
'Execute the query
Set oRecordSet = oConnection.Execute(strQuery)
If oRecordSet.EOF And oRecordSet.BOF Then
'An empty recordset was returned
QueryActiveDirectory = "Not Found"
Else 'Records were found; loop through them
While Not oRecordSet.EOF
QueryActiveDirectory = oRecordSet.Fields("distinguishedName")
oRecordSet.MoveNext
Wend
End If
End Function