Hi All !
Azure Automation allows the installation of Modules published on the PS Gallery, or beeing uploaded from a ZIP File.
The ZIP-Upload can be automated with commands from AzureRM.Automation Module, but i was facing the following problem.
When i automatically deploy an Azure Automation Account, how can i install a PowerShell Module from the Gallery automatically ?
It turned out that all the www.powershellgallery.com stores its Modules as nuget packages on a Azure Storage Blog account. The next few lines will show you how to find the nuGet Package and install the module from there.
Before using the code, start a VS Code of ISE, or PowerShell Host, and login to your Azure Subscription with Add-AzureRmAccount
1.) Define your Azure Automation environment and the Module you want to install
1 2 3 |
$ResourceGroupName = 'PlayGround' $AutomationAccountName = 'MyAzureAutomationAccount' $ModuleName = 'psexcel' |
2.) Define the URL to parse if the module exists on the Gallery
1 |
$Url = "https://www.powershellgallery.com/api/v2/Search()?`$filter=IsLatestVersion&searchTerm=%27$ModuleName%27&targetFramework=%27%27&includePrerelease=false&`$skip=0&`$top=40" |
3.) Parse the Module Details
1 |
$Moduledetails = Invoke-RestMethod -Method Get -Uri $Url -UseBasicParsing |
4.) Get the Module Version
1 |
$moduleversion = ((($Moduledetails.id).Split('Version=')[-1]).Trimend(')')).Trim([Char]0x0027) |
5.) Contruct the module content URL
1 |
$ModuleContentUrl = "https://www.powershellgallery.com/api/v2/package/$ModuleName/$ModuleVersion" |
6.) Find the Blob Storage Account with the NuGet Package
1 |
$Bloburl = (Invoke-WebRequest -Uri $ModuleContentUrl -MaximumRedirection 0 -UseBasicParsing -ErrorAction Ignore).Headers.Location |
7.) Install the Module
1 2 3 4 5 |
New-AzureRmAutomationModule ` -ResourceGroupName $ResourceGroupName ` -AutomationAccountName $AutomationAccountName ` -Name $ModuleName ` -ContentLink $Bloburl |
The Script is attached to this blog and you can find it below: InstallPSModulesFromGalleryInAzureAutomation
Hope that helps – Good luck when testing !
Roman