script for OU info

  • Thread starter Thread starter ade
  • Start date Start date
A

ade

Hi,

Is tere a script that will tell me which users are in what OU's please? I
have been asked to produce a report for this and will need it to run once
per month, I appreciate I could go thruogh ADU&C manually, but theres quite
a lot of OU's.

TIA
 
This script iterates through all containers and OUs and outputs all users
under each one.
It must be run with cscript, and may output computers too (I can't remember
if I've fixed the query or not).

It's not the neatest, but you can copy and paste and format accordingly.

Hope it helps.


' displayUserInfo.vbs
'
' Simple script that iterates through all OUs and Container objects and
displays all
' user account objects in each container
'
' Author: Paul Williams
' Date: 21-02-2005
' Version: 1.1.1.
'
' Last Updated:21-02-2005
'
' USEAGE:
'
' cscript displayUserInfo.vbs [ou=ouname,dc=domain-name,dc=com]
'
' This script simply queries all container and OU objects from the Base DN
' (RootDSE.defaultNamingContext), or from a search base entered in the
format of a
' DN as an argument, and then outputs all user objects returned from each
' container object.
'
Option explicit

dim objBase,objOu,objCont,objUsr,objArgs,objRootDSE
dim
strOu,strCn,strDisplayName,strSAMAccountName,strMail,strUserPrincipalName
dim strProxyAddresses
dim strTokenGroups,strObjectSID,strSidHistory

set objArgs=wscript.arguments

if objArgs.count<>0 then
set objBase=getObject("LDAP://"&objArgs(0))

echo("LDAP://"&objArgs(0)&vbCrLf)
else
set objRootDSE=getObject("LDAP://RootDSE")
set objBase=getObject("LDAP://"&objRootDSE.get("defaultNamingContext"))

echo("LDAP://"&objRootDSE.get("defaultNamingContext")&vbCrLf)
end if

objBase.filter=array("container","organizationalUnit")

for each objOu in objBase
On error resume next

strOu=objOu.ADsPath
echo(vbCrLf&strOu)

set objCont=getObject(strOu)

objCont.filter=array("person")

for each objUsr in objCont
strCn=vbTab&objUsr.get("cn")
strDisplayName=vbTab&objUsr.get("displayName")
strSAMAccountName=vbTab&objUsr.get("sAMAccountName")
strMail=vbTab&objUsr.get("mail")
strUserPrincipalName=vbTab&objUsr.get("userPrincipalName")
strProxyAddresses=vbTab&objUsr.get("proxyAddresses")

strTokenGroups=vbTab&objUsr.get("tokenGroups")
strObjectSID=vbTab&objUsr.get("objectSID")
strSidHistory=vbTab&objUsr.get("sidHistory")

if err.number<>0 then err.clear()

echo
strCn&strDisplayName&strSAMAccountName&strMail&strUserPrincipalName&strProxyAddresses
next
next



' *********************************************************************
' echo(string messageToEcho)
'
' Sub routine simply echos the passed string.
' Sub used for outputting all information to the screen/ console
'
' *********************************************************************
Private Sub echo(strMessage)
wscript.echo strMessage
End Sub
 
Thanks Paul,

One quick question, I have installed csript and pasted your script into it,
but when I click run I get an error 'line(5):error:parse error' I am
running this from a desktop, does it need to be done on a dc?

Any help much appreciated


ptwilliams said:
This script iterates through all containers and OUs and outputs all users
under each one.
It must be run with cscript, and may output computers too (I can't remember
if I've fixed the query or not).

It's not the neatest, but you can copy and paste and format accordingly.

Hope it helps.


' displayUserInfo.vbs
'
' Simple script that iterates through all OUs and Container objects and
displays all
' user account objects in each container
'
' Author: Paul Williams
' Date: 21-02-2005
' Version: 1.1.1.
'
' Last Updated:21-02-2005
'
' USEAGE:
'
' cscript displayUserInfo.vbs [ou=ouname,dc=domain-name,dc=com]
'
' This script simply queries all container and OU objects from the Base DN
' (RootDSE.defaultNamingContext), or from a search base entered in the
format of a
' DN as an argument, and then outputs all user objects returned from each
' container object.
'
Option explicit

dim objBase,objOu,objCont,objUsr,objArgs,objRootDSE
dim
strOu,strCn,strDisplayName,strSAMAccountName,strMail,strUserPrincipalName
dim strProxyAddresses
dim strTokenGroups,strObjectSID,strSidHistory

set objArgs=wscript.arguments

if objArgs.count<>0 then
set objBase=getObject("LDAP://"&objArgs(0))

echo("LDAP://"&objArgs(0)&vbCrLf)
else
set objRootDSE=getObject("LDAP://RootDSE")
set objBase=getObject("LDAP://"&objRootDSE.get("defaultNamingContext"))

echo("LDAP://"&objRootDSE.get("defaultNamingContext")&vbCrLf)
end if

objBase.filter=array("container","organizationalUnit")

for each objOu in objBase
On error resume next

strOu=objOu.ADsPath
echo(vbCrLf&strOu)

set objCont=getObject(strOu)

objCont.filter=array("person")

for each objUsr in objCont
strCn=vbTab&objUsr.get("cn")
strDisplayName=vbTab&objUsr.get("displayName")
strSAMAccountName=vbTab&objUsr.get("sAMAccountName")
strMail=vbTab&objUsr.get("mail")
strUserPrincipalName=vbTab&objUsr.get("userPrincipalName")
strProxyAddresses=vbTab&objUsr.get("proxyAddresses")

strTokenGroups=vbTab&objUsr.get("tokenGroups")
strObjectSID=vbTab&objUsr.get("objectSID")
strSidHistory=vbTab&objUsr.get("sidHistory")

if err.number<>0 then err.clear()

echo
strCn&strDisplayName&strSAMAccountName&strMail&strUserPrincipalName&strProxy
Addresses
next
next



' *********************************************************************
' echo(string messageToEcho)
'
' Sub routine simply echos the passed string.
' Sub used for outputting all information to the screen/ console
'
' *********************************************************************
Private Sub echo(strMessage)
wscript.echo strMessage
End Sub



--
Paul Williams

http://www.msresource.net/
http://forums.msresource.net/
 
You paste the script into a text file and save it with a .vbs file extension.

You then run the script from the command line as follows:

C:\>cscript filename.vbs
 
Back
Top