I am attempting to use azure keyvault in a powershell script that is used by a github action. HEre is an example of my code, which worked at least once earlier today

$TenantID = my_tenant_id"$securePassword = "passowrd_for_key_vault_account" | ConvertTo-SecureString -AsPlainText -Force$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "my_application_id", $securePasswordConnect-AzAccount -ServicePrincipal -Credential $Credential -TenantID $TenantID

When I attempt to run this, I get the following error

The 'Connect-AzAccount' command was found in the module 'Az.Accounts', but the module could not be loaded. For more information, run 'Import-Module Az.Accounts'.

When I run Import-Module Az.Accounts I get

Import-Module: Assembly with same name is already loaded

I am installing & Importing the module earlier in my code with the following

 Install-Module -Name Az -scope currentuser -force -AllowClobberImport-Module Az

I tried to connect an azure service accountI was expecting it to log in and continue on to pull information from key vault

1

Best Answer


AZ module seems to keep breaking in powershell with a message about import-module:

This is usually the case if you have an AzureRM module installed in addition to the Az modules in PowerShell library. Even with the Az/Az.Accounts module installed, there may be issues with PowerShell if AzureRM was previously installed.

As a result, use this command to uninstall the AzureRM module.

uninstall-AzureRM

After executing it, I tried connecting Az account and it worked successfully.

$TenantID = "tenant_ID"$securePassword = "secret/password" | ConvertTo-SecureString -AsPlainText -Force$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "AppID", $securePasswordConnect-AzAccount -ServicePrincipal -Credential $Credential -TenantID $TenantID

Output:

enter image description here

Note:

  1. Make sure that update to the latest versions of the required modules when any new releases are there using command:
update-module
  1. To check the current running version use this command with the required module name:
 get-module Az.accounts

enter image description here

  1. Check the available modules list by using:
get-module -ListAvailable

enter image description here