0
votes

I'm running a Self Hosted Windows Agent in Azure DevOps. I have installed PowerShell Core 6.2.3 and as the Administrator installed the Az module so that it is available to all users.

When I run a build using the Azure PowerShell task (V4) - marking as "Use PowerShell Core" and the "Latest Version" of Azure PowerShell I receive the following message:

==============================================================================
Task         : Azure PowerShell
Description  : Run a PowerShell script within an Azure environment
Version      : 4.159.3
Author       : Microsoft Corporation
Help         : [Learn more about this task](https://go.microsoft.com/fwlink/?LinkID=613749)
==============================================================================
Added TLS 1.2 in session.
Disconnect-AzureRmAccount -Scope Process -ErrorAction Stop
##[warning]Run Connect-AzureRmAccount to login.
##[error]Could not find the modules: 'Az.Accounts' with Version: ''. If the module was recently installed, retry after restarting the Azure Pipelines task agent.

If I run the script on a Hosted Agent it works fine. I can't work out why it fails. I assume it is something different in the profile or environment that the build agent runs under - but I'm not sure how to check this.

On the build agent if I open a PSCore window and run:

Get-Module Az -ListAvailable

It finds the module correctly.

1
Have you restart your machine?Jack Jia
Yes several times!simon_dmorias
You can try to switch to version 3.0 of the task to see if the problem go away. Please refer to this similar issueHugh Lin
@Hugh V3 uses AzureRM and does not support PowerShell Core - of which I need both! I think the "Use PowerCore Core" checkbox does nothing (on Windows at least) and it is trying to execute it using Windows PowerShell.simon_dmorias

1 Answers

0
votes

The DevOps Agent is most likely running as a different user and the Az modules have not been installed for that user. Assuming the agent is running as a service, you can check the user by running services.msc and look for "Azure Pipelines Agent", "VSTS Agent" or "vstsagent.[orgname].[agentname]" [1].

Run Get-Module Az -ListAvailable to see where the modules have been installed. Modules under C:\Program Files\PowerShell\Modules have been installed for all users [2].

You can install the modules for all users with:

Install-Module -Name Az -Scope AllUsers

[1] https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows?view=azure-devops#run-as-a-service

[2] https://docs.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-7.1#parameters