Tuesday 29 January 2013

Join Domain and Rename Computer with PowerShell

   Below is a script that we use to join computers to a domain.  We use this as part of a task sequence in SCCM and feed a system name into the launching of the script.  We do not restart the computer using the script though - otherwise the task sequence would fail. 

Tested with Windows 7
Type: PowerShell 3.0 << Powershell 3.0 found here:
 http://www.microsoft.com/en-us/download/details.aspx?id=34595

# Script Usage:

# <ScriptName.ps1 MyNewComputerName>

param

([Parameter(Position=0,mandatory=
$true)]

[string]$newname) # Required Parameter, script will fail without it 

# === Set the Variables ===

$domain = "Domain"          # NETBIOS name of your domain
$DNSDomain = 'domain.local' # FQDN of your domain
$domainUserroot
= 'ADUser'  # An AD user with permissions to join workstations to the domain
$domainpass = 'supersecret' # The password for the above user
$domainUser = $domain+'\'+$domainUserroot $securePass = ConvertTo-SecureString –String $domainpass -AsPlainText –force $domaincred = New-Object System.Management.Automation.PSCredential` $domainUser,$securePass
$OU = "OU=Computers-Win7,DC=domain,DC=local" # Target OU
# === Start of Commands ===
# Add the computer to the domain
Add-Computer -DomainName $dnsdomain -Credential $domaincred -OUPath $OU 

# Rename the Computer - Requires Domain Credentials
Rename-Computer -NewName $newname -DomainCredential $domaincred # Rename Computer

Restart-Computer # Reboot the Computer
A note about the password:  Due to the nature of PSH, passing credentials is rather difficult, especially if you want to encrypt them.  I found it easier to create an AD user and lock that user account down so that it is allowed to join workstations to the specific container, rather than the former.
 

No comments:

Post a Comment