Scripting Azure Automation Module Imports Directly from MyGet or PowerShell Gallery
There are few ways to add PowerShell modules to Azure Automation accounts:
1. Via the Azure Portal by uploading the module zip file from local computer.
2. If the module is located in PowerShell Gallery, you can push it to your Automation Account directly from PowerShell Gallery.
3. Use PowerShell cmdlet New-AzureRmAutomationModule from the AzureRM.Automation module.
One of the limitation of using New-AzureRMAutomationModule cmdlet is, the module must be zipped and located somewhere online that Azure has access to. You will need to specify the location by using the –ContentLink parameter. In the past, in order to script the module deployment, even when the module is located in PowerShell Gallery, I had to save the module to a place where my Automation Account has access to (such as an Azure blob storage, or creating a release in a public Github repo).
Tonight, I was writing a script and I wanted to see if I can deploy modules to my Automation Account directly from a package repository of my choice – other than PowerShell Gallery, I also have a private MyGet feed that I use for storing my PowerShell modules.
It turned out to be really easy to do so, only took me few minutes to figure out how. I’ll use a module I wrote in the past called “SendEmail” as an example. It is published in both PowerShell Gallery, and my private MyGet feed.
Importing from PowerShell Gallery
the URL for this module in PowerShell Gallery is: https://www.powershellgallery.com/packages/SendEmail/1.3
The –ContentLink URI that we need to pass to the Add-AzureRmAutomationModule cmdlet would be:
https://www.powershellgallery.com/api/v2/package/SendEmail/1.3.
As you can see, all you need to do is to add “api/v2/” in the URI. The PowerShell command would be something like this:
New-AzureRmAutomationModule -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -Name 'SendEmail' -ContentLink 'https://www.powershellgallery.com/api/v2/package/SendEmail/1.3'
Importing from a private MyGet feed
For a private MyGet feed, you can access it by embedding the API key into the URL:
The URL for my module would be: “http://www.myget.org/F/<Your MyGet feed name>/auth/<MyGet API Key>/api/v2/package/<Module Name><Module Version>“
i.e. for my SendEmail module, the PowerShell command would be something like this:
$MyGetFeedName = 'SampleMyGetFeed'
$MyGetAPIKey = '89c2d7b8-2d76-4274-a5b7-bcb1f186502c'
$PackageURI = "https://www.myget.org/F/$MyGetFeedName/auth/$MyGetAPIKey/api/v2/package/SendEmail/1.3"
New-AzureRmAutomationModule -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -Name 'SendEmail' -ContentLink $PackageURI
Importing from a public MyGet feed
If the module is located in a public MyGet feed, then the API key is not required. the URI for the module would be very similar to PowerShell Gallery, you will just need to embed “api/v2/” in to the original URI:
‘https://www.myget.org/F/<MyGet Public Feed Name>api/v2/package/<Module Name><Module Version>‘
the PowerShell script would be something like this:
$MyGetPublicFeedName = 'SampleMyGetPublicFeed'
New-AzureRmAutomationModule -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -Name 'SendEmail' -ContentLink "https://www.myget.org/F/$MyGetPublicFeedName/api/v2/package/SendEmail/1.3"
Leave a comment