Update Modules on Azure Automation with AZ PowerShell

Hi All,

As you probably know, i am a big Fan of Azure Automation to run some Automation Scripts for M365 in Azure.

When you try to install a new Microsoft Graph Module in Azure Automation from the Gallery it will fail, due its dependency for Microsoft.Graph.Authentication, because an older Version is installed.


I've tried to figure out, how to update the Modules with PowerShell (too much clicking for doing it manually).
By the way it's worth mentioning, that you only see the PowerShell 5.1 Modules. Anyone knows how to display the PowerShell 7 Modules?

Connect-AzAccount
$AutomationAccount = Get-AzAutomationAccount -Name icewolfautomation -ResourceGroup RG_DEV
$AutomationModules[0]
$AutomationModules | where {$_.Name -match "Microsoft.Graph"}


Uninstall the Modules

$AutomationModules = Get-AzAutomationModule -AutomationAccountName icewolfautomation -ResourceGroup RG_DEV
$GraphModules = $AutomationModules | where {$_.Name -match "Microsoft.Graph"}
Foreach ($GraphModule in $GraphModules)
{
    $ModuleName = $GraphModule.Name
    Write-Host "Uninstalling $ModuleName"
    Remove-AzAutomationModule -AutomationAccountName icewolfautomation -ResourceGroup RG_DEV -Name $ModuleName -Confirm:$False -Force
}



Now the Microsoft.Graph Modules for PowerShell 5.1 are removed


Install the New Modules. I've still got the List in Memory, so i can install the Same modules but with a newer Verson.

$moduleVersion = "1.9.6"
Foreach ($GraphModule in $GraphModules)
{
    $ModuleName = $GraphModule.Name
    Write-Host "Installing $ModuleName"
    New-AzAutomationModule -AutomationAccountName icewolfautomation -ResourceGroup RG_DEV -Name $moduleName -ContentLinkUri "https://www.powershellgallery.com/api/v2/package/$moduleName/$moduleVersion"
}


After a while, you can see in the Portal that the Modules are beeing installed


But Modules other than Microsoft.Graph.Authentication fail, because they are dependent on that Module and it is not installed at that point in time


I'll run the same Script again. Now it shows "Importing never Version" (it's the same version as already installed - but hey)

$moduleVersion = "1.9.6"
Foreach ($GraphModule in $GraphModules)
{
    $ModuleName = $GraphModule.Name
    Write-Host "Installing $ModuleName"
    New-AzAutomationModule -AutomationAccountName icewolfautomation -ResourceGroup RG_DEV -Name $moduleName -ContentLinkUri "https://www.powershellgallery.com/api/v2/package/$moduleName/$moduleVersion"
}



Now it did work



Regards
Andres Bohren