[download id=”19″]

UPDATE: New improved script 0.0.3 uploaded. Thanks to Nico_ at Technet Forums!

Hello everyone

My collegue Michael Petersen, needed a script to move computers to another OU, after re-installing them via SCCM/ConfigMgr.

He have written a blog post to show how to use it in a Task Sequence: https://blog.ctglobalservices.com/mip/using-ts-variables-when-running-a-script-under-a-different-account/

therefore i developed this small script

All you needs to do is to run it with the CN for the new OU (without the LDAP://) in the commandline like this:

cscript.exe MoveOU.vbs “OU=HQ,dc=woodgrovebank,dc=com”

' //***************************************************************************
' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution:  ConfigMgr
' // File:      MoveComputerToCorrectOU.vbs
' // Author:	Jakob Gottlieb Svendsen, Coretech A/S. https://blog.ctglobalservices.com
' // Purpose:   Move computer to the correct OU that remains in variable MachineObjectOU
' //		Run inside TS after install
' //
' // Usage:     MoveComputerToCorrectOU.vbs
' //
' //
' // CORETECH A/S History:
' // 0.0.1	JGS 17/12/2009  Created initial version.
' // 0.0.2	MIP 17/03/2009 	Added feature to add argument to script
' // 0.0.3  JGS 02/12/2010  Changed to ADSystemInfo for the DN retrieval, instead of a homemade function.
' //						Thanks to Nico_ at Technet Forums
' //
' // Customer History:
' //
' // ***** End Header *****
' //***************************************************************************
'//----------------------------------------------------------------------------
'//  Main routines
'//----------------------------------------------------------------------------

On Error Resume Next

'Get MachineObjectOU Value
Set wshNetwork = CreateObject("WScript.Network")
Set oFso = CreateObject("Scripting.FileSystemObject")
Set objSysInfo = CreateObject( "ADSystemInfo" )
Set ArgObj = WScript.Arguments

'Use first argument as target OU
strMachineObjectOU = ArgObj(0)
strComputerDN = objSysInfo.ComputerName

nComma = InStr(strComputerDN,",")
strCurrentOU = Mid(strComputerDN,nComma+1)
strComputerName = Left(strComputerDN,nComma - 1)

'If current ou is different than target OU. Move object
If UCase(strCurrentOU) <> UCase(strMachineObjectOU) Then
	Set objNewOU = GetObject("LDAP://" & strMachineObjectOU)
	Set objMoveComputer = objNewOU.MoveHere("LDAP://" & strComputerDN, strComputerName)
End If 

'//----------------------------------------------------------------------------
'//  End Script
'//----------------------------------------------------------------------------