42

I am trying to execute the following PowerShell script in Azure DevOps pipeline by using PowerShell task with inline mode.

$clientId= "xxxxxxxxx"
$clientSecret= "xxxxxxx"
$subscriptionId= "xxxxxxxx"
$tenantId= "xxxxxxxxxxxxx"
# sign in
Write-Host "Logging in...";
$SecurePassword = $clientSecret | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $clientId, $SecurePassword
Connect-AzAccount -ServicePrincipal -Credential $cred-Tenant $tenantId 
# set azure context with  subscriptionId
Set-AzContext -SubscriptionId $subscriptionId
# select subscription
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzSubscription -SubscriptionId $subscriptionId;

But I am getting the following error:

Connect-AzAccount : The term 'Connect-AzAccount' 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.

Pradeep
  • 5,101
  • 14
  • 68
  • 140
  • 2
    From this [issue](https://github.com/MicrosoftDocs/azure-devops-docs/issues/3869), try to use the v4 of the Azure PowerShell task. This loads the Az cmdlets. – vernou Oct 02 '20 at 13:16

7 Answers7

53

It is possible that the module this command belongs to - 'Az' isn't present/have to be imported. In which case,

Case-1

  1. Open Powershell as Administrator
  2. Install module - Install-Module Az
  3. Import-Module Az
  4. Your command - Connect-AzAccount should work now.

For case-2 Import module using - Import-Module Az.Accounts

Shivam Anand
  • 952
  • 1
  • 10
  • 21
Yash Tamakuwala
  • 1,789
  • 1
  • 24
  • 33
  • 1
    Helpful hint: If this solution doesn't work, try running the above steps in PowerShell 7. You will probably have to install version 7 as the default on Windows is PowerShell 5. This is what worked for me. – EzPz Aug 09 '22 at 16:41
21

For me this was the issue - AzureRM and AZ both were installed.

In Windows PowerShell, check that you have AzureRM installed:

Get-InstalledModule -name AzureRM

use command Uninstall-module -name AzureRM to remove it.

If above command doesn't work use below one

Get-Module -ListAvailable | Where-Object {$_.Name -like 'AzureRM*'} | Uninstall-Module

Next ->

Set executionPolicy to RemoteSigned for powershell

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Next ->

Install the Az PowerShell Module in Windows PowerShell

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

Next ->

type Connect-AzAccount and complete the signing flow.

Sukhi
  • 13,261
  • 7
  • 36
  • 53
WitVault
  • 23,445
  • 19
  • 103
  • 133
  • 2
    For those who are curious, AZ is the successor to AzureRM, as you can see here: https://azure.microsoft.com/en-us/blog/azure-powershell-cross-platform-az-module-replacing-azurerm/#:~:text=Az%20is%20a%20replacement%20for,latest%20tooling%20for%20Azure%20services. It may be my imagination but it seems like the powershell tooling has gone through multiple revisions. As of 2022 WitVault's approach is probably your best bet. – Ryanman Feb 03 '22 at 15:45
  • I had this problem today on a fresh install of Windows 11. I'm guessing Visual studio is what installed AzureRM for me. – Paul Wade Feb 09 '22 at 00:48
12

I would recommend you to switch to AzurePowershellTask as you find there preinstalled modules:

enter image description here

You can also try install modules on your own as it is shown here but this is pointless since you can leverage existing task.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
7

In my case, AZ wasn't successfully installed because some AZ modules were already installed with AzureRM. I added the parameter -AllowClobber and now all the AZ modules are now installed.

 Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -AllowClobber

Uninstalling AzureRM with command Uninstall-AzureRM may also be a great solution, because you're not going to use AzureRM anymore. Microsoft is going to stop supporting it sometimes in February 2024.

Auguste
  • 2,007
  • 2
  • 17
  • 25
3

Try using

Login-AzAccount

instead of

Connect-AzAccount

cristiandatum
  • 329
  • 2
  • 10
0

First hurdle. As stated, first makes sure what version of cmdlets you're using: Az or AzureRM. The above advice is very version specific so that's important.

Then make sure you've granted the correct permissions with the Azure RG.

Here's an example using MI for authentication to run an ADF pipeline that works great AND includes simple error handling so sqlagent will fail if ps command fails (by default it does NOT).

#NOSQLPS
$erroractionpreference = "Stop"

Connect-AzAccount -Identity
Select-AzSubscription -Tenant "tenantidgoeshere"
Select-AzSubscription -SubscriptionName "nameofsubsriptiongoeshere"

$dfname = "ADFNamegoeshere"
$rgName = "RGNamegoeshere"
$pipeline = "ADFPipelineNamegoeshere"

Invoke-AzDataFactoryV2Pipeline -DataFactoryName $dfname -ResourceGroupName $rgName -PipelineName $pipeline
Gary
  • 69
  • 6
  • Stackoverflow isn't allowing edits Grrr....so putting this in comments. Above is using the currently supported Az cmdlets, not the de-supported AzureRM. Also there was some advice above which was costly to me regarding installing the Az cmdlets as it was only installing for the current user. That won't do you any good to run from Sqlagent using the sqlagent service account. Instead do the command as follows to allow all users to use them..... Install-Module -Name Az -Scope AllUsers -Repository PSGallery -Force – Gary Feb 21 '23 at 22:40
0

I have been struggling for hours and it all worked after switching from powershell x86 to powershell x64 bit version and following th steps mentioned in https://learn.microsoft.com/en-us/powershell/azure/install-azps-windows?view=azps-10.1.0&tabs=powershell&pivots=windows-psgallery . Most importantly when the command Get-Module -Name AzureRM -ListAvailable is run it should return empty

Padmanaban
  • 55
  • 7