AD Domain Join your Virtual Machines and VM Scale Sets

03 Jul 2020

You can connect your Azure VMs and VMSS to your Active directory through PowerShell script. This allows you automate the domain join work after spinning up your Virtual Machines or else while Scaling Out you Scale Sets instances.

Following cmdlet expects you to provide a username and password that has permission to add the VM/Computer to your Active Directory domain service (AD DS).

[CmdletBinding()]
param (
  $DomainName,
  $DomainArmUserName,
  $DomainArmPass,
  $OUPath,
  $ResourceGroup,
  $ScaleSetObject
)

Write-Host  "Join the VMSS instances to $DomainName ...";

$domainJoinName = "vmssjoindomain"

# JoinOptions.NETSETUP_JOIN_DOMAIN | JoinOptions.NETSETUP_ACCT_CREATE
$Settings = @{
  "Name"    = $DomainName;
  "User"    = $DomainArmUserName;
  "Restart" = "true";
  "Options" = 3;
  "OUPath"  = $OUPath;
}

$ProtectedSettings = @{
  "Password" = $DomainArmPass
}

try {
  Remove-AzVmssExtension `
    -VirtualMachineScaleSet $ScaleSetObject `
    -Name $domainJoinName `
    -ErrorAction SilentlyContinue | Out-Null
}
catch {
  Write-Host "Remove existing domain join extension failed. Ignore if it is VMSS creation.";
  Write-Host "Error info: $_"
}

Add-AzVmssExtension `
  -VirtualMachineScaleSet $ScaleSetObject `
  -Publisher "Microsoft.Compute" `
  -Type "JsonADDomainExtension"  `
  -TypeHandlerVersion 1.3  `
  -Name $domainJoinName `
  -Setting $Settings `
  -ProtectedSetting $ProtectedSettings `
  -AutoUpgradeMinorVersion $true `
  -Verbose | Out-Null