I have a script to automate the server provisioning with VMWare on-premises. It`s tied to a WPF form and used Invoke-VMScript (VMWare PowerCLI) to handle the domain join function. You can check the post How to change IP and join a VM into domain by PowerCLI in VMware.
I don’t see many people used PowerShell to join a domain in Azure since ARM template seems to be a better way for it. I don’t use ARM as we have a custom GUI for the inputs.
Johan shared the article for Domain Join AzureRM VM’s with PowerShell, but it does not work on the new Az module with Set-AzVMExtension. I got the following error when running it…
Set-AzVMExtension : Multiple VMExtensions per handler not supported for OS type ‘Windows’. VMExtension ‘joindomain’ with handler ‘Microsoft.Compute.JsonADDomainExtension’ already
added or specified in input.
Then, I found a new cmdlet Set-AzVMADDomainExtension in the Az module. There is no examples on the help page, but I decide to give it a try. It works perfectly and is much easier. Here is how I did it.
- Run a PowerShell session
- Connect to your Azure subscription: Connect-AzAccount
- Run the following script
$DomainName = "your_domain_name" $VMName = "your_VM_Name" $credential = Get-Credential your_Domain_account $ResourceGroupName = "your_RG_name" Set-AzVMADDomainExtension -DomainName $DomainName -VMName $VMName -Credential $credential -ResourceGroupName $ResourceGroupName -JoinOption 0x00000001 -Restart -Verbose
The key is the -JoinOption parameter. You need to set the fJoinOptions to 0x00000001. If this value is not specified, it will join the computer to a workgroup.
You should see the following message when the server is joined to the domain successfully.