Shayne D. Swann
2008-12-08 13:16:36 UTC
Im looking for a way to search a sam account name attribute in all of my
domains trusted domains. I.E. when I search a name I want to search to be
performed on all of my trusted domains (even outside of my forest). I have
had some luck with the following script however you still have to specify the
netbios domain name:
Dim varArgument, arrParameters, adoCon, strConnection, adoRS
varArgument = WScript.Arguments(0)
If varArgument <> "" Then
arrParameters = Split(varArgument, "\")
Set adoCon = CreateObject("ADODB.Connection")
With adoCon
.Provider = "ADsDSOObject"
.Open "Active Directory Provider"
End With
strConnection = "SELECT mail, displayName FROM 'LDAP://" &
arrParameters(0) & "' WHERE objectClass='user' AND objectCategory='Person'
AND samAccountName ='" & arrParameters(1) & "'"
Set adoRS = adoCon.Execute(strConnection)
If Not adoRS.EOF Then
Wscript.Echo varArgument & " is " & adoRS.Fields("displayName")
& vbCrLf & "Email address is " & adoRS.Fields("mail")
Else
Wscript.Echo varArgument & " was not found in AD."
End If
End If
Set adoRS = Nothing
Set adoCon = Nothing
domains trusted domains. I.E. when I search a name I want to search to be
performed on all of my trusted domains (even outside of my forest). I have
had some luck with the following script however you still have to specify the
netbios domain name:
Dim varArgument, arrParameters, adoCon, strConnection, adoRS
varArgument = WScript.Arguments(0)
If varArgument <> "" Then
arrParameters = Split(varArgument, "\")
Set adoCon = CreateObject("ADODB.Connection")
With adoCon
.Provider = "ADsDSOObject"
.Open "Active Directory Provider"
End With
strConnection = "SELECT mail, displayName FROM 'LDAP://" &
arrParameters(0) & "' WHERE objectClass='user' AND objectCategory='Person'
AND samAccountName ='" & arrParameters(1) & "'"
Set adoRS = adoCon.Execute(strConnection)
If Not adoRS.EOF Then
Wscript.Echo varArgument & " is " & adoRS.Fields("displayName")
& vbCrLf & "Email address is " & adoRS.Fields("mail")
Else
Wscript.Echo varArgument & " was not found in AD."
End If
End If
Set adoRS = Nothing
Set adoCon = Nothing