2
votes

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

2
This 'It seems that MS has been kicking PowerShell to the kerb for a while now' is not really a valid statement. PowerShell is huge and will continue to be. As for installing modules. Module installation and use are per machine or per local user profile. Windows PowerShell modules default to $env:USERPROFILE\Documents\WindowsPowerShell\Modules, and Powershell v7 (PowerShell Core) get loaded here: $env:USERPROFILE\Documents\PowerShell\Modules. You can load to the AllUsers profile as well and that location is prepopulated with all the default modules already. See the info on $PSModulePath.postanote
Thank you for your comment, but I am asking about Azure Automation, not PowerShell on my local machine.Lars Panzerbjrn

2 Answers

2
votes

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.

0
votes

As per the docs for the module, does your name $AzMod.Name, resolve to the expected file type.

New-AzAutomationModule

Example 1: Import a module

Windows PowerShell module, which has a .psm1 or .dll file name extension

Windows PowerShell module manifest, which has a .psd1 file name extension The name of the .zip file, the name of the folder, and the name of the file in the folder must be the same. Specify the .zip file as a URL that the Automation service can access. If you import a Windows PowerShell module into Automation by using this cmdlet or the Set-AzAutomationModule cmdlet, the operation is asynchronous. The command finishes whether the import succeeds or fails. To check whether it succeeded, run the following command: PS C:\> $ModuleInstance = Get-AzAutomationModule -NameModuleName Check the ProvisioningState property for a value of Succeeded.