Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBScript to Move Computer OUs

159 views
Skip to first unread message

Trevor.March

unread,
Feb 25, 2011, 9:37:42 AM2/25/11
to
I am new to scripting and am trying to create a VBScript that will move machines in a particular OU group in AD ('Computers') to an 'Unmanagaed' OU.

Please help!!!

' MoveComputer.vbs
' VBScript to Move computers from the Computers OU to the Unmanaged OU
' Author Trevor March
' -----------------------------------------------------------------'

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 in the Computers OU
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,dc=gfoundries,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))

' Filter Computers.
If strInitial = "FC8" Then

strComputer = objRecordSet.Fields("Name").Value
strComputer = "CN="& strComputer

intCounter = intCounter +1
Wscript.Echo "Computer Name: " & strComputer

' Domain info
strSourceOU = ",OU=Computers,DC=gfoundries,DC=com"
strDestinationOU = "OU=Unmanaged,DC=gfoundries,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 VBScript

conner.d...@gmail.com

unread,
Feb 5, 2013, 2:31:27 PM2/5/13
to
Solution is below..

strSourceOU = "CN=Computers,DC=domain,DC=com"
strTargetLaptopOU = "OU=Laptops,OU=Workstations,DC=domain,DC=com"
strTargetDesktopOU = "OU=Desktops,OU=Workstations,DC=domain,DC=com"

Set objSourceOU = GetObject("LDAP://" & strSourceOU)
Set objTargetLaptopOU = GetObject("LDAP://" & strTargetLaptopOU)
Set objTargetDesktopOU = GetObject("LDAP://" & strTargetDesktopOU)

strFile = "computers.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objInput = objFSO.OpenTextFile(strFile, ForReading, False)
While Not objInput.AtEndOfStream
strLine = Trim(objInput.ReadLine)
If strLine <> "" Then
strComputer = "LDAP://CN=" & strLine & "," & strSourceOU
On Error Resume Next
If Left(UCase(strLine, 3) = "LAP") Then
objTargetLaptopOU.MoveHere strComputer, vbNullString
ElseIf Left(UCase(strLine, 3) = "DES") Then
objTargetDesktopOU.MoveHere strComputer, vbNullString
Else
WScript.Echo "Unable to determine target OU for " & strLine
End If
If Err.Number = 0 Then
WScript.Echo strComputer & " successfully moved."
Else
WScript.Echo strComputer & " failed to move. Error " & Err.Number & ": " & Err.Description
End If
Err.Clear
On Error GoTo 0
End If
Wend
objInput.Close

WScript.Echo "Done"

Todd Vargo

unread,
Feb 6, 2013, 9:52:23 AM2/6/13
to
On 2/5/2013 2:31 PM, conner.d...@gmail.com wrote:
> Solution is below..
<snip>

Note, the date of original post was Feb 25, 2011

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
0 new messages