How do you use the 'Az' PowerShell module with Octopus?

Hello,

I’m trying to do something with Azure that unfortunately needs to be done through the new-ish Az PowerShell, as opposed to AzureRm PowerShell. Sadly though, I can’t seem to get it to work on our self-hosted Octopus server :frowning:

After finding this support topic, I’ve installed the Az module on the Octopus server as an administrator, and I can import and use the module when running PowerShell directly on the server.

Install-Module -Name Az -AllowClobber
Import-Module Az

### Test the 'Az' PowerShell module
$storageAccount = Get-AzStorageAccount -ResourceGroupName "my-resource-group" -AccountName "mystorageaccount"

However, when I try to use the Az module in a Octopus step (Run a Script), I get an error -

Import-Module : The specified module ‘Az’ was not loaded because no valid module file was found in any module directory.

How can I use the Az PowerShell module in Octopus, without breaking AzureRm?

Failing Step

$accountClientId = $OctopusParameters["Az.Account.Client"]
$accountClientPassword = $OctopusParameters["Az.Account.Password"]
$accountTenantId = $OctopusParameters["Az.Account.TenantId"]
$accountSubscriptionId = $OctopusParameters["Az.Account.SubscriptionNumber"]
$resourceGroupName = $OctopusParameters["Az.StorageAccount.ResourceGroup"]
$storageAccount = $OctopusParameters["Az.StorageAccount.Name"]
$indexDocument = $OctopusParameters["Az.StorageAccount.StaticWebsite.IndexDocument"]
$404Document = $OctopusParameters["Az.StorageAccount.StaticWebsite.404Document"]

### Import 'Az' PowerShell.
Import-Module Az

### Connect to the relevant Azure account and set the subscription to use.
$securePassword = ConvertTo-SecureString $accountClientPassword -AsPlainText -Force
$pscredential = New-Object System.Management.Automation.PSCredential($accountClientId, $securePassword)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $accountTenantId
Set-AzContext -Subscription $accountSubscriptionId

### Get the Storage Account context.
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -AccountName $storageAccount
$context = $storageAccount.Context

### Enable the static website and set the index and error documents.
Enable-AzStorageStaticWebsite -Context $context -IndexDocument $indexDocument -ErrorDocument404Path $404Document

Thanks,
David

Happily the solution to this was pretty simple.

When running Install-Module -Name Az -AllowClobber, the modules were placed in the ‘Documents’ directory for the user that installed them; it seems that the default -Scope is CurrentUser for PowerShell versions >2.0.

The issue then was that the ‘Documents’ path for that user was not included in the PSModulePath environment variable when running a step in Octopus.

So now to use Az PowerShell -

  1. Move the modules somewhere easy to find
  2. Add the path to the modules to the PSModulePath environment variable.

And now I’m good to go…

$env:PSModulePath = "{0};{1}" -f $env:PSModulePath, "C:\AzPowerShell\Modules"
Import-Module Az
1 Like

Hi David,

Great job figuring this out, and thank you for posting such a detailed solution. I’m sure this will come in handy for others that run into this issue.

Any other problems, please don’t hesitate to get in touch.

Kind regards,
Paul