I am trying to figure out how to install modules into an Azure automation Account from the PowerShell gallery, and I am struggling…
New-AzAutomationModule should have been able to do this, but it constantly gives a status of “Failed” in the list of modules in Azure Automation. The command I used is this:
$AzMods = Find-Module az.*
ForEach ($AzMod in $AZMods)
{
New-AzAutomationModule -AutomationAccountName $AAccName -Name $AzMod.Name -ContentLinkUri "$($AZMod.RepositorySourceLocation)package/$($AzMod.Name)/$($AzMod.Version)" -ResourceGroupName $RGName -Verbose -ErrorAction Continue
Sleep 5
}
I also tried without the version part of the url, but that didn’t change anything. Does anyone know how to do this successfully? The few posts I have found are sadly out of date and no longer relevant.
I can do it by going to the gallery and click the button to deploy to Azure Automation, so I know that it can be done, but it is hardly optimal to have to do this manually.
It seems that MS has been kicking PowerShell to the kerb for a while now, but it baffles me that installing modules from the gallery isn’t even listed in the Get-Help section
I can reproduce your issue, because the most of the modules have their dependencies, if its dependency has not been installed, it will fail.
You could check their dependencies via the command below.
$AzMods = Find-Module Az.*
ForEach ($AzMod in $AZMods)
{
$AzMod.Dependencies.Name
}
From the command above, you can find the dependencies are Az.Accounts
, Az.Profile
, Az.Blueprint
, and Az.Accounts
is also the dependency of Az.Blueprint
, so to fix the issue, we could install the Az.Accounts
, Az.Profile
first, then install Az.Blueprint
, at last, install the other modules.
Here is a sample for you to refer, in my sample, I just use Sleep
, in your production environment, you can also use some judgment statements, check if the ProvisioningState
is Succeeded
via Get-AzAutomationModule
, it depends on your requirements.
$AAccName = "<automation-account-name>"
$RGName = "<group-name>"
$deps1 = @("Az.Accounts","Az.Profile")
$deps2 = "Az.Blueprint"
foreach($dep in $deps1){
$module = Find-Module -Name $dep
$link = $module.RepositorySourceLocation + "/package/" + $module.Name + "/" + $module.Version
New-AzAutomationModule -AutomationAccountName $AAccName -Name $module.Name -ContentLinkUri $link -ResourceGroupName $RGName
}
Sleep 300
$module = Find-Module -Name $deps2
$link = $module.RepositorySourceLocation + "/package/" + $module.Name + "/" + $module.Version
New-AzAutomationModule -AutomationAccountName $AAccName -Name $module.Name -ContentLinkUri $link -ResourceGroupName $RGName
Sleep 200
$AzMods = Find-Module -Name Az.*
ForEach ($AzMod in $AZMods){
if($AzMod.Name -ne 'Az.Accounts' -and $AzMod.Name -ne 'Az.Profile' -and $AzMod.Name -ne 'Az.Blueprint'){
$link = $AzMod.RepositorySourceLocation + "/package/" + $AzMod.Name + "/" + $AzMod.Version
New-AzAutomationModule -AutomationAccountName $AAccName -Name $AzMod.Name -ContentLinkUri $link -ResourceGroupName $RGName
Sleep 10
}
}
Besides, I notice the Az.DevOps.Blueprint
module still fail, not sure why, even if I import it in the portal, it also fail, maybe relate to the module itself.