PowerShell Script to Join AD Domain

  • by

PowerShell Script to Join AD Domain

  1. Verify DNS Server is configured correctly
#Verify DNS Server
$DNSServer= "192.168.1.200" 

if ((Get-DnsClientServerAddress -InterfaceAlias "Ethernet0" -AddressFamily IPv4).ServerAddresses -eq $DNSServer) {

    Write-Host -ForegroundColor Green "DNS Server is" $DNSServer
}
else {

    Set-DnsClientServerAddress -InterfaceAlias "Ethernet0" -ServerAddresses $DNSServer

    }
  1. Prepare an AES Key file to encrypt the Password for Domain Administrator and store it on Share Folder.
$KeyFile = "\\UAT-AD01\MyShare\UAT-AES.key"
$PasswordFile = "\\UAT-AD01\MyShare\UAT-Password.txt"

$Key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file $KeyFile

(get-credential).Password | ConvertFrom-SecureString -key (get-content $KeyFile) | set-content $PasswordFile
  1. Enter Username & Password when prompted

PS-AESPassword-01

  1. Join the server to AD Domain and reboot it
$Password = Get-Content $PasswordFile | ConvertTo-SecureString -Key (Get-Content $KeyFile)
$UserName = "UAT\Administrator" 
$credential = New-Object System.Management.Automation.PsCredential($UserName,$Password)

$DomainName = "uat.aventislab.com"

Add-Computer -computername $env:computername -domainname uat.aventislab.com –credential $credential -Restart -Force

Reference link

https://www.altaro.com/msp-dojo/encrypt-password-powershell/