Need a script to find OU information for a user

A

Andy Svendsen

Does anyone know of a script or the location of a script that will return
what OUs a user is in at logon time? I can find a million scripts that look
up user information for users in a given OU, but nothing that will find what
OUs a user is in.

--
*******************************************************************
Andy S.
(e-mail address removed)

Please remove NOMORESPAM before replying.

*******************************************************************
 
M

Mike Brannigan [MSFT]

Andy Svendsen said:
Does anyone know of a script or the location of a script that will return
what OUs a user is in at logon time? I can find a million scripts that look
up user information for users in a given OU, but nothing that will find what
OUs a user is in.

Both the distinguishedname and canonicalname attributes of a user object
list the user with all the OUs they are nested in.


--

Regards,

Mike
--
Mike Brannigan [Microsoft]

This posting is provided "AS IS" with no warranties, and confers no
rights

Please note I cannot respond to e-mailed questions, please use these
newsgroups
 
A

Andy Svendsen

Is there a place with a script example?
--
*******************************************************************
Andy S.
(e-mail address removed)

Please remove NOMORESPAM before replying.


*******************************************************************
 
A

Alex

This might be useful for you.

Dim oRootDSE
Dim oConnection
Dim oRecordSet
Dim oCommand
Dim strQuery
Dim strDomainNC
Dim strName
Dim strOU
Dim strDN

' Find the domain naming context
set oRootDSE = GetObject("LDAP://RootDSE")
strDomainNC = oRootDSE.Get("defaultNamingContext")
set oRootDSE = Nothing

' Setup the ADO connection
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "ADs Provider"

Set oCommand = CreateObject("ADODB.Command")

strQuery = "<LDAP://" & strDomainNC & ">;
(&(objectCategory=Person)(objectClass=user));
distinguishedName,objectClass,name;subtree"
oCommand.ActiveConnection = oConnection
oCommand.CommandText = strQuery
oCommand.Properties("Page Size") = 99

' Execute the query
Set oRecordset = oCommand.Execute

If oRecordSet.Eof then
WScript.Echo "No objects were found"
WScript.Quit(0)
Else
On Error Resume Next
While Not oRecordset.Eof
strName = oRecordset.Fields("name").Value
strDN = oRecordset.Fields("distinguishedName").Value
strOU = Mid(strDN, Len(strName)+5)
WScript.Echo strName & " is in " & strOU & " OU."
oRecordset.MoveNext
Wend
End If

'Clean up
Set oRecordset = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
 
A

Andy Svendsen

Thanks, that is a good start for me. I'm going to try and adapt it to a
logon script for just one user, instead of a using a recordset to get
everything. I will post back if I can make a solution work.

--
*******************************************************************
Andy S.
(e-mail address removed)

Please remove NOMORESPAM before replying.

Always keep your antivirus and Microsoft software
up to date with the latest definitions and product updates.
Be suspicious of every email attachment, I will never send
or post anything other than the text of a http:// link nor
post the link directly to a file for downloading.

This posting is provided "as is" with no warranties
and confers no rights.

*******************************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top