Configure Az Module when running Azure Script

Paul,

This is what I have observed so far:

  • Using Execution Container
    I have tried to use octopusdeploy/worker-tools:3.0-windows.ltsc2019 container from Docker. The interesting thing is that if my script is simply like

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
Get-AzContext

It can be run successfully but if the Get-AzContext is change to Get-AzSubscription, I got an error

InvalidOperation: Run Connect-AzAccount to login.

  • Add install-module in the script
    First of all, I do not think “adding the Az install commands to a standard script step that runs before the Azure step.” will work since I am using dynamic worker unless there is a workaround which can allow me to use the same worker for two different steps. Anyway, I did come across this Azure PowerShell ‘Az’ Module version 1.0 - #4 by Iain_Brown and one of the change is that I have to create a “Run a Script” step instead of “Run Azure Script”. With this change I am able to successfully run the script with Az Powershell module. One concern is that occasionally the run book will fail with an error:

ObjectNotFound: The term ‘Get-AzEnvironment’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
July 9th 2021 20:02:28Error
At C:\Octopus\Tentacle\Work\20210710005326-4581-6\Script.ps1:8 char:10
July 9th 2021 20:02:28Error

  • $azEnv = Get-AzEnvironment -Name $azEnv

The Get-AzEnvironment is the first Az cmdlet to be used in the script. However, the subsequent run will be successful. I am using the default worker pool. I might try to limit the runbook to Win2019 worker pool. Any comments?