trarthur
asked on
Script to export computer accounts from an OU and all child OUs
I'm looking for a VB script that will export the computer accounts in an OU and child OU's to a txt file.
It needs to be able to drill down to the child OU's.
Can anyone help?
Thanks!
It needs to be able to drill down to the child OU's.
Can anyone help?
Thanks!
Do you want it to pull email addresses also?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@mcrossland - I don''t need email addresses. I just need the computer account names.
@LauraEHunterMVP - Thanks. I'll check it out. I forgot about joeware. Do you happen to know if you can configure adfind to start at a specific OU and recurse through its child OU's?
@LauraEHunterMVP - Thanks. I'll check it out. I forgot about joeware. Do you happen to know if you can configure adfind to start at a specific OU and recurse through its child OU's?
By default any search will recurse down through its starting point.
The -default switch will start at the domain root of the currently-logged-on domain. If you want to start searching at the "BLAH" OU, add the -rb switch for "Relative base."
adfind -default -rb ou=BLAH ... and the rest of the command is the same.
The -default switch will start at the domain root of the currently-logged-on domain. If you want to start searching at the "BLAH" OU, add the -rb switch for "Relative base."
adfind -default -rb ou=BLAH ... and the rest of the command is the same.
Here's a vbscript I had for doing that for users...I've attempted to modify it for computers...should report back from the top domain down through all OU levels...
'EnumOUs.vbs
Dim objFSO, strFileName, RptFile
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
strFileName="C:\Output.csv "
Set RptFile = objFSODim objFSO, strFileName, RptFile
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
strFileName="C:\Output.csv "
Set RptFile = objFSO.CreateTextFile(strF ileName)
Dim objRoot, objDomain
Set objRoot = GetObject("LDAP://RootDSE" )
Set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNaming Context"))
EnumOUs(objDomain.ADsPath)
RptFile.Close
Set RptFile=Nothing
Set objUsers=Nothing
Set objOUs=Nothing
Set objDomain=Nothing
Set objRoot=Nothing
Set objFSO=Nothing
wscript.quit
Sub EnumOUs(adspath)
Dim objOUs, OU
Set objOUs = GetObject(adspath)
objOUs.Filter = Array("OrganizationalUnit" )
Call EnumPCs(objOUs.ADsPath)
For Each OU In objOUs
RptFile.WriteLine Replace(Ucase(OU.Name), "OU=", "")
Call EnumOUs(OU.ADsPath)
Next
End Sub
Sub EnumPCs(adspath)
Dim objPCs, PC
Set objPCs= GetObject(adspath)
objUsers.Filter = Array("Computer")
RptFile.WriteLine
For Each PC In objPCs
RptFile.WriteLine PC.Name
Next
RptFile.WriteLine
End Sub
'EnumOUs.vbs
Dim objFSO, strFileName, RptFile
Set objFSO = CreateObject("Scripting.Fi
strFileName="C:\Output.csv
Set RptFile = objFSODim objFSO, strFileName, RptFile
Set objFSO = CreateObject("Scripting.Fi
strFileName="C:\Output.csv
Set RptFile = objFSO.CreateTextFile(strF
Dim objRoot, objDomain
Set objRoot = GetObject("LDAP://RootDSE"
Set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNaming
EnumOUs(objDomain.ADsPath)
RptFile.Close
Set RptFile=Nothing
Set objUsers=Nothing
Set objOUs=Nothing
Set objDomain=Nothing
Set objRoot=Nothing
Set objFSO=Nothing
wscript.quit
Sub EnumOUs(adspath)
Dim objOUs, OU
Set objOUs = GetObject(adspath)
objOUs.Filter = Array("OrganizationalUnit"
Call EnumPCs(objOUs.ADsPath)
For Each OU In objOUs
RptFile.WriteLine Replace(Ucase(OU.Name), "OU=", "")
Call EnumOUs(OU.ADsPath)
Next
End Sub
Sub EnumPCs(adspath)
Dim objPCs, PC
Set objPCs= GetObject(adspath)
objUsers.Filter = Array("Computer")
RptFile.WriteLine
For Each PC In objPCs
RptFile.WriteLine PC.Name
Next
RptFile.WriteLine
End Sub
ASKER
Thanks Laura. It mostly works. I'm getting ALL objects in the specified OU. Groups, user accounts.
I'm sure there is a switch I'm not setting correctly.
AdFind.exe -default -rb ou=OU1 (objectcategory=computer) -dn
I'm sure there is a switch I'm not setting correctly.
AdFind.exe -default -rb ou=OU1 (objectcategory=computer) -dn
Sorry, I typoed. (Was entering the command off the top of my head without testing syntax, my bad.)
Add a "-f" in front of the (objectcategory=computer) bit. "-f" is the switch to specify an LDAP filter of computer objects only, without it you'll get all objects returned as you're seeing.
Add a "-f" in front of the (objectcategory=computer) bit. "-f" is the switch to specify an LDAP filter of computer objects only, without it you'll get all objects returned as you're seeing.
ASKER
Perfect! That was it! Thank you!!
ASKER
SirBounty - if I wanted to use your script, how would I modify it to start at a specific OU and work its way down just that OU?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
SirBounty - thanks!
I'm getting an error though:
Untitled1.vbs(49, 5) Microsoft VBScript runtime error: Object required: 'objComputers'
Line 49 = objComputers.Filter = Array("Computer")
Here is the script:
'EnumOUs.vbs
Dim objFSO, strFileName, RptFile
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
strFileName="C:\Output.csv "
Set RptFile = objFSO.CreateTextFile(strF ileName)
Dim objDomain
Set objDomain = GetObject("LDAP://OU=foo,D C=bar,DC=n et") '<<<<Change this to the parent OU >>>>>>
EnumOUs(objDomain.ADsPath)
RptFile.Close
Set RptFile=Nothing
Set objUsers=Nothing
Set objOUs=Nothing
Set objDomain=Nothing
Set objRoot=Nothing
Set objFSO=Nothing
Set objComputers=Nothing
wscript.quit
Sub EnumOUs(adspath)
Dim objOUs, OU
Set objOUs = GetObject(adspath)
objOUs.Filter = Array("OrganizationalUnit" )
Call EnumPCs(objOUs.ADsPath)
For Each OU In objOUs
RptFile.WriteLine Replace(Ucase(OU.Name), "OU=", "")
Call EnumOUs(OU.ADsPath)
Next
End Sub
Sub EnumPCs(adspath)
Dim objPCs, PC
Set objPCs= GetObject(adspath)
objComputers.Filter = Array("Computer")
RptFile.WriteLine
For Each PC In objPCs
RptFile.WriteLine PC.Name
Next
RptFile.WriteLine
End Sub
I'm getting an error though:
Untitled1.vbs(49, 5) Microsoft VBScript runtime error: Object required: 'objComputers'
Line 49 = objComputers.Filter = Array("Computer")
Here is the script:
'EnumOUs.vbs
Dim objFSO, strFileName, RptFile
Set objFSO = CreateObject("Scripting.Fi
strFileName="C:\Output.csv
Set RptFile = objFSO.CreateTextFile(strF
Dim objDomain
Set objDomain = GetObject("LDAP://OU=foo,D
EnumOUs(objDomain.ADsPath)
RptFile.Close
Set RptFile=Nothing
Set objUsers=Nothing
Set objOUs=Nothing
Set objDomain=Nothing
Set objRoot=Nothing
Set objFSO=Nothing
Set objComputers=Nothing
wscript.quit
Sub EnumOUs(adspath)
Dim objOUs, OU
Set objOUs = GetObject(adspath)
objOUs.Filter = Array("OrganizationalUnit"
Call EnumPCs(objOUs.ADsPath)
For Each OU In objOUs
RptFile.WriteLine Replace(Ucase(OU.Name), "OU=", "")
Call EnumOUs(OU.ADsPath)
Next
End Sub
Sub EnumPCs(adspath)
Dim objPCs, PC
Set objPCs= GetObject(adspath)
objComputers.Filter = Array("Computer")
RptFile.WriteLine
For Each PC In objPCs
RptFile.WriteLine PC.Name
Next
RptFile.WriteLine
End Sub
ASKER
SirBounty - Nevermind, I found the issue. objComputers was never declared. I changed it to objPCs and it runs like a champ. Thanks!
:^) Glad to hear it.
ASKER
Thanks to both LauraEHunterMVP and SirBounty for helping out. Different options for essentially the same result. I'm going to split the points since both offered a solution. The VBscript gets more since that was what I'm looking for. Thanks again folks!
For the others who may run across this thread looking to solve the same problem I had, here is a summary.
VBscript solution:
'EnumOUComputers.vbs
Dim objFSO, strFileName, RptFile
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
strFileName="C:\Output.csv " '<<<<Change this to the output name and location of your choice >>>>>>
Set RptFile = objFSO.CreateTextFile(strF ileName)
Dim objDomain
Set objDomain = GetObject("LDAP://OU=foo,D C=bar,DC=c om") '<<<<Change this to the parent OU >>>>>>
EnumOUs(objDomain.ADsPath)
RptFile.Close
Set RptFile=Nothing
Set objUsers=Nothing
Set objOUs=Nothing
Set objDomain=Nothing
Set objRoot=Nothing
Set objFSO=Nothing
Set objComputers=Nothing
wscript.quit
Sub EnumOUs(adspath)
Dim objOUs, OU
Set objOUs = GetObject(adspath)
objOUs.Filter = Array("OrganizationalUnit" )
Call EnumPCs(objOUs.ADsPath)
For Each OU In objOUs
RptFile.WriteLine Replace(Ucase(OU.Name), "OU=", "")
Call EnumOUs(OU.ADsPath)
Next
End Sub
Sub EnumPCs(adspath)
Dim objPCs, PC
Set objPCs= GetObject(adspath)
objPCs.Filter = Array("Computer")
RptFile.WriteLine
For Each PC In objPCs
RptFile.WriteLine PC.Name
Next
RptFile.WriteLine
End Sub
adfind solution:
AdFind.exe -default -rb ou=FOO -f "objectcategory=computer" -dn
Replace FOO with the OU you wish to enumerate.
The AdFind .exe can be found at www.joeware.net
For the others who may run across this thread looking to solve the same problem I had, here is a summary.
VBscript solution:
'EnumOUComputers.vbs
Dim objFSO, strFileName, RptFile
Set objFSO = CreateObject("Scripting.Fi
strFileName="C:\Output.csv
Set RptFile = objFSO.CreateTextFile(strF
Dim objDomain
Set objDomain = GetObject("LDAP://OU=foo,D
EnumOUs(objDomain.ADsPath)
RptFile.Close
Set RptFile=Nothing
Set objUsers=Nothing
Set objOUs=Nothing
Set objDomain=Nothing
Set objRoot=Nothing
Set objFSO=Nothing
Set objComputers=Nothing
wscript.quit
Sub EnumOUs(adspath)
Dim objOUs, OU
Set objOUs = GetObject(adspath)
objOUs.Filter = Array("OrganizationalUnit"
Call EnumPCs(objOUs.ADsPath)
For Each OU In objOUs
RptFile.WriteLine Replace(Ucase(OU.Name), "OU=", "")
Call EnumOUs(OU.ADsPath)
Next
End Sub
Sub EnumPCs(adspath)
Dim objPCs, PC
Set objPCs= GetObject(adspath)
objPCs.Filter = Array("Computer")
RptFile.WriteLine
For Each PC In objPCs
RptFile.WriteLine PC.Name
Next
RptFile.WriteLine
End Sub
adfind solution:
AdFind.exe -default -rb ou=FOO -f "objectcategory=computer" -dn
Replace FOO with the OU you wish to enumerate.
The AdFind .exe can be found at www.joeware.net