PowerShell How-To

Uploading a PowerShell Gallery Module Directly to Azure Automation

Azure Automation can be an excellent PowerShell script orchestration tool. But as-is, you only have a limited number of PowerShell modules you can use in that environment.

Luckily, though, we can upload and use our own modules, too. Not only that, but did you know we can also "transfer" them directly from the PowerShell Gallery to Azure Automation? No need to save it from the gallery and upload it manually. But first, ensure you have the AzureRm PowerShell modules installed by running Install-Module -Name AzureRm.

To demonstrate, let's pick any PowerShell module available inside of the PowerShell Gallery. Any module will do, but for this example, we'll pick the xActiveDirectory DSC module.

PS C:\> find-module -Name xActiveDirectory

Version    Name                                Repository           Description
-------    ----                                ----------           -----------
2.18.0.0   xActiveDirectory                    PSGallery            The xActiveDirectory module is originally part of the Windows PowerShell D...

To get a module from the gallery to Azure Automation, we first need to figure out the URI that the module lives at. To do that, we can get the base repository URI from our previous Find-Module call. This is the repo URI for all PowerShell modules.

PS C:\> $galleryRepoUri = (find-module -Name xActiveDirectory).RepositorySourceLocation
PS C:\> $galleryRepoUri
https://www.powershellgallery.com/api/v2/

Once we have that, we can then append the other piece of the URI specifically for the module we'd like to get into Azure Automation.

PS> $moduleUri = '{0}{1}' -f $galleryRepoUri, 'package/xActiveDirectory'

Once we've got the URI build for the module we'd like to upload to Azure Automation, we'll then need to run the New-AzureRmAutomationModule PowerShell cmdlet. This cmdlet requires knowing the automation account's resource group name and the name of the account itself. If you don't know this off of the top of your head, you can use Get-AzureRmAutomationAccount to figure it out.

PS C:\> Get-AzureRmAutomationAccount


SubscriptionId        : XXXXXXXXXXX
ResourceGroupName     : labtesting
AutomationAccountName : lab-test
Location              : eastus2
State                 :
Plan                  :
CreationTime          : 3/23/2018 8:54:24 PM -05:00
LastModifiedTime      : 3/25/2018 8:45:27 PM -05:00
LastModifiedBy        :
Tags                  : {}

Once you've got the automation account's name and resource group, simply plug those values in as parameters to New-AzureRmAutomationModule along with the URI we built earlier and the name of the module.

PS> New-AzureRmAutomationModule -ResourceGroupName 'labtesting' -AutomationAccountName 'lab-test' -Name xActiveDirectory -ContentLink $galleryRepoUri

ResourceGroupName     : labtesting
AutomationAccountName : lab-test
Name                  : xActiveDirectory
IsGlobal              : False
Version               :
SizeInBytes           : 0
ActivityCount         : 0
CreationTime          : 4/20/2018 11:54:31 AM -05:00
LastModifiedTime      : 4/20/2018 11:54:31 AM -05:00
ProvisioningState     : Creating

After this command is run, you can see that it begins creating. You can check on the status via PowerShell, or you can also see it in the GUI.

[Click on image for larger view.] Azure Automation module importing.

The module will be imported and should be complete in less than 60 seconds. At this point, you can use the module's cmdlet just as you would any other module in Azure Automation.

Inspiration for this post was provided by Karim Vies via his blog post "Azure Automation: Adding Modules via PowerShell."

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025