I have computers in various OUs and they have been move around so they are in
the wrong OU's.

Is there are query/script or any way I can find which computer belongs to
which OU? Instead we manually go and check each of them???

Advise please.

Thanks,

RE: Computers in OU [WildPacket] by Chuck

Chuck
Wed Jan 25 10:00:07 CST 2006

This will dump the distinguished name of every computer object in the current
domain, from which you can extract hostname and OU. You can redirect this to
a text file and import into Excel as a CSV to analyze.

It may take a while to complete depending on the size of your domain, in my
case it takes about 2 minutes to dump ~55000 objects when redirected to a
text file.
----------------------
sCurDomain = GetObject("LDAP://rootDSE").Get("defaultNamingContext")

Dim oConnection : Set oConnection = CreateObject("ADODB.Connection")
oConnection.Provider = "ADsDSOObject"
oConnection.Open

Dim oCommand : Set oCommand = CreateObject("ADODB.Command")
Set oCommand.ActiveConnection = oConnection
oCommand.Properties("Page Size") = 100
oCommand.CommandText = "<GC://" & sCurDomain &
">;(objectCategory=Computer);distinguishedName;Subtree"

Dim oRecordSet : Set oRecordSet = oCommand.Execute
oRecordSet.MoveFirst
Do Until oRecordSet.EOF
Wscript.Echo oRecordSet.Fields("distinguishedName").Value
oRecordSet.MoveNext
Loop

"WILDPACKET" wrote:

> I have computers in various OUs and they have been move around so they are in
> the wrong OU's.
>
> Is there are query/script or any way I can find which computer belongs to
> which OU? Instead we manually go and check each of them???
>
> Advise please.
>
> Thanks,

RE: Computers in OU [WildPacket] by WILDPACKET

WILDPACKET
Thu Jan 26 14:02:02 CST 2006

Thank you Chuck that helped.




"Chuck" wrote:

> This will dump the distinguished name of every computer object in the current
> domain, from which you can extract hostname and OU. You can redirect this to
> a text file and import into Excel as a CSV to analyze.
>
> It may take a while to complete depending on the size of your domain, in my
> case it takes about 2 minutes to dump ~55000 objects when redirected to a
> text file.
> ----------------------
> sCurDomain = GetObject("LDAP://rootDSE").Get("defaultNamingContext")
>
> Dim oConnection : Set oConnection = CreateObject("ADODB.Connection")
> oConnection.Provider = "ADsDSOObject"
> oConnection.Open
>
> Dim oCommand : Set oCommand = CreateObject("ADODB.Command")
> Set oCommand.ActiveConnection = oConnection
> oCommand.Properties("Page Size") = 100
> oCommand.CommandText = "<GC://" & sCurDomain &
> ">;(objectCategory=Computer);distinguishedName;Subtree"
>
> Dim oRecordSet : Set oRecordSet = oCommand.Execute
> oRecordSet.MoveFirst
> Do Until oRecordSet.EOF
> Wscript.Echo oRecordSet.Fields("distinguishedName").Value
> oRecordSet.MoveNext
> Loop
>
> "WILDPACKET" wrote:
>
> > I have computers in various OUs and they have been move around so they are in
> > the wrong OU's.
> >
> > Is there are query/script or any way I can find which computer belongs to
> > which OU? Instead we manually go and check each of them???
> >
> > Advise please.
> >
> > Thanks,

RE: Computers in OU [WildPacket] by Dmitry

Dmitry
Thu Feb 09 13:09:28 CST 2006

I use this function as a part of the logon script. It processes the
Distinguished Name attribute to return the OU name. I am not taking any
credit for it, it is based on an example by Microsoft scripting guys.

Best regards,

Dmitry


Function CheckOU(strComputer)
' -----------------------------------------------
' This function takes computer name as an input
' and returns the name of OU computer resides in
' -----------------------------------------------

Const ADS_SCOPE_SUBTREE = 2


Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
"SELECT Name, distinguishedName FROM 'LDAP://dc=example,dc=com' " _
& "WHERE objectCategory='computer'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
intTestOU = InStr(objRecordSet.Fields("Name").Value, strComputer)
If intTestOU <> 0 Then
strDN = objRecordSet.Fields("distinguishedName").Value
arrPath = Split(strDN, ",")
intLength = Len(arrPath(1))
intNameLength = intLength - 3
checkOU = Right(arrPath(1), intNameLength)
Wscript.Echo "Computer " &strComputer &" resides in the following OU: "
&checkOU

End If

objRecordSet.MoveNext
Loop
End Function

"WILDPACKET" wrote:

> Thank you Chuck that helped.
>
>
>
>
> "Chuck" wrote:
>
> > This will dump the distinguished name of every computer object in the current
> > domain, from which you can extract hostname and OU. You can redirect this to
> > a text file and import into Excel as a CSV to analyze.
> >
> > It may take a while to complete depending on the size of your domain, in my
> > case it takes about 2 minutes to dump ~55000 objects when redirected to a
> > text file.
> > ----------------------
> > sCurDomain = GetObject("LDAP://rootDSE").Get("defaultNamingContext")
> >
> > Dim oConnection : Set oConnection = CreateObject("ADODB.Connection")
> > oConnection.Provider = "ADsDSOObject"
> > oConnection.Open
> >
> > Dim oCommand : Set oCommand = CreateObject("ADODB.Command")
> > Set oCommand.ActiveConnection = oConnection
> > oCommand.Properties("Page Size") = 100
> > oCommand.CommandText = "<GC://" & sCurDomain &
> > ">;(objectCategory=Computer);distinguishedName;Subtree"
> >
> > Dim oRecordSet : Set oRecordSet = oCommand.Execute
> > oRecordSet.MoveFirst
> > Do Until oRecordSet.EOF
> > Wscript.Echo oRecordSet.Fields("distinguishedName").Value
> > oRecordSet.MoveNext
> > Loop
> >
> > "WILDPACKET" wrote:
> >
> > > I have computers in various OUs and they have been move around so they are in
> > > the wrong OU's.
> > >
> > > Is there are query/script or any way I can find which computer belongs to
> > > which OU? Instead we manually go and check each of them???
> > >
> > > Advise please.
> > >
> > > Thanks,