How to import modules into Azure Automation Account using PowerShell?

爱⌒轻易说出口 提交于 2021-01-27 17:55:35

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/60847861/how-to-import-modules-into-azure-automation-account-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!