This blog describes how to onboard multiple VM’s to Azure Automation.
From an administration computer: First login to both ASM and ARM
Add-AzureAccount Login-AzureRmAccount
Here is the function that installs the Azure Automation DSC extension on the VM. The script originated from this site,
https://azure.microsoft.com/sv-se/documentation/articles/automation-dsc-onboarding/
To be able to onboard a huge amount of vm’s we need to make some kind of loop and that’s what I have done.
function sVm { param( $VMName, $ServiceName, $AutomationAccountName, $AutomationAccountResourceGroup ) # fill in the name of a Node Configuration in Azure Automation DSC, for this VM to conform to $NodeConfigName = '' # get Azure Automation DSC registration info $Account = Get-AzureRmAutomationAccount -Name $AutomationAccountName -ResourceGroupName $AutomationAccountResourceGroup $RegistrationInfo = $Account | Get-AzureRmAutomationRegistrationInfo # use the DSC extension to onboard the VM for management with Azure Automation DSC $VM = Get-AzureVM -Name $VMName -ServiceName $ServiceName $PublicConfiguration = ConvertTo-Json -Depth 8 @{ SasToken = '' ModulesUrl = "https://eus2oaasibizamarketprod1.blob.core.windows.net/automationdscpreview/RegistrationMetaConfigV2.zip" ConfigurationFunction = "RegistrationMetaConfigV2.ps1RegistrationMetaConfigV2" # update these DSC agent Local Configuration Manager defaults if they do not match your use case. Properties = @{ RegistrationKey = @{ UserName = 'notused' Password = 'PrivateSettingsRef:RegistrationKey' } RegistrationUrl = $RegistrationInfo.Endpoint NodeConfigurationName = $NodeConfigName ConfigurationMode = "ApplyAndMonitor" ConfigurationModeFrequencyMins = 15 RefreshFrequencyMins = 30 RebootNodeIfNeeded = $False ActionAfterReboot = "ContinueConfiguration" AllowModuleOverwrite = $False } } $PrivateConfiguration = ConvertTo-Json -Depth 8 @{ Items = @{ RegistrationKey = $RegistrationInfo.PrimaryKey } } $VM = Set-AzureVMExtension -VM $vm -Publisher Microsoft.Powershell -ExtensionName DSC -Version 2.6 -PublicConfiguration $PublicConfiguration -PrivateConfiguration $PrivateConfiguration -ForceUpdate $VM | Update-AzureVM }
Finally, we create a variable with the VM’s and then loop through them.
$allvm=get-azurevm $allvm.ForEach({ sVm -VMName ($PSItem.Name) -ServiceName ($PSItem.ServiceName) -AutomationAccountName "[NameOfYourAutomationAccount]" -AutomationAccountResourceGroup "NameOfAutomationAccountResourceGroup" })