Unable to domain join new virtual machines (Resource Manager)
I currently have a function that deploys a new VM in Azure that will automatically join the VM to our AD (not AAD). I use the cmdlet Add-AzureProvisioningConfig for this.
I'm now trying to recreate that function in the Resource Manager model but am unable to domain-join the server. Is something on the roadmap. And if so, when is it to be expected? Or is it available but I just can't find it?
I hoped to do this with Add-AzureRmVMAdditionalUnattendContent but according to the manual this isn't possible (wanted to use the JoinDomain keyword).
Hi Vincent, In ARM , you need to use DSC extension with the xComputerManagement module Here is the template https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-domain-join
Alex
@VincentK76 Domain join capability is now available as an extension in ARM VMs. The extension specific cmdlets aren't available yet but you can use the generic extension cmdlet. Try this
Set-AzureRMVMExtension –ResourceGroupName “rg1” -Name "JsonADDomainExtension" -Publisher "Microsoft.Compute" -TypeHandlerVersion "1.0" -Settings '{ "Name" : "workgroup1", "User" : "domain\test", "Restart" : "false", "Options" : 1}' -VMName “testvm” -ProtectedSettings '{"password": "pass"}'
The value for joining the domain I believe is 1, see here https://msdn.microsoft.com/en-us/library/windows/desktop/aa370433(v=vs.85).aspx
@singhkay Thank you for the update. I will try this. I already started working on the DSC configuration but this will be a nice, quick, alternative.
@VincentK76 Sounds good, I'll close this issue in the meantime. Feel free to reopen if you have any update.
@VincentK76 - any update on your progress? I'm working on the same thing.
@wbrophy There was a bug in the 1.0.1 Azure RM cmdlets that prevented me from further testing this solution. The bug was a problem with Register-AzureRmAutomationDscNode that tried to install a old WMF 5.0 version. This bug is solved in 1.0.2.
I do however tend to NOT use DSC at the moment. I find the solution very hard to use and documented badly. It is a real niche solution and I really need to ask myself to question if this really is something that we want to introduce into our IT services. Knowledge for PowerShell is already scarce and this will only add to the confusion. I will try the solution posted by @singhkay since domain join is really the only thing that I want to do at this moment.
@singhkay I tried your command and after some adjustment here and there I got a working command. My command is (added variables and backticks for readability)
$ResourceGroupName = "<RG Name>"
$VMName = "<VM Name>"
$DomainName = "<Domain Name>"
$Location = "West Europe"
$DomainJoinAdminName = "<Domain Admin>"
$DomainJoinPassword = "<Password>"
Set-AzureRMVMExtension `
-VMName $VMName `
–ResourceGroupName $ResourceGroupName `
-Name "JoinAD" `
-ExtensionType "JsonADDomainExtension" `
-Publisher "Microsoft.Compute" `
-TypeHandlerVersion "1.0" `
-Location $Location `
-Settings @{ "Name" = $DomainName; "User" = $DomainJoinAdminName; "Restart" = "true"; "Options" = 1} `
-ProtectedSettings @{"password" = $DomainJoinPassword}
This command however results in a rather strange error message;
Status : Failed
StatusCode : OK
RequestId : 813ea2e6-178d-4986-b33d-c00a417b7682
Output :
Error : {
"Details": [],
"InnerError": null,
"Code": "VMExtensionProvisioningError",
"Message": "VM has reported a failure when processing extension 'JoinAD'.
Error message: \"Join completed for Domain '<Domain Name>'\".",
"Target": null
}
StartTime : 12/21/2015 12:47:54 PM +01:00
EndTime : 12/21/2015 12:49:08 PM +01:00
TrackingOperationId : 5ae9ee58-7f47-43ba-b531-803358f4c20b
I removed all real world values. Please mind the error messages that says that the join is complete even though the command fails. The latter is true, the virtual machine is not joined to the domain although the extension is added to the virtual machine.
Do you happen to have a solution?
I end up with the same error @VincentK76 receives.
I have been having the same problem and I finally got it working. This is the only approach that has worked for me so far. While I continue to look for better approaches here is how you can get it working. In case anybody comes up with a better approach please do share.
Step 1. Create a Join2Domain.ps1 file with the following info.
$OSType = (Get-WmiObject -Class Win32_OperatingSystem).Caption $DomainName = "domain.com" $DomainUsername = "domain.com\adminaccount" $DomainPassword = "p@55w0rd" $Credential = New-Object System.Management.Automation.PSCredential ($DomainUsername, (ConvertTo-SecureString $DomainPassword -AsPlainText -Force))
If($OSType -like '2008')
{ Add-Computer -DomainName $DomainName -Credential $Credential Restart-Computer }
If($OSType -like '2012') { Add-Computer -DomainName $DomainName -Credential $Credential -Restart }
Step 2: Upload the above Join2Domain.ps1 script to your azure storage account. Below is the code for your ease:
$StorageAccountName = "storageaccountname" $StorageAccountKey = "jhaX2zXOnNCM5jSH3hYA3jIeFExKQr9WzBb0ggj6dRWd7Hj87jK/A==" $ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey $containername = "scripts" $localfiledirectory = "C:\Users\Username\Desktop\Scripts" $blobname = "Join2Domain.ps1" $localfile = $localfiledirectory + $blobname Set-AzureStorageBlobContent -File $localfile -Container $containername -Blob $blobname -Context $ctx
Step 3: Set AzureVM Custom Script Extension:
Define storage account where the Join2Domain.ps1 script is stored:
$StorageAccountName = 'storageaccountname' $ContainerName = 'scripts' $StorageKey = 'jjhaX2zXOnNCM5jSH3hYA3jIeFExKQr9WzBb0ggj6dRWd7Hj87jK/A=='
Identify the target VM:
$ResourceGroup = "resourcegroupofvm" $VMName = "vmname" $Location = "location of VM"
Set-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroup -VMName $VMName -Name "CustomScriptExtension" -TypeHandlerVersion "1.4" -StorageAccountName $StorageAccountName -ContainerName $ContainerName -StorageAccountKey $StorageKey -Location $Location -FileName "Join2Domain.ps1" -Run "Join2Domain.ps1"
Step 4: Remove Azure VM Extension
After the VM is joined to the domain you better remove the Azure extension using the command below. When you Set-AzureRmVMCustomScriptExtension the Join2Domain.ps1 gets downloaded to the VM that is being joined to the domain and that has got the credentials of the user account with permissions to join computer to domain in clear text. So obviously you would not want someone to lay their hands on it. When you remove the extension the package gets deleted along with the Join2Domain.ps1.
The PS1 when downloaded via the extension will be stored in C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.4\Downloads\Join2Domain.ps1
Remove-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroup -VMName $VMName -Name "CustomScriptExtension" -Force
@VincentK76 @Vijayaraghavanl @wbrophy So this environment took a bit of time to replicate and debug!! but I got it to work finally
I started with @VincentK76's script above and modified it a bit. Here's the final script.
The changes are
- Password property name is case sensitive and should be called
Passwordinstead ofpassword - You need option 3 which joins and creates the account if it doesn't exist
- OUPath is needed as well even if it's empty
- User needs to be specified in the format domain\user. This should also work
user@domain
$ResourceGroupName = "somerg"
$VMName = "somevm"
$DomainName = "contoso.local"
$Location = "northcentralus"
$DomainJoinAdminName = $DomainName + "\username"
$DomainJoinPassword = "password"
Set-AzureRMVMExtension `
-VMName $VMName `
–ResourceGroupName $ResourceGroupName `
-Name "JoinAD" `
-ExtensionType "JsonADDomainExtension" `
-Publisher "Microsoft.Compute" `
-TypeHandlerVersion "1.0" `
-Location $Location `
-Settings @{ "Name" = $DomainName; "OUPath" = ""; "User" = $DomainJoinAdminName; "Restart" = "true"; "Options" = 3} `
-ProtectedSettings @{"Password" = $DomainJoinPassword}
Is it also possible to implement the domain join using a vm powershell extension (non dsc)? So baking the code into the json template with an added customscript instead of running an extra powershell command (set-azureRMVMextension)
I have added 3 parameters to the json, domain / user / password, but don't know how to feed these 3 parameters as arguments for the powershell script to run as an extension.
Trying to do this in the following manner.
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -file, ', variables('AddDomainScriptFolder'), '/', variables('AddDomainScriptFileName'), '',parameters('domainname'), ' ' ,parameters('username'), '',parameters('password'))]"
I want to forward the parameters to the custom script which looks like this
$domainName = $args[0]
$username = $args[1]
$password = $args[2]
Set-DnsClient -InterfaceAlias "Ethernet*"
-ConnectionSpecificSuffix $domainName
$securePassword = ConvertTo-SecureString $password -AsPlainText
-Force
$cred = New-Object System.Management.Automation.PSCredential($username, $securePassword)
Add-Computer -DomainName $domainName -Credential $cred -Restart –Force
@D-DRMZ Domain join extension is now available. You can use it in a template like this
{
"name": "[concat(parameters('VirtualMachines'),'/extensions')]",
"type": "Microsoft.Compute/virtualMachines/extensions",
"location": "[resourceGroup().location]",
"apiVersion": "[parameters('apiVersion')]",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachines/', parameters('VirtualMachineName')]"
],
"properties": {
"publisher": "Microsoft.Compute",
"type": "JsonADDomainExtension",
"typeHandlerVersion": "1.0",
"settings": {
"Name": "domainName",
"OUPath": "OUPATH",
"User": "domain\username or username@domain",
"Restart": "true",
"Options" : 3
},
"protectedsettings": {
"Password": "user pass"
}
}
},
We'll be updating the domain join template over on the quickstarts repo to use this extension next week https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-domain-join
@singhkay Great, works perfectly!
@singhkay : I still can't get it to work: Below are details. Values below have been changed from real ones.
$ResourceGroupName = "rgtestvms" $VMName = "testvm" $DomainName = "mydomain.com" $Location = "East US" $DomainJoinAdminName = "mydomain.com\adminaccount" $DomainJoinPassword = "P@55w0rd!"
Set-AzureRMVMExtension -VMName $VMName –ResourceGroupName $ResourceGroupName -Name "JoinAD" -ExtensionType "JsonADDomainExtension" -Publisher "Microsoft.Compute" -TypeHandlerVersion "1.0" -Location $Location -Settings @{ "Name" = $DomainName; "OUPath" = ""; "User" = $DomainJoinAdminName; "Restart" = "true"; "Options" = 3} -ProtectedSettings @{"password" = $DomainJoinPassword}
Status : Failed StatusCode : OK RequestId : d079cd40-c539-4baf-9cae-5a617975d450 Output : Error : { "Details": [], "InnerError": null, "Code": "VMExtensionProvisioningError", "Message": "VM has reported a failure when processing extension 'JoinAD'. Error message: "Join completed for Domain 'mydomain.com'".", "Target": null } StartTime : 12/28/2015 1:27:38 PM +05:30 EndTime : 12/28/2015 1:28:03 PM +05:30 TrackingOperationId : ae256f0c-f631-4c9a-80b8-e900283b3e1a
Below is an error I noticed in my System Log:
Log Name: System
Source: NetJoin
Date: 12/28/2015 6:36:58 AM
Event ID: 4097
Task Category: None
Level: Error
Keywords:
User: SYSTEM
Computer: TestVM
Description:
The machine TestVM attempted to join the domain mydomain.com but failed. The error code was 110.
Are you able to help?
@Vijayaraghavanl Password property is case sensitive
FYI: The template is now updated to use the domain join extension https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-domain-join
@singhkay : :) Seriously. You got to be kidding me. It took me a while to understand that I had to say "Password" and not "password". That's a bug man!!
Nevertheless THAT WORKED with "Password" !!!!!!!!! :+1: :)
@Vijayaraghavanl Ya took me a few days to figure out all the bugs with that bad error message. I've filed a bug on the API team
Yea report it as a Bug with upper case "B" :) :) :)
@singhkay How does this work in the background? This worked for me creating the first VM. Next I sysprepped the domain joined system and used this VHD as a custom VHD with a new ARM template. This results in the new VM not being added to the domain. The workgroup is however changed. The extension gives me a status succeeded.
It seems that the old extension data on the C drive of the sysprepped image is somehow messing up the use of the extension in the second VM. Is this possible?
Update: used custom vhd which was not added to a domain before but this results in netjoin 4097 errors. (network and all that ok). So seems that domain join extension works a bit differently using a sysprepped image (??)