Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Guy's Scripting Ezine 27 - Moving Computers
This week's script is mission impossible. Given more time, I would
have liked to have broken the script into simpler sections, then assembled
it.
This weeks script is extremely difficult. There are three things you
must do to succeed: a) Pay minute attention to detail, b) Immerse yourself
in the scenario, c) amend the script to match your domain.
The scenario is that you wish to move a bunch of computers from one OU to
another. My example uses Accounts OU as the source and Payroll OU as the
destination.
In the original brief it was to move only those machines whose name contained
a particular string. What the customer wanted was to move machines whose
name began with XP and leave all the others where they were.
Preparation:
Go to Active Directory. Create an OU called Accounts, and another OU
called payroll. Create 3 or 4 computer objects in the Accounts OU.
Go through the script looking for domain references to amend e.g. dc=cp,
dc=com. Make sure you change this to your domain name(s).
Specifically, find, and edit these lines:
"Select Name, Location from 'LDAP://OU=Accounts,dc=cp,dc=com'" _
strSourceOU = ",OU=Accounts,DC=cp,DC=com"
strDestinationOU = "OU=Payroll,DC=cp,DC=com"
Instructions
- Follow the preparation above, create two OUs in Active Directory.
- Copy and paste the script below into notepad. Alternatively, use a
script editor like VBsEdit.
- Be sure to make amendments so the script reflects YOUR domain name, not
mine.
- Save the file with .vbs extension e.g. MoveComputer.vbs
- Double click and observe the message box
- If you get errors, pay attention to syntax and object names.
' MoveComputer.vbs - A very difficult script.
' VBScript to Move computers from the Accounts to the Payroll OU
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.2 - April 25th 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objConnection, objCommand, objRecordSet, objNewOU, objMoveComputer
Dim strComputer, strInitial, strSourceOU, strDestinationOU
Dim intCounter
' This is a tricky section. Makes a link to Active Directory
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
' Selects all the computer objects (not Users) in the Accounts OU
' NB Change the LDAP://ou.... to reflect your domain.
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Accounts,dc=cp,dc=com'" _
& "where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
' Treats the computer objects as a set of record cards
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
' Get all the records
Do Until objRecordSet.EOF
strComputer = objRecordSet.Fields("Name").Value
strComputer = "CN="& strComputer
intCounter = intCounter +1
Wscript.Echo "Computer Name: " & strComputer
' N.B. change the next two lines to OUs in YOUR domain.
strSourceOU = ",OU=Accounts,DC=cp,DC=com"
strDestinationOU = "OU=Payroll,DC=cp,DC=com"
Set objNewOU = GetObject("LDAP://" & strDestinationOU)
Set objMoveComputer = objNewOU.MoveHere _
("LDAP://" & strComputer & strSourceOU, strComputer)
objRecordSet.MoveNext
Loop
Wscript.Echo intCounter & " Computers moved to " & strDestinationOU
WScript.Quit
' End of example VBScript
Learning Points
Note 1: The top sections makes a connection to Active Directory. In my
mind's eye, its like a pipe to draw off the computer names.
Note 2: Spot the DO WHILE .... LOOP and note how it uses the EOF (End of
Record) marker to halt the looping.
Note 3: If you have problems, check the OU names, check the domain
references.
Summary:
This is a particularly challenging script, it combines numerous variables and
components to move computers from one OU to another.
This week there are no 'Out Takes'. However, here is an example
which filters the machine names that you want to move.
Note: Here is the crucial line:
If strInitial = "XP" Then
..
End If.
' MoveComputer.vbs - A very difficult script.
' VBScript to Move computers from the Accounts to the Payroll OU
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.5 - April 25th 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objConnection, objCommand, objRecordSet, objNewOU, objMoveComputer
Dim strComputer, strInitial, strSourceOU, strDestinationOU
Dim intCounter
' This is a tricky section. Makes a link to Active Directory
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
' Selects all the computer objects (not Users) in the Accounts OU
' NB Change the LDAP://ou.... to reflect your domain.
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Accounts,dc=cp,dc=com'" _
& "where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
' Treats the computer objects as a set of record cards
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
' Get all the records
Do Until objRecordSet.EOF
strInitial = UCase(Left(objRecordSet.Fields("Name").Value,2))
' Should you wish to Filter Computers, then amend the next line.
If strInitial = "XP" Then
strComputer = objRecordSet.Fields("Name").Value
strComputer = "CN="& strComputer
intCounter = intCounter +1
Wscript.Echo "Computer Name: " & strComputer
' Nota Bene: change the next two lines to OUs in YOUR domain.
strSourceOU = ",OU=Accounts,DC=cp,DC=com"
strDestinationOU = "OU=Payroll,DC=cp,DC=com"
Set objNewOU = GetObject("LDAP://" & strDestinationOU)
Set objMoveComputer = objNewOU.MoveHere _
("LDAP://" & strComputer & strSourceOU, strComputer)
' Next line matches If strInitial
End if
objRecordSet.MoveNext
Loop
Wscript.Echo intCounter & " " & strInitial & " moved"
' End of example VBScript
The concept is to move computers from one OU to another. The second
part applies a filter so that you can just move computers that match a
particular criteria.
This is another marathon script out of the 'mission impossible' stable.
However, applying that old saying 'yard by yard and it's hard, but inch by inch
and it's a cinch' - you can get it to work.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|