Goal: On a computer running Windows Server 2008 R2, use PowerShell 2.0 to:
Condition: Steps 1 and 2 must be performed together, i.e., without a computer restart between them
These are the PowerShell functions I've created for each step.
According to my Internet research, PowerShell 2.0 at one point before release had a built-in cmdlet called Rename-Computer, but it was removed for reasons unknown in CTP 3. My version uses WMI.
function Rename-Computer { param ( [Parameter(Mandatory=$true)][string]$name ) process { try { $computer = Get-WmiObject -Class Win32_ComputerSystem $result = $computer.Rename($name) switch($result.ReturnValue) { 0 { Write-Host "Success" } 5 { Write-Error "You need administrative rights to execute this cmdlet" exit } default { Write-Host "Error - return value of " $result.ReturnValue exit } } } catch { Write-Host "Exception occurred in Rename-Computer " $Error } } }
As you can see, this function is really just a wrapper for the built-in cmdlet Add-Computer that gathers the domain name and creates some credentials to use.
function Join-ComputerToDomain { param ( [Parameter(Mandatory=$true)][string]$domain ) process { try { $_domainCredential = $Host.UI.PromptForCredential("Enter domain credentials", "Enter domain credentials to be used when joining computer to the domain", "", "NetBiosUserName") Add-Computer -DomainName $_domain -cred $_domainCredential } catch { Write-Error "Exception occurred in Join-ComputerToDomain " $Error } } }
Result: Output from Rename-Computer indicates that name was changed, but after restart, name did not change, but computer was joined to domain
Result: Return value from Rename-Computer is 1326 (Logon failure: unknown user name or bad password). I assume this is because domain credentials are required for the rename once it's joined to the domain. I attempted to use credentials with the Get-WmiObject call in Rename-Computer, but it threw an error about not being able to use different credentials on the local system.
Result: Everything works as expected, but extra restart required. Works but I want to eliminate the restart at step 2.
You can use the -NewName parameter to rename the computer name and join the domain. It sets a new name for the computer in the new domain. This command adds the computer DelftPC to the domain delftstack and changes the computer name to DelftPC01 .
This documentation indicates that you can modify the computer name and domain at the same time (and doesn't caution against it). I suspect you're dealing with someone who has the same superstition that I've had over the years: Change the computer name, reboot, then join the domain.
To rename a joined computer, you must: Leave the domain. Rename the computer using the domain join command-line interface. Rejoin the computer to the domain.
You can just use Add-Computer, there is a parameter for "-NewName"
Example: Add-Computer -DomainName MYLAB.Local -ComputerName TARGETCOMPUTER -newname NewTARGETCOMPUTER
You might want to check also the parameter "-OPTIONS"
http://technet.microsoft.com/en-us/library/hh849798.aspx
This solution is working:
In code:
# get the credential $cred = get-credential # enter the computer in the right place Add-Computer -DomainName EPFL -Credential $cred -OUPath "...,DC=epfl,DC=ch" # rename the computer with credential (because we are in the domain) $Computer = Get-WmiObject Win32_ComputerSystem $r = $Computer.Rename("NewComputerName", $cred.GetNetworkCredential().Password, $cred.Username)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With