Azure Automation: update them modules redux

I did some work on updating the Update-AzureAHybridWorkerModules script I previously published here. It was a very basic script that only updated modules on hybrid workers that had been installed by using Install-Module.

After thinking a bit, and becoming ever increasingly frustrated that we still do not have logic for automatically syncing modules installed in AA down to hybrid workers. I sat down to see if I could expand on the thoughts I implemented in the first script.

One of the major hurdles to achieve module sync between AA and hybrid workers, is that I found no PS backed API to retrieve the module bits imported into AA. The best I could do was get some metadata on the modules, like name and version. Though nothing about where it came from originally. So I knew this would be a blocker, and I needed a workaround.

The best I could do was make some assumptions, and one of these was that I assumed that most of the modules would come from PSGallery. I therefore implemented logic that would search for the modules in AA using PSGallery. This meant I needed to fetch name and version of each module in AA and search PSGallery for them. The drawback here is that usually there are only 4 version of most modules in the repository. If the modules in AA have not been updated in a while (older than the 4 version found in PSGallery), there would be no way of getting the module down to the hybrid worker.

So this is the first limitation of the new update script. So to combat this I added logic so one can use other repositories than PSGallery. Best case scenario here would be to set up a private repository that all the hybrid workers can access and have all modules used in AA here. Then have the script use this as the source to search for modules to install on the hybrid worker. So I added the logic to do this in.

Though I found that a bit limiting, so I then added the ability to use multiple repositories. The chevat here is that it becomes complicated fast to handle modules that are found in multiple repositories. So in this initial release it will use the repository with the newest version. If both have the same version it will choose the repository registered first (or the first one in the list retrieved using Get-PSRepository).

To add a new repository just run the Runbook once and set ModuleRepositoryName and ModuleSourceLocation to the desired repository. This will add it as a trusted repo to all hybrid workers.

Then either keep ModuleRepositoryName set to the same repo name (the new one added) for the scheduled runbook to only use this new repo, or set AllRepositories to $true for it to search for modules in both PSGallery and this new one.

So basically the new script can do the same as before, namly update modules on hybrid workers installed using Install-Module to the latest version. In addition it can now look at the modules installed in AA and try to find them in the registered trusted repos on hybrid workers and if it finds the same version, install this one. Though if it does not find the same version it will do nothing. To give some options I added the ability to install the newest version found in the repo instead by setting UpdateToNewestModule to $true.

After thinking some more I added the ability to only do one of the two main scenarios by manipulating UpdateOnly or SyncOnly. In case one does not want modules to always be updated to the latest all the time, or only process the modules found in AA. Though note setting both to true will make the script do nothing.

That’s a short intro to the new stuff. Take a look at the script for more information.

I will post two versions, one using AzureRM and one using Az instead. Though note Az will not be fully supported in AA before Q2 next year. But you can still use it on hybrid workers if AzureRM is removed . Though it is best to wait until it is fully supported in AA.

Update-AAHybridWorkerModules:

Update-AAHybridWorkerModulesAz:

Leave a Comment

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