我设法编写了我想要的 VBScript,所以我将在这里分享。该脚本可能需要很好的清理,但现在它完成了这项工作,所以我希望它也可以帮助其他人。
' Get OU
strOU1 = "OU=here,DC=mydomain,DC=com"
strOU2 = "OU=there,DC=yourdomain,DC=com"
Dim samid
Dim ldap_command
' Create connection to AD
'
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
' Create command
'
Set objCommand1 = CreateObject("ADODB.Command")
objCommand1.ActiveConnection = objConnection
objCommand1.Properties("Page Size") = 1000
' Execute command to get all users in OU
'
objCommand1.CommandText = _
"<LDAP://" & strOU1 & ">;" & _
"(&(objectclass=user)(objectcategory=person));" & _
"adspath,distinguishedname,sAMAccountName;subtree"
Set objRecordSet = objCommand1.Execute
' Show info for each user in OU
'
Do Until objRecordSet.EOF
' Show required info for a user
'
samid = objRecordSet.Fields("sAMAccountName").Value
WScript.Echo "Processing " & objRecordSet.Fields("sAMAccountName").Value
Set objCommand2 = CreateObject("ADODB.Command")
ldap_command = _
"<LDAP://" & strOU2 & ">;" & _
"(&(objectclass=user)(objectcategory=person)" & _
"(sAMAccountName=" & samid & "));" & _
"adspath,distinguishedname,sAMAccountName;subtree"
objCommand2.CommandText = ldap_command
objCommand2.ActiveConnection = objConnection
objCommand2.Properties("Chase referrals") = &H40
Set objRecordSet2 = objCommand2.Execute
If objRecordSet2.RecordCount = 0 Then
Wscript.Echo "The sAMAccountName is not in use."
Else
Wscript.Echo "This ID is in use"
End If
' Move to the next user
'
objRecordSet.MoveNext
Loop