Guy's Scripting Ezine 27 - Moving Computers
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.
Guy Recommends: Solarwinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click and import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in the free download, and all 3 have been approved by Microsoft:
- Bulk-import new users into Active Directory.
- Seek and zap unwanted user accounts.
- Find inactive computers.
Download your FREE bulk import tool.
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.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
Solarwinds'
Orion performance monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is the ability to monitor the health of individual VMWare
virtual machines. If you are interested in troubleshooting, and creating
network maps, then I recommend that you take advantage of Solarwinds' offer.
Download a free trial of
the Network Performance Monitor.
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.
See More Active Directory VBScripts featuring Active Directory
• Create
Users • Ezines • PowerShell
Add Computer •
LDAP Properties
• Free
CSV Importer
•
Ezine 23 enable accounts •
UserAccountControl Values •
Ezine 27 Move Computers •
Ezine 42 LDAP
• Ezine 44 CSVDE •
PB 55 CSVDE • Ezine
56 OU • Ezine 123 Ad Tree •
Ezine 124 Ad Tree •
IPAM
|