VBScript: Move computer object to another OU via Command line parameter
Downloaded 378 times
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: http://blog.coretech.dk/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. http://blog.coretech.dk
' // 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
'//----------------------------------------------------------------------------

Subscribe to Jakob's RSS Feed
[...] [...]
Thanks, very useful !.
Thanks so much for posting this! The in-built task sequence steps for joining to domain and specifying OU don’t move to OU if the computer object already exists in AD and is already in an OU. A bit useless! I’ve incorporated this script into my task sequence and it works great. Cheers!
[...] solution is using this great VBSscript provided by Coretech.dk you can find it here:http://blog.coretech.dk/scripting/vbscript-move-computer-object-to-another-ou-via-command-line-param... The script will move the computer if it is not already in the specified OU, i was thinking of [...]
Great script! Thanks for sharing!
I wrote a post about how I use it in a Task Seqeunce. http://ccmexec.com/?p=686
Again, thanks!
Regards,
Jörgen
Hi! I’m sure this is a great script but I cant’t manage to get it to run in my TS. If I run it manually from the computer it works fine. I also tried to copy it in the TS to the computer and run with command line, no luck. Any suggestions?
Hello Erik
Since you have not put any error message / log output in your comment, i have no chance of knowing that problem you have
i suggest to look at this article about running commands with variables in TS
http://blog.coretech.dk/mip/using-ts-variables-when-running-a-script-under-a-different-account/
and also the above guide in Jörgen’s comment
http://ccmexec.com/?p=686
Hi Jakob
Script works by itself when running out of fully operational Win 7 OS, but when running from the task in a TS, it fails with this error “CScript Error: Can’t find script engine “VBScript” for script …
I am using the same method depicted at:http://ccmexec.com/2010/12/move-computer-to-the-correct-ou-during-deployment/
I should mention, I am using domain admin account to run the script out of task in my Windows 7 Image deployment Task Sequence ….not the computer system account. All my tasks executing VB Scripts run perfectly well in the same TS (while deploying the very same image) when running in the context of the system account
Interesting: when I tried to deploy one of my test Win 7 images using the same TS script worked.
Is my production Image broken?
Thanks
Hi, thank you for your reply. No error messages, I have put this as the last step after installing SW. Some lines from the SMSTS log (If this is to much, let me know
:
Variable name: _SMSTSRunCommandLineAsUser TSManager 24.06.2011 08:08:38 336 (0×0150)
Variable name: SMSTSRunCommandLineUserName TSManager 24.06.2011 08:08:38 336 (0×0150)
Variable name: SMSTSRunCommandLineUserPassword TSManager 24.06.2011 08:08:38 336 (0×0150)
Action command line: smsswd.exe /run:P010003C cscript.exe MoveOU.vbs “OU=Desktop,OU=Computers,OU=xxxxxxxx,dc=xxxxx,dc=no” TSManager 24.06.2011 08:08:38 336 (0×0150)
Hi again,
Finally got it working,:). Removed the dashes in the string and retyped them, must have been a copy error or a typo i guess. cscript.exe MoveOU.vbs “OU=Desktop,OU=Computers,OU=xxxxxxxx,dc=xxxxx,dc=no”
Thanks!
I am trying to test it now. But i do have a question. I have a staging OU, so i can run this script and move the client to staging OU “in case if client is already in AD” and then run the script as the very last step to move in to the perm OU?
When i run it manually i get no error msg but its not moving client to a new OU. When i try to run it from the TS the script is failing.
cscript.exe “%SCRIPTROOT%\MoveOU.vbs “OU=Windows7,OU=***,OU=***,OU=***,DC=***,DC=***,DC=com”
Thanks for sharing this, it was exactly what I needed to resolve issues with GPO doing unwanted things during the OSD process. Works great for us
MDT users: which part of the task sequence are you guys adding the vbs to?
This script runs great once I copy it to the local machine. The issue that I am having is that I am PXE booting a PC and it is pulling a MINIT… computername. How can I add a variable to use when searching AD instead of using the currenlty assigned computername?
I am fairly new to scripting, but hacking away everyday.
Thanks.
P.S.
To those who can not run it inside of a TS, I have resolved this by copying it to the local C:\, running it, and then deleted it from the C:\ in my TS.
@Ben
thnx for sharing the fix about the TS, before i got to reply.
It is always a good idea to copy script to local PC if you are having problems
about the computername
just change
strComputerDN = objSysInfo.ComputerName
to
strComputerDN = “CN=MyPC,DC=Contoso,DC=com”
unfortunately you will have to use the FQDN and not the NEtbios computername
if you want to search by name instead you can implement at script like this one
http://gallery.technet.microsoft.com/scriptcenter/0d865bfb-bb10-4147-86c2-f2b68a438de9
@Jakob
thnx for the quick reply. I think that I worded my question wrong. I am wanting a popup box to ask me for the desired new name of the PC. Then I want the script to move that PC to a specific OU. Then that same name should be passed to the UDI for the computer name portion. Below is what I have so far. It does pass the name to the UDI, but it does not move the PC to the correct OU. All of this is being run in a TS.
On Error Resume Next
‘Added variables
Dim OSDComputerName
‘Popup to get OSDComputername
strComputer = InputBox(“Enter Computer Name:”)
Set myTSEnv = CreateObject(“Microsoft.SMS.TSEnvironment”)
myTSEnv(“OSDComputerName”) = strComputer
‘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
strComputerDN = objSysInfo.OSDComputerName
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
hello ben
there are sevreal problems in your approach.
1. You cannot show a messagebox or any other interaction with the user from a TS. It is not possible, since the TS does not run in the same context as the user (it runs in administrive context / local_system). at least that is what the documentation says, so i always keep from it, but maybe it does work on some machines?
2. You are assigning a computername to myTSEnv(“OSDComputerName”) but using it as objSysInfo.OSDComputerName ?
3. what kind of computername are you typing in ? is it DistinguishedName as required?
- Jakob
I can’t seem to get this to work even from the command prompt. Using 0.0.3 version
Usage:
cscript.exe moveou.vbs “OU=XXXX,OU=XXX,OU=XX,DC=XXX,DC=XXX,DC=XXX”
used dsquery computer to verify LDAP ifo.
Any way to troubleshoot this with a log or something?
Thanks.
Robert
Hello
I’m trying to use this script in a sccm TS. This TS is migrating a machine from XP to win7 and is using the MDT integrated DB to change the computername. The new computerobject is than put into the correct OU in AD.
What I want to do is to use a script like this to move the old computerobject to a OU for migrated computers.
I have noticed that the old computername is stored in the SCCM variable _SMSTSMachineName during the complete TS. So I am thinking of running this like:
cssript.exe moveou.vbs “OU-….” %SMSTSMachineName%
My skills in vb scripting are limited, but so far I have come up with the following change to the script:
‘Use first argument as target OU
strMachineObjectOU = ArgObj(0)
strComputerDN = ArgObj(1)
So far this is not working for me. Any ideas
@Ben.
You could use a HTA to pass variables from HTA to the TS.
Heres a great post on it http://www.windows-noob.com/forums/index.php?/topic/3245-how-can-i-pass-values-from-the-hta-to-the-task-sequence/
i also use a script to ask for a computer if the computer name begins with “minint” great when you install a new computer.
This might not be the most beneficial place to inquire about this, but I am searching for a pest management company within the pittsburgh area and have no method to find out who’s the best. I uncovered this company that’s really close to me and wanted to see if any one has any evaluations on them. Spectrum Pest Control, 3058 West Liberty Avenue, Dormont, PA 15216 – (412) 446-2802
This may not be the best place to ask this, but I’m trying to find a pest management company inside the pittsburgh region and have no way to find out who is the best. I discovered this business that is actually close to me and wanted to see if any individual has any testimonials on them. Spectrum Pest Control, 3058 West Liberty Avenue, Dormont, PA 15216 – (412) 446-2802