1
votes

I am trying to rename a machine and add it to the domain with 1 restart from WORKGROUP (after the machine gets renamed and joined domain). I tried the code below but it's giving me an error:

$bios = (Get-WmiObject Win32_Bios).SerialNumber
$name = $bios
Rename-Computer -NewName "$name"
$domain = "DOMAINNAME"
$username = "USERNAME"
$password = "PASSWORD" | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential -NewName $name
Read-Host "The computer will restart in 5..."
shutdown /r /t 5

The error I'm receiving is the account already exist. It does join the domain but not the "Renamed" name that I want.

1
doesn't Rename-Computer usually require a restart to make the name change? this -Restart [<SwitchParameter>] Indicates that this cmdlet restarts the computer that was renamed. A restart is often required to make the change effective. seems to indicate that you should add a -Restart to the parameter list.Lee_Dailey
It should work without restart. I can rename and join the machine using system properties manually without restart. The only restart I do is when the machine joins the domain.Duy Gonzales
thank you for the feedback! still, ReadingTheFriendlyManual indicates it is "sometimes needed" ... have you tried it?Lee_Dailey
I did try it. It joins domain but the name changed didn't work. It joined the domain using the default name, disregarding the "renamed" name.Duy Gonzales
i am beyond my depth now. i will go back to lurking & hope someone knowledgeable shows up with an answer. good luck! [grin]Lee_Dailey

1 Answers

2
votes

This is how it has to be done.

Rename-Computer -NewName newserver -Force
Add-Computer -DomainName example.ne -Credential $credential -NewName newserver -Options JoinWithNewName

You could better read the full documentation of Add-Computer cmdelt. Get-Help Add-Computer -Online