How to move a computer to a new OU as a step in SCCM Task Sequence?
Requirements:
As this is a Task Sequence step, the command will be executed locally on the SCCM Client so using the Active-Directory module is not an option unless the module is installed on every system that could be affected by this task.
Command-line execution defaults to CMD.exe, forcing to deal with painful string escape steps.
Looking around online I found a blog post that details SCCM allows for a step in Task Sequences called "Run Command Line". However that blog post solution is not a stand-alone command line execution.
Additionally, I found a blog post with snippits for using ADSI instead of the Active-Directory module when attempting a Powershell solution to move the local host (any non-Domain Controller) to a new OU.
Combining those procedures into a single command line execution, it took some time, but i was able to work around combining the CMD.exe string parsing/escaping and the Powershell string parsing/escaping into a single command line execution that will work within a SCCM task sequence.
Command line:
C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -command iex \" `$TargetOU=\"\"OU=TargetOUName,DC=ConglomiCo,DC=com\"\"; `$SysInfo=New-Object -ComObject \"\"ADSystemInfo\"\";`$ComputerDN=`$SysInfo.GetType().InvokeMember(\"\"ComputerName\"\",\"\"GetProperty\"\",`$Null,`$SysInfo,`$Null);`$Computer=[ADSI]\"\"LDAP://`$ComputerDN\"\";`$OU=[ADSI]\"\"LDAP://`$TargetOU\"\";`$Computer.psbase.MoveTo(`$OU);" \"
Note: Be sure to specify a domain account in the "Run Command Line" task that has the proper permissions:
This solution contains the destination OU within the Command Line entry, however it is easily modifiable to read a registry entry, text file on the file system, etc which could contain the destination OU DN.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.