Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename computer and join to domain in one step with PowerShell

Goal: On a computer running Windows Server 2008 R2, use PowerShell 2.0 to:

  1. Rename the computer
  2. Join the computer to a domain

Condition: Steps 1 and 2 must be performed together, i.e., without a computer restart between them

Functions I'm Using

These are the PowerShell functions I've created for each step.

Rename Computer

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         }     } } 

Join Computer to Domain

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         }     } } 

Steps I've Tried

Attempt 1

  1. Call Rename-Computer
  2. Call Join-ComputerToDomain
  3. Restart

Result: Output from Rename-Computer indicates that name was changed, but after restart, name did not change, but computer was joined to domain

Attempt 2

  1. Call Join-ComputerToDomain
  2. Call Rename-Computer
  3. Restart

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.

Attempt 3

  1. Call Rename-Computer
  2. Restart
  3. Call Join-ComputerToDomain
  4. Restart

Result: Everything works as expected, but extra restart required. Works but I want to eliminate the restart at step 2.

like image 779
brett rogers Avatar asked Jun 02 '11 17:06

brett rogers


People also ask

How do I change computer domain name and join PowerShell?

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 .

Can I change the computer name and join to domain at the same time?

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.

Can I rename a PC that is joined to a 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.


2 Answers

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

like image 160
Francois-Xavier Cat Avatar answered Sep 21 '22 01:09

Francois-Xavier Cat


This solution is working:

  • Enter the computer in the Active Directory domain with authentication (no Restart)
  • Rename the computer with authentication (no Restart)
  • after, Restart

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) 
like image 34
Laurent Kling Avatar answered Sep 18 '22 01:09

Laurent Kling