What I thought would be a simple task has proved daunting. I keep running into snags with each approach. I will start out with my first pass and gather any input. I am taking a workgroup-based PC, changing its name based upon location and serial number while preserving the old name in the computer description. I then want to add this computer into the domain using this new name. I would like to do this by running the script on the local workgroup PC (and have only one reboot). If I do this process manually, it works fine. The problem I have is that it adds the machine to the domain with the old name. If I remove the restart-computer, I see that the name of the computer is changed (with a note subject to a reboot) and the description field get populated properly. When I use the Add-Computer to join it to the domain, it does so using the old name and not the new name as desired. I have also upgraded the powershell on the source machine to version 4.0.
$site=Read-Host -Prompt "Enter 3-digit Site Code where this machine is located"
$cred=Get-Credential -Message "Enter UserID and Password with rights to join PC to domain.local"
#Get the Name of machine, whether it is joined to a domain
$ThisPC=GWMI -Class Win32_ComputerSystem|Select -Property *
$BIOSInfo=GWMI -Class Win32_BIOS
$NewName=$site+'-'+($BIOSInfo.SerialNumber)
$OldName=$env:Computername
#Check to make sure this machine is NOT already joined to a domain
If ($ThisPC.PartofDomain -eq $false) {
#Place the current computer name into the local description field for safe keeping
#but we want to insure this was not already done on the machine (avoid wiping away original name)
If ($NewName -ne $OldName) {
$OS=GWMI -Class Win32_OperatingSystem #May use $ThisPC variable but not much #speed saving
$OS.Description=$OldName
$OS.Put()
} #end if for local description
(GWMI -Class Win32_ComputerSystem).rename($NewName) #after this command runs, #the computer recognized the new name but subject to reboot
#the following command may require Powershell 3.0 as the newname, options, and server #parameters are not recognized in 2.0
Add-Computer -Credential $cred -DomainName domain.local -Force -NewName $NewName -Options JoinWithNewName -Server 172.16.50.20
#The below did not run as the ActiveDirectory Module is not included in their workstations
#we'll just need to find another way from a central location to place what is in the local description into AD
Set-ADComputer -Identity $NewName -Credential $cred -Description $OldName -Server 172.16.50.20
Restart-Computer
} #end if for checking domain membership
else {
Write-Host "this machine is already joined to a domain and this script should not be used. Migration will be via ADMT"
} #end else