jarjarbinx1979
asked on
VBScript to Move AD Computers
What I am after is a script to move computers from one OU to another OU. Why? Well all new computers go to an "Unassigned" OU when they join the domain, then we have separate OU's for operating systems (2k and XP) so the script needs to determine the operating system then move to the correct OU for that operating system. However it should not move servers.
I have searched the WEB and have created the below script, but it doesn't seem to be working correctly....if anyone can help it will be appreciated
JJB
========================== ===
Option Explicit
Dim objRootDSE, strDNSDomain, objCommand, objConnection
Dim strBase, strFilter, strAttributes, strQuery, objRecordSet
Dim strDN, strOS, objW2kOU, objXPOU
' Bind to target W2k and XP OU containers.
Set objW2kOU = GetObject("LDAP://ou=2k,ou =workstati ons,ou=Com puters,ou= OU,dc=MY,d c=DOMAIN,d c=COM")
Set objXPOU = GetObject("LDAP://ou=XP,ou =workstati ons,ou=Com puters,ou= OU,dc=MY,d c=DOMAIN,d c=COM")
' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE" )
strDNSDomain = objRootDSE.Get("defaultNam ingContext ")
' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Comman d")
Set objConnection = CreateObject("ADODB.Connec tion")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnectio n = objConnection
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on all computer objects.
strFilter = "(objectCategory=computer) "
' Retrieve distinguishedName and operatingSystem attributes.
strAttributes = "distinguishedName,operati ngSystem"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Pag e Size") = 100
objCommand.Properties("Tim eout") = 30
objCommand.Properties("Cac he Results") = False
Set objRecordSet = objCommand.Execute
' Loop through all computer objects.
Do Until objRecordSet.EOF
' Make strings lower case.
strDN = LCase(objRecordSet.Fields( "distingui shedName") .Value)
strOS = LCase(objRecordset.Fields( "operating system").V alue)
' Check if computer object in cn=computers container.
If (InStr(strDN, "OU=Unassigned,OU=Computer s,OU=OU,DC =york,MY=g ov,DOMAIN= COM")) Then
' Check OS, skip servers.
If strOS = "windows 2000 Professional" Then
Set objMoveComputer = objW2kOU.MoveHere("LDAP:// " & strDN, vbNullString)
ElseIf strOS = "Windows XP Professional" Then
Set objMoveComputer = objXPOU.MoveHere("LDAP://" & strDN, vbNullString)
End If
End If
' Move to the next computer object.
objRecordSet.MoveNext
Loop
' Clean up.
Set objRootDSE = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing
Set objW2kOU = Nothing
Set objXPOU = Nothing
I have searched the WEB and have created the below script, but it doesn't seem to be working correctly....if anyone can help it will be appreciated
JJB
==========================
Option Explicit
Dim objRootDSE, strDNSDomain, objCommand, objConnection
Dim strBase, strFilter, strAttributes, strQuery, objRecordSet
Dim strDN, strOS, objW2kOU, objXPOU
' Bind to target W2k and XP OU containers.
Set objW2kOU = GetObject("LDAP://ou=2k,ou
Set objXPOU = GetObject("LDAP://ou=XP,ou
' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE"
strDNSDomain = objRootDSE.Get("defaultNam
' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Comman
Set objConnection = CreateObject("ADODB.Connec
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnectio
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on all computer objects.
strFilter = "(objectCategory=computer)
' Retrieve distinguishedName and operatingSystem attributes.
strAttributes = "distinguishedName,operati
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Pag
objCommand.Properties("Tim
objCommand.Properties("Cac
Set objRecordSet = objCommand.Execute
' Loop through all computer objects.
Do Until objRecordSet.EOF
' Make strings lower case.
strDN = LCase(objRecordSet.Fields(
strOS = LCase(objRecordset.Fields(
' Check if computer object in cn=computers container.
If (InStr(strDN, "OU=Unassigned,OU=Computer
' Check OS, skip servers.
If strOS = "windows 2000 Professional" Then
Set objMoveComputer = objW2kOU.MoveHere("LDAP://
ElseIf strOS = "Windows XP Professional" Then
Set objMoveComputer = objXPOU.MoveHere("LDAP://"
End If
End If
' Move to the next computer object.
objRecordSet.MoveNext
Loop
' Clean up.
Set objRootDSE = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing
Set objW2kOU = Nothing
Set objXPOU = Nothing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm pretty sure that was it.
I can't see any other problems with your script.
It fails for me as well, until I change the INSTR comparison to be lower case
I can't see any other problems with your script.
It fails for me as well, until I change the INSTR comparison to be lower case
How should that INSTR syntax look when forced to lowercase?
meant to say strDN and strOS