This is a multi-part message in MIME format.

------=_NextPart_000_0015_01C3C9FA.7E933750
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Does anyone know how I can include the users parent container from the =
following script? The "ou" portion (in BLACK) is where I was trying to =
gather that, but it doesn't work.

strDNSDomain =3D objRootDSE.Get("defaultNamingContext")
strFilter =3D "(&(objectCategory=3Dperson)(objectClass=3Duser))"
strQuery =3D "<LDAP://" & strDNSDomain & ">;" & strFilter _
& ";distinguishedName,cn,ou,userPrincipalName,sAMAccountName;subtree"



The script below returns exactly what I'm looking for however, I can't =
figure out how to incorporate in into the query. I'm trying to populate
a spreadsheet with the information that is returned from the "sUserOU" =
variable.

' Connect to the LDAP server's root object
Set oADsRootDSE =3D GetObject("LDAP://RootDSE")

Set oADsSysInfo =3D CreateObject("ADSystemInfo")

' Get distinguished name of the current user
sADsUser =3D oADsSysInfo.UserName
WScript.Echo "Distinguished name: " & sADsUser

' Get user object
sUserPath =3D "LDAP://" & sADsUser
Set oADsContainer =3D GetObject(sUserPath)

sUserOU =3D oADsContainer.parent
WScript.Echo "Users OU: " & sUserOU

' Get full name of the current user
sUserNameFull =3D oADsContainer.cn
WScript.Echo "Full name: " & sUserNameFull

Here is the format that the results are returned.


Distinguished name: =
CN=3DPRH007,OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet
Users OU: LDAP://OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet
Full name: PRH007

Thanks,

Roger


------=_NextPart_000_0015_01C3C9FA.7E933750
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1264" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>Does anyone know how I can include the =
users parent=20
container from the following script?&nbsp; The "ou" portion (in BLACK) =
is where=20
I was trying to gather that, but it doesn't work.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2><STRONG>strDNSDomain =
=3D=20
objRootDSE.Get("defaultNamingContext")<BR>strFilter =3D=20
"(&amp;(objectCategory=3Dperson)(objectClass=3Duser))"<BR>strQuery =3D =
"&lt;LDAP://"=20
&amp; strDNSDomain &amp; "&gt;;" &amp; strFilter _<BR>&nbsp; &amp;=20
";distinguishedName,cn,<FONT=20
color=3D#000000>ou</FONT>,userPrincipalName,sAMAccountName;subtree"</STRO=
NG></FONT></DIV>
<DIV><STRONG><FONT face=3DArial color=3D#ff0000 =
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial color=3D#000000 =
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>The script below returns =
exactly what I'm=20
looking for however, I can't figure out how to incorporate in into the=20
query.&nbsp; I'm trying to populate</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>a spreadsheet with the =
information that is=20
returned from the "sUserOU" variable.</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Connect to the LDAP =
server's root=20
object<BR>Set oADsRootDSE =3D GetObject("</FONT><A =
href=3D"ldap://RootDSE"><FONT=20
face=3DArial color=3D#ff0000 size=3D2>LDAP://RootDSE</FONT></A><FONT =
face=3DArial=20
color=3D#ff0000 size=3D2>")</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>Set oADsSysInfo =3D=20
CreateObject("ADSystemInfo")</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Get distinguished =
name of the=20
current user<BR>sADsUser =3D oADsSysInfo.UserName<BR>WScript.Echo =
"Distinguished=20
name: " &amp; sADsUser</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Get user =
object<BR>sUserPath =3D=20
"LDAP://" &amp; sADsUser<BR>Set oADsContainer =3D=20
GetObject(sUserPath)</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>sUserOU =3D=20
oADsContainer.parent<BR>WScript.Echo "Users OU: " &amp;=20
<STRONG>sUserOU</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Get full name of the =
current=20
user<BR>sUserNameFull =3D oADsContainer.cn<BR>WScript.Echo "Full name: " =
&amp;=20
sUserNameFull</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Here is the format that the =
results are=20
returned.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><STRONG>Distinguished =
name:=20
CN=3DPRH007,OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet<BR>Users OU: =

</STRONG></FONT><A href=3D"ldap://t"><STRONG><A=20
href=3D"ldap://OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet"><FONT =
face=3DArial=20
size=3D2>LDAP://</FONT></STRONG></A><STRONG><FONT face=3DArial=20
size=3D2>OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet</FONT></A><BR><F=
ONT face=3DArial=20
color=3D#0000ff size=3D2>Full name: PRH007</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial color=3D#0000ff =
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>Thanks,</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>Roger</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial color=3D#0000ff=20
size=3D2></FONT></STRONG>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_0015_01C3C9FA.7E933750--

Re: Get Users Parent container from query by Richard

Richard
Wed Dec 24 10:49:50 CST 2003

This is a multi-part message in MIME format.

------=_NextPart_000_0042_01C3CA0B.A88BEFA0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

Unfortunately, only Organizational Units have a mandatory "ou" =
attribute. And, "Parent" is a property method of the user object, not an =
attribute, so it cannot be retrieved directly with an ADO query. =
Instead, you will have to parse the distinguishedName (DN) attribute, or =
bind to the user object and use the "Parent" property method. It would =
be faster to parse the DN. For example, this function would parse a DN =
and return the Distinguished Name of the parent container/ou.

Function ParseOU(strDN)
ParseOU =3D Mid(strDN, InStr(strDN, "=3D") + 1)
ParseOU =3D Mid(ParseOU, InStr(ParseOU, "=3D") - 2)
End Function

If you are using ADO to retrieve information on all users in your =
domain, you can use this function in the loop where you enumerate the =
records returned by the query.

Actually, user objects have an "ou" attribute, but it is never has a =
value.

--=20
Richard
Microsoft MVP Scripting and ADSI
HilltopLab web site - http://www.rlmueller.net
--
"Roger" <hainesr3@nationwide.com> wrote in message =
news:OVudKziyDHA.540@tk2msftngp13.phx.gbl...
Does anyone know how I can include the users parent container from the =
following script? The "ou" portion (in BLACK) is where I was trying to =
gather that, but it doesn't work.

strDNSDomain =3D objRootDSE.Get("defaultNamingContext")
strFilter =3D "(&(objectCategory=3Dperson)(objectClass=3Duser))"
strQuery =3D "<LDAP://" & strDNSDomain & ">;" & strFilter _
& =
";distinguishedName,cn,ou,userPrincipalName,sAMAccountName;subtree"



The script below returns exactly what I'm looking for however, I can't =
figure out how to incorporate in into the query. I'm trying to populate
a spreadsheet with the information that is returned from the "sUserOU" =
variable.

' Connect to the LDAP server's root object
Set oADsRootDSE =3D GetObject("LDAP://RootDSE")

Set oADsSysInfo =3D CreateObject("ADSystemInfo")

' Get distinguished name of the current user
sADsUser =3D oADsSysInfo.UserName
WScript.Echo "Distinguished name: " & sADsUser

' Get user object
sUserPath =3D "LDAP://" & sADsUser
Set oADsContainer =3D GetObject(sUserPath)

sUserOU =3D oADsContainer.parent
WScript.Echo "Users OU: " & sUserOU

' Get full name of the current user
sUserNameFull =3D oADsContainer.cn
WScript.Echo "Full name: " & sUserNameFull

Here is the format that the results are returned.


Distinguished name: =
CN=3DPRH007,OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet
Users OU: LDAP://OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet
Full name: PRH007

Thanks,

Roger


------=_NextPart_000_0042_01C3CA0B.A88BEFA0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1276" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>Hi,</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Unfortunately, only Organizational Units have a =
mandatory "ou"=20
attribute. And, "Parent" is a property method of the user object, not an =

attribute, so it cannot be retrieved directly with an ADO query. =
Instead, you=20
will have to parse the distinguishedName (DN)&nbsp;attribute, or bind to =
the=20
user object and use the "Parent" property method. It would be faster to =
parse=20
the DN. For example, this function would parse a DN and return the =
Distinguished=20
Name of the parent container/ou.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Function ParseOU(strDN)</FONT></DIV>
<DIV><FONT size=3D2>&nbsp; ParseOU =3D Mid(strDN, InStr(strDN, "=3D") +=20
1)</FONT></DIV>
<DIV><FONT size=3D2>&nbsp; ParseOU =3D Mid(ParseOU, InStr(ParseOU, =
"=3D") -=20
2)</FONT></DIV>
<DIV><FONT size=3D2>End Function</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>If you are using ADO to retrieve information on all =
users in=20
your domain, you can use this function in the loop where you enumerate =
the=20
records returned by the query.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>Actually, user objects have an "ou" attribute, but =
it is never=20
has a value.</FONT></DIV>
<DIV><FONT size=3D2><BR>-- <BR>Richard<BR>Microsoft MVP Scripting and=20
ADSI<BR>HilltopLab web site - <A=20
href=3D"http://www.rlmueller.net">http://www.rlmueller.net</A><BR>--</FON=
T></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Roger" &lt;<A=20
=
href=3D"mailto:hainesr3@nationwide.com">hainesr3@nationwide.com</A>&gt; =
wrote in=20
message <A=20
=
href=3D"news:OVudKziyDHA.540@tk2msftngp13.phx.gbl">news:OVudKziyDHA.540@t=
k2msftngp13.phx.gbl</A>...</DIV>
<DIV><FONT face=3DArial size=3D2>Does anyone know how I can include =
the users=20
parent container from the following script?&nbsp; The "ou" portion (in =
BLACK)=20
is where I was trying to gather that, but it doesn't =
work.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2><STRONG>strDNSDomain =
=3D=20
objRootDSE.Get("defaultNamingContext")<BR>strFilter =3D=20
"(&amp;(objectCategory=3Dperson)(objectClass=3Duser))"<BR>strQuery =3D =
"&lt;LDAP://"=20
&amp; strDNSDomain &amp; "&gt;;" &amp; strFilter _<BR>&nbsp; &amp;=20
";distinguishedName,cn,<FONT=20
=
color=3D#000000>ou</FONT>,userPrincipalName,sAMAccountName;subtree"</STRO=
NG></FONT></DIV>
<DIV><STRONG><FONT face=3DArial color=3D#ff0000=20
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial color=3D#000000=20
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>The script below returns =
exactly what I'm=20
looking for however, I can't figure out how to incorporate in into the =

query.&nbsp; I'm trying to populate</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>a spreadsheet with the =
information that=20
is returned from the "sUserOU" variable.</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Connect to the LDAP =
server's root=20
object<BR>Set oADsRootDSE =3D GetObject("</FONT><A =
href=3D"ldap://RootDSE"><FONT=20
face=3DArial color=3D#ff0000 size=3D2>LDAP://RootDSE</FONT></A><FONT =
face=3DArial=20
color=3D#ff0000 size=3D2>")</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>Set oADsSysInfo =3D=20
CreateObject("ADSystemInfo")</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Get distinguished =
name of the=20
current user<BR>sADsUser =3D oADsSysInfo.UserName<BR>WScript.Echo =
"Distinguished=20
name: " &amp; sADsUser</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Get user =
object<BR>sUserPath =3D=20
"LDAP://" &amp; sADsUser<BR>Set oADsContainer =3D=20
GetObject(sUserPath)</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>sUserOU =3D=20
oADsContainer.parent<BR>WScript.Echo "Users OU: " &amp;=20
<STRONG>sUserOU</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Get full name of =
the current=20
user<BR>sUserNameFull =3D oADsContainer.cn<BR>WScript.Echo "Full name: =
" &amp;=20
sUserNameFull</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Here is the format that the =
results are=20
returned.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2><STRONG>Distinguished =
name:=20
CN=3DPRH007,OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet<BR>Users =
OU:=20
</STRONG></FONT><A href=3D"ldap://t"><STRONG><A=20
href=3D"ldap://OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet"><FONT =
face=3DArial=20
size=3D2>LDAP://</FONT></STRONG></A><STRONG><FONT face=3DArial=20
=
size=3D2>OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet</FONT></A><BR><F=
ONT face=3DArial=20
color=3D#0000ff size=3D2>Full name: PRH007</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial color=3D#0000ff=20
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>Thanks,</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>Roger</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial color=3D#0000ff=20
size=3D2></FONT></STRONG>&nbsp;</DIV></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_0042_01C3CA0B.A88BEFA0--


Re: Get Users Parent container from query by Roger

Roger
Wed Dec 24 11:21:22 CST 2003

This is a multi-part message in MIME format.

------=_NextPart_000_0053_01C3CA10.0F6D1380
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Thanks for your quick response Richard. You've already been more help =
than should be allowed. In fact, most of the following code is yours.

Can you please tell me where to put the function, based on the code =
below to return the parsed DN to get the parent container.

Sorry but I'm dense when it comes to this, I just learning this stuff.

Please note in bold below where I've put it and it doesn't work. I'm =
sure you'll look at it and laugh.

Thanks,

Roger




Option Explicit

Dim strExcelPath, objConnection, objCommand, objRootDSE, strDNSDomain, =
strADsPath
Dim strFilter, strQuery, objRecordSet, strDN, strUserDN, strCN, strOU, =
strParentOU, strUPN, strSAM, objExcel, objSheet, k
Dim oADsSysInfo, oADsContainer, sADsUser, sUserPath, sUserOU

' Check for required arguments.
If Wscript.Arguments.Count < 1 Then
Wscript.Echo "Arguments <FileName> required. For example:" & vbCrLf _
& "wscript CreateUserList3.vbs d:\ADUsers\UserList3.xls"
Wscript.Quit(0)
End If

' Spreadsheet file to be created.
strExcelPath =3D Wscript.Arguments(0)

' Bind to Excel object.
Set objExcel =3D CreateObject("Excel.Application")
objExcel.Workbooks.Add

' Bind to worksheet.
Set objSheet =3D objExcel.ActiveWorkbook.Worksheets(1)
objSheet.Name =3D "Domain Users"
objSheet.Cells(1, 1).Value =3D "User Distinguished Name"
objSheet.Cells(1, 2).Value =3D "User cn"
objSheet.Cells(1, 3).Value =3D "User OU Container"
objSheet.Cells(1, 4).Value =3D "User userPrincipalName"
objSheet.Cells(1, 5).Value =3D "User sAMAccountName"


' Use ADO to search the domain for all users.
Set objConnection =3D CreateObject("ADODB.Connection")
Set objCommand =3D CreateObject("ADODB.Command")
objConnection.Provider =3D "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection =3D objConnection

' Determine the DNS domain from the RootDSE object.
Set objRootDSE =3D GetObject("LDAP://RootDSE")

' Code for finding parent object of user
' Connect to the LDAP server's root object
'Set oADsRootDSE =3D GetObject("LDAP://RootDSE")

Set oADsSysInfo =3D CreateObject("ADSystemInfo")

' Get distinguished name of the current user
sADsUser =3D oADsSysInfo.UserName
'WScript.Echo "Distinguished name: " & sADsUser

' Get user object
sUserPath =3D "LDAP://" & sADsUser
Set oADsContainer =3D GetObject(sUserPath)

sUserOU =3D oADsContainer.parent
'WScript.Echo "Users OU: " & sUserOU

' Get full name of the current user
'sUserNameFull =3D oADsContainer.cn
'WScript.Echo "Full name: " & sUserNameFull

strDNSDomain =3D objRootDSE.Get("defaultNamingContext")
strFilter =3D "(&(objectCategory=3Dperson)(objectClass=3Duser))"
strQuery =3D "<LDAP://" & strDNSDomain & ">;" & strFilter _
& ";distinguishedName,cn,userPrincipalName,sAMAccountName;subtree"
=20
objCommand.CommandText =3D strQuery
objCommand.Properties("Page Size") =3D 100
objCommand.Properties("Timeout") =3D 30
objCommand.Properties("Cache Results") =3D False
objCommand.Properties("Sort On") =3D "userPrincipalName"

' Enumerate all users. Write each user's Distinguished Name to the
' spreadsheet.
k =3D 2
Set objRecordSet =3D objCommand.Execute
Do Until objRecordSet.EOF

Function ParseOU(strDN)
ParseOU =3D Mid(strDN, InStr(strDN, "=3D") + 1)
ParseOU =3D Mid(ParseOU, InStr(ParseOU, "=3D") - 2)
End Function


strDN =3D objRecordSet.Fields("distinguishedName")
strCN =3D objRecordSet.Fields("cn")
strOU =3D objRecordSet.Fields("ParseOU")
strUPN =3D objRecordSet.Fields("userPrincipalName")
strSAM =3D objRecordSet.Fields("sAMAccountName")
objSheet.Cells(k, 1).Value =3D strDN
objSheet.Cells(k, 2).Value =3D strCN
objSheet.Cells(k, 3).Value =3D strOU
objSheet.Cells(k, 4).Value =3D strUPN
objSheet.Cells(k, 5).Value =3D strSAM
k =3D k + 1
objRecordSet.MoveNext
Loop

' Format the spreadsheet.
objSheet.Range("A1:E1").Font.Bold =3D True
objSheet.Select
objExcel.Columns(5).ColumnWidth =3D 100

' Save the spreadsheet.
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close

' Quit Excel.
objExcel.Application.Quit

' Clean up.
objConnection.Close
Set objConnection =3D Nothing
Set objCommand =3D Nothing
Set objRootDSE =3D Nothing
Set objRecordSet =3D Nothing
Set objSheet =3D Nothing
Set objExcel =3D Nothing

Wscript.Echo "Done"
"Richard Mueller [MVP]" <rlmueller-NOSPAM@ameritech.NOSPAM.net> wrote =
in message news:OJYYF5jyDHA.832@TK2MSFTNGP09.phx.gbl...
Hi,

Unfortunately, only Organizational Units have a mandatory "ou" =
attribute. And, "Parent" is a property method of the user object, not an =
attribute, so it cannot be retrieved directly with an ADO query. =
Instead, you will have to parse the distinguishedName (DN) attribute, or =
bind to the user object and use the "Parent" property method. It would =
be faster to parse the DN. For example, this function would parse a DN =
and return the Distinguished Name of the parent container/ou.

Function ParseOU(strDN)
ParseOU =3D Mid(strDN, InStr(strDN, "=3D") + 1)
ParseOU =3D Mid(ParseOU, InStr(ParseOU, "=3D") - 2)
End Function

If you are using ADO to retrieve information on all users in your =
domain, you can use this function in the loop where you enumerate the =
records returned by the query.

Actually, user objects have an "ou" attribute, but it is never has a =
value.

--=20
Richard
Microsoft MVP Scripting and ADSI
HilltopLab web site - http://www.rlmueller.net
--
"Roger" <hainesr3@nationwide.com> wrote in message =
news:OVudKziyDHA.540@tk2msftngp13.phx.gbl...
Does anyone know how I can include the users parent container from =
the following script? The "ou" portion (in BLACK) is where I was trying =
to gather that, but it doesn't work.

strDNSDomain =3D objRootDSE.Get("defaultNamingContext")
strFilter =3D "(&(objectCategory=3Dperson)(objectClass=3Duser))"
strQuery =3D "<LDAP://" & strDNSDomain & ">;" & strFilter _
& =
";distinguishedName,cn,ou,userPrincipalName,sAMAccountName;subtree"



The script below returns exactly what I'm looking for however, I =
can't figure out how to incorporate in into the query. I'm trying to =
populate
a spreadsheet with the information that is returned from the =
"sUserOU" variable.

' Connect to the LDAP server's root object
Set oADsRootDSE =3D GetObject("LDAP://RootDSE")

Set oADsSysInfo =3D CreateObject("ADSystemInfo")

' Get distinguished name of the current user
sADsUser =3D oADsSysInfo.UserName
WScript.Echo "Distinguished name: " & sADsUser

' Get user object
sUserPath =3D "LDAP://" & sADsUser
Set oADsContainer =3D GetObject(sUserPath)

sUserOU =3D oADsContainer.parent
WScript.Echo "Users OU: " & sUserOU

' Get full name of the current user
sUserNameFull =3D oADsContainer.cn
WScript.Echo "Full name: " & sUserNameFull

Here is the format that the results are returned.


Distinguished name: =
CN=3DPRH007,OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet
Users OU: LDAP://OU=3DAccounts,OU=3DSales,DC=3Dmydomain,DC=3Dnet
Full name: PRH007

Thanks,

Roger


------=_NextPart_000_0053_01C3CA10.0F6D1380
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1264" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Thanks for your quick response =
Richard.&nbsp;=20
You've already been more help than should be allowed.&nbsp; In fact, =
most of the=20
following code is yours.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Can you please tell me where to put the =
function,=20
based on the code below to return the parsed DN to get the parent=20
container.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Sorry but I'm dense when it comes to =
this, I just=20
learning this stuff.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Please note in bold below where I've =
put it and it=20
doesn't work.&nbsp; I'm sure you'll look at it and laugh.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thanks,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Roger</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Option Explicit</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Dim strExcelPath, objConnection, =
objCommand,=20
objRootDSE, strDNSDomain, strADsPath<BR>Dim strFilter, strQuery, =
objRecordSet,=20
strDN, strUserDN, strCN, strOU, strParentOU, strUPN, strSAM, objExcel, =
objSheet,=20
k<BR>Dim oADsSysInfo, oADsContainer, sADsUser, sUserPath, =
sUserOU</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Check for required arguments.<BR>If=20
Wscript.Arguments.Count &lt; 1 Then<BR>&nbsp; Wscript.Echo "Arguments=20
&lt;FileName&gt; required. For example:" &amp; vbCrLf =
_<BR>&nbsp;&nbsp;&nbsp;=20
&amp; "wscript CreateUserList3.vbs d:\ADUsers\UserList3.xls"<BR>&nbsp;=20
Wscript.Quit(0)<BR>End If</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Spreadsheet file to be =
created.<BR>strExcelPath =3D=20
Wscript.Arguments(0)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Bind to Excel object.<BR>Set objExcel =
=3D=20
CreateObject("Excel.Application")<BR>objExcel.Workbooks.Add</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Bind to worksheet.<BR>Set objSheet =
=3D=20
objExcel.ActiveWorkbook.Worksheets(1)<BR>objSheet.Name =3D "Domain=20
Users"<BR>objSheet.Cells(1, 1).Value =3D "User Distinguished=20
Name"<BR>objSheet.Cells(1, 2).Value =3D "User cn"<BR>objSheet.Cells(1, =
3).Value =3D=20
"User OU Container"<BR>objSheet.Cells(1, 4).Value =3D "User=20
userPrincipalName"<BR>objSheet.Cells(1, 5).Value =3D "User=20
sAMAccountName"</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><BR><FONT face=3DArial size=3D2>' Use ADO to search the domain for =
all=20
users.<BR>Set objConnection =3D CreateObject("ADODB.Connection")<BR>Set =
objCommand=20
=3D CreateObject("ADODB.Command")<BR>objConnection.Provider =3D=20
"ADsDSOOBject"<BR>objConnection.Open "Active Directory Provider"<BR>Set=20
objCommand.ActiveConnection =3D objConnection</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Determine the DNS domain from the =
RootDSE=20
object.<BR>Set objRootDSE =3D GetObject("</FONT><A =
href=3D"ldap://RootDSE"><FONT=20
face=3DArial size=3D2>LDAP://RootDSE</FONT></A><FONT face=3DArial=20
size=3D2>")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Code f
r finding parent object of =
user<BR>'=20
Connect to the LDAP server's root object<BR>'Set oADsRootDSE =3D=20
GetObject("</FONT><A href=3D"ldap://RootDSE"><FONT face=3DArial=20
size=3D2>LDAP://RootDSE</FONT></A><FONT face=3DArial =
size=3D2>")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Set oADsSysInfo =3D=20
CreateObject("ADSystemInfo")</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Get distinguished name of the current =

user<BR>sADsUser =3D oADsSysInfo.UserName<BR>'WScript.Echo =
"Distinguished name: "=20
&amp; sADsUser</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Get user object<BR>sUserPath =3D =
"LDAP://" &amp;=20
sADsUser<BR>Set oADsContainer =3D GetObject(sUserPath)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>sUserOU =3D =
oADsContainer.parent<BR>'WScript.Echo=20
"Users OU: " &amp; sUserOU</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Get full name of the current=20
user<BR>'sUserNameFull =3D oADsContainer.cn<BR>'WScript.Echo "Full name: =
" &amp;=20
sUserNameFull</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>strDNSDomain =3D=20
objRootDSE.Get("defaultNamingContext")<BR>strFilter =3D=20
"(&amp;(objectCategory=3Dperson)(objectClass=3Duser))"<BR>strQuery =3D =
"&lt;LDAP://"=20
&amp; strDNSDomain &amp; "&gt;;" &amp; strFilter _<BR>&nbsp; &amp;=20
";distinguishedName,cn,userPrincipalName,sAMAccountName;subtree"<BR>&nbsp=
;=20
<BR>objCommand.CommandText =3D strQuery<BR>objCommand.Properties("Page =
Size") =3D=20
100<BR>objCommand.Properties("Timeout") =3D =
30<BR>objCommand.Properties("Cache=20
Results") =3D False<BR>objCommand.Properties("Sort On") =3D=20
"userPrincipalName"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Enumerate all users. Write each =
user's=20
Distinguished Name to the<BR>' spreadsheet.<BR>k =3D 2<BR>Set =
objRecordSet =3D=20
objCommand.Execute<BR>Do Until objRecordSet.EOF</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2><STRONG>Function=20
ParseOU(strDN)<BR>&nbsp; ParseOU =3D Mid(strDN, InStr(strDN, "=3D") + =
1)<BR>&nbsp;=20
ParseOU =3D Mid(ParseOU, InStr(ParseOU, "=3D") - 2)<BR>End=20
Function</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><BR><FONT face=3DArial size=3D2>&nbsp; strDN =3D=20
objRecordSet.Fields("distinguishedName")<BR>&nbsp; strCN =3D=20
objRecordSet.Fields("cn")<BR>&nbsp; strOU =3D objRecordSet.Fields("<FONT =

color=3D#ff0000><STRONG>ParseOU</STRONG></FONT>")<BR>&nbsp; strUPN =3D=20
objRecordSet.Fields("userPrincipalName")<BR>&nbsp; strSAM =3D=20
objRecordSet.Fields("sAMAccountName")<BR>&nbsp; objSheet.Cells(k, =
1).Value =3D=20
strDN<BR>&nbsp; objSheet.Cells(k, 2).Value =3D strCN<BR>&nbsp; =
objSheet.Cells(k,=20
3).Value =3D strOU<BR>&nbsp; objSheet.Cells(k, 4).Value =3D =
strUPN<BR>&nbsp;=20
objSheet.Cells(k, 5).Value =3D strSAM<BR>&nbsp; k =3D k + 1<BR>&nbsp;=20
objRecordSet.MoveNext<BR>Loop</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Format the=20
spreadsheet.<BR>objSheet.Range("A1:E1").Font.Bold =3D=20
True<BR>objSheet.Select<BR>objExcel.Columns(5).ColumnWidth =3D =
100</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Save the=20
spreadsheet.<BR>objExcel.ActiveWorkbook.SaveAs=20
strExcelPath<BR>objExcel.ActiveWorkbook.Close</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Quit=20
Excel.<BR>objExcel.Application.Quit</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>' Clean =
up.<BR>objConnection.Close<BR>Set=20
objConnection =3D Nothing<BR>Set objCommand =3D Nothing<BR>Set =
objRootDSE =3D=20
Nothing<BR>Set objRecordSet =3D Nothing<BR>Set objSheet =3D =
Nothing<BR>Set objExcel=20
=3D Nothing</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Wscript.Echo "Done"</FONT></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Richard Mueller [MVP]" &lt;<A=20
=
href=3D"mailto:rlmueller-NOSPAM@ameritech.NOSPAM.net">rlmueller-NOSPAM@am=
eritech.NOSPAM.net</A>&gt;=20
wrote in message <A=20
=
href=3D"news:OJYYF5jyDHA.832@TK2MSFTNGP09.phx.gbl">news:OJYYF5jyDHA.832@T=
K2MSFTNGP09.phx.gbl</A>...</DIV>
<DIV><FONT size=3D2>Hi,</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Unfortunately, only Organizational Units have a =
mandatory=20
"ou" attribute. And, "Parent" is a property method of the user object, =
not an=20
attribute, so it cannot be retrieved directly with an ADO query. =
Instead, you=20
will have to parse the distinguishedName (DN)&nbsp;attribute, or bind =
to the=20
user object and use the "Parent" property method. It would be faster =
to parse=20
the DN. For example, this function would parse a DN and return the=20
Distinguished Name of the parent container/ou.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Function ParseOU(strDN)</FONT></DIV>
<DIV><FONT size=3D2>&nbsp; ParseOU =3D Mid(strDN, InStr(strDN, "=3D") =
+=20
1)</FONT></DIV>
<DIV><FONT size=3D2>&nbsp; ParseOU =3D Mid(ParseOU, InStr(ParseOU, =
"=3D") -=20
2)</FONT></DIV>
<DIV><FONT size=3D2>End Function</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>If you are using ADO to retrieve information on =
all users in=20
your domain, you can use this function in the loop where you enumerate =
the=20
records returned by the query.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>Actually, user objects have an "ou" attribute, but =
it is=20
never has a value.</FONT></DIV>
<DIV><FONT size=3D2><BR>-- <BR>Richard<BR>Microsoft MVP Scripting and=20
ADSI<BR>HilltopLab web site - <A=20
=
href=3D"http://www.rlmueller.net">http://www.rlmueller.net</A><BR>--</FON=
T></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Roger" &lt;<A=20
=
href=3D"mailto:hainesr3@nationwide.com">hainesr3@nationwide.com</A>&gt; =
wrote=20
in message <A=20
=
href=3D"news:OVudKziyDHA.540@tk2msftngp13.phx.gbl">news:OVudKziyDHA.540@t=
k2msftngp13.phx.gbl</A>...</DIV>
<DIV><FONT face=3DArial size=3D2>Does anyone know how I can include =
the users=20
parent container from the following script?&nbsp; The "ou" portion =
(in=20
BLACK) is where I was trying to gather that, but it doesn't=20
work.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 =
size=3D2><STRONG>strDNSDomain =3D=20
objRootDSE.Get("defaultNamingContext")<BR>strFilter =3D=20
"(&amp;(objectCategory=3Dperson)(objectClass=3Duser))"<BR>strQuery =
=3D=20
"&lt;LDAP://" &amp; strDNSDomain &amp; "&gt;;" &amp; strFilter =
_<BR>&nbsp;=20
&amp; ";distinguishedName,cn,<FONT=20
=
color=3D#000000>ou</FONT>,userPrincipalName,sAMAccountName;subtree"</STRO=
NG></FONT></DIV>
<DIV><STRONG><FONT face=3DArial color=3D#ff0000=20
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial color=3D#000000=20
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial =
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>The script below returns =
exactly what=20
I'm looking for however, I can't figure out how to incorporate in =
into the=20
query.&nbsp; I'm trying to populate</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>a spreadsheet with the =
information that=20
is returned from the "sUserOU" variable.</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial =
size=3D2></FONT></STRONG>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Connect to the =
LDAP server's=20
root object<BR>Set oADsRootDSE =3D GetObject("</FONT><A=20
href=3D"ldap://RootDSE"><FONT face=3DArial color=3D#ff0000=20
size=3D2>LDAP://RootDSE</FONT></A><FONT face=3DArial color=3D#ff0000 =

size=3D2>")</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>Set oADsSysInfo =3D =

CreateObject("ADSystemInfo")</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Get distinguished =
name of the=20
current user<BR>sADsUser =3D oADsSysInfo.UserName<BR>WScript.Echo=20
"Distinguished name: " &amp; sADsUser</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Get user =
object<BR>sUserPath =3D=20
"LDAP://" &amp; sADsUser<BR>Set oADsContainer =3D=20
GetObject(sUserPath)</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>sUserOU =3D=20
oADsContainer.parent<BR>WScript.Echo "Users OU: " &amp;=20
<STRONG>sUserOU</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2>' Get full name of =
the current=20
user<BR>sUserNameFull =3D oADsContainer.cn<BR>WScript.Echo "Full =
name: " &amp;=20
sUserNameFull</FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Here is the format that the =
results are=20
returned.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#ff0000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#