I have this code which works.
$computers = (get-ItemProperty hklm:\SOFTWARE\Microsoft\MPSD\OSD).OSDComputerName
$GetOU = (get-ItemProperty hklm:\SOFTWARE\Microsoft\MPSD\OSD).OSDDomainOUName
$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
foreach($computer in $computers){
$search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(cn=$computer)"
$result = $search.FindOne()
$computerToMove = [ADSI]$result.path
$computerToMove.psbase.Moveto([ADSI]"LDAP://$GetOU")}
This is run as part of a CM2012 OS deployment task sequence. It reads the values set in the registry and then moves the computer account to the OU specified.
How can I confirm it all worked? I want to add something like this.
If MOVE worked THEN
$text = "computer moved worked"
$text | Out-File 'c:\windows\MGH\logs\ADTasks.txt' -Append
ELSE "it did not work"
Do I have to add code that checks the new OU for the machine name? That makes sense but I was really wondering if the move command generates any sort of EXIT CODE that I can capture "Live" in the code without checking AD.