Powershell - command was found in the module, but the module could not be loaded

Viewed 125

I am using let's encrypt certificate and azure key vault to automate renewal process using this repo: https://github.com/brent-robinson/posh-acme-azure-example

I have installed the module Az.KeyVault using yaml on azure devops pipeline:

# Install the Az PowerShell modules our script will need
- task: PowerShell@2
  displayName: Install PowerShell Modules (Az.Accounts, Az.KeyVault, Az.Resources, Posh-ACME)
  inputs:
    targetType: 'inline'
    script: 'Install-Module Az.Accounts, Az.KeyVault, Az.Resources, Posh-ACME -Force'
    errorActionPreference: 'stop'
    failOnStderr: true
    pwsh: true

But, when I run the script, getting the below error:

The 'Get-AzKeyVaultCertificate' command was found in the
     | module 'Az.KeyVault', but the module could not be loaded. For
     | more information, run 'Import-Module Az.KeyVault'.

When I try to add the import module (Import-Module -Name Az.KeyVault -Force) it's giving the below error:

Assembly with the same name is already loaded
2 Answers

When you try to install the module in CI/CD pipeline you can specify the user so that it won't conflict with other.

#install Az PowerShell Modules with Specific User
- task: PowerShell@2
  displayName: Install PowerShell Modules (Az.Accounts, Az.KeyVault, Az.Resources, Posh-ACME)
  inputs:
    targetType: 'inline'
    script: 'Install-Module Az.Accounts, Az.KeyVault, Az.Resources, Posh-ACME -Force CurrentUser' 

The 'Get-AzKeyVaultCertificate' command was found in the | module 'Az.KeyVault', but the module could not be loaded. For | more information, run 'Import-Module Az.KeyVault'.

Assembly with the same name is already loaded

The error messages show that Az keyVault was already installed but not able to load in a pipeline to run task. You can specify the user(Current user or all user) to install the required module to specific user and import the required module.

Related