Changes
→Pulling workstation names to hosts.xml from Active Directory OUs automatically with vbScript
Wscript.Echo "Finished..."
</source>
Alternatively if you want hosts listed with each OU they reside within separately to allow you to apply software to the higher leve OUs, this should do the trick:
<source lang="vb">
' Short Description: Output AD Hosts to hosts.xml file
' Original source code: http://wpkg.org/WPKG_with_Active_Directory#Pulling_workstation_names_to_hosts.xml_from_Active_Directory_OUs_automatically_with_vbScript
' Modifed by: Marc Ozin
' Modified Date: 2010-03-02
' Modification:
' Hosts generated with multiple Profile-IDs to show each sub OU the Host resides within.
' e.g. if Computer1 is contained within Head-Office/Finance/Payroll the following Profile-IDs will be generated:
' <host name="Computer1">
' <profile-id="/root" />
' <profile-id="/root/Head-Office" />
' <profile-id="/root/Head-Office/Finance" />
' <profile-id="/root/Head-Office/Finance/Payroll" />
' </host>
Const ADS_SCOPE_SUBTREE = 2
set fs = CreateObject("Scripting.FileSystemObject")
set textstream = fs.CreateTextFile("hosts.xml", True)
textstream.WriteLine "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf
textstream.WriteLine "<!-- automagically generated with " & Wscript.ScriptFullName
textstream.WriteLine " Date: " & Date() & " -->" & vbCrLf & vbCrLf
textstream.WriteLine "<wpkg>"
Set rootDSE = GetObject("LDAP://RootDSE")
domainContainer = rootDSE.Get("defaultNamingContext")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, distinguishedName from 'LDAP://" & domainContainer & "' " _
& "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
'Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
'Wscript.Echo "distinguishedName: " & objRecordSet.Fields("distinguishedName").Value
arrPath = Split(objRecordSet.Fields("distinguishedName").Value, ",")
strOU = ""
textstream.WriteLine vbTab & "<host name=""" & objRecordSet.Fields("Name").Value & """>"
for each a in arrPath
if left(a,2) = "OU" Then
strOU = "/" & right(a,len(a) - 3) & strOU
End If
Next
'Wscript.Echo "Path: " & StrOU
strOU = "root" & strOU
arrProfiles=Split(StrOU,"/")
sFullProfile = ""
iDepth=0
for each sProfile in arrProfiles
sFullProfile = sFullProfile & "/" & sProfile
textstream.WriteLine vbTab & vbTab & "<profile-id=""" & sFullProfile & """ />"
next
textstream.WriteLine vbTab & "</host>"
objRecordSet.MoveNext
Loop
textstream.WriteLine "</wpkg>"
textstream.close
Wscript.Echo "Finished..."
</source>
[[category:Documentation]]
[[category:Installation]]