Azure Automation: Refactor module update logic

As I had a good go creating logic to handle modules on hybriod workers, I decided to also update some existing code the Automation team has had on their github for awhile. This code has become stale and does not work that good anymore.

The team is also deprecating their github soon as it does not comply to their internal standards for Open Source projects. They have a separate project that handles module updates here , though in my opinion some of the logic decisions taken creating it limited its flexibility potential.

This is why I chose to fix Update-ModulesInAutomationToLatestVersion instead. This logic supported updating all modules in account as long as the module in question exists in PSGallery. You could also use it to import a module from gallery with it’s dependencies. Working on it it I decided to split these two functionalities into two separate runbooks. So instead of the one, there now are two: Import-PSGalleryModulesToAA.ps1 and Update-PSGalleryModulesInAA.ps1.

The update module now support to either just update the Azure modules (Az and AzureRM) or try to update all modules to latest version. At the moment there is no support for setting a desired import version, though if this becomes an issue it can always be added to the logic.

The import logic can be used to get the Az module and all of it’s dependencies into the Automation account initially. Though note this is a big module and will take more than 30 minutes to complete. Also note that as AA does not support having multiple version of the same module imported, the logic will chose to keep the highest version that dependencies have on a given module. There is also a recursion limit of how far down the dependencies tree the logic will go. This is hardcoded to 3. To import multiple modules at the same time follow the syntax as shown below.

Sadly I’m not able to contribute this back to the Automations team github, though if they open a new one up there might be a option to surface it there.

Hope more will find this useful to keep their modules under management.

Happy tinkering!

Leave a Comment

Your email address will not be published. Required fields are marked *