Reputation: 5500
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.
Upvotes: 56
Views: 149232
Reputation: 376
I could only get this to work on Windows (Powershell 5.1) if I installed the latest Azure CLI into Windows from the MSI package:
Upvotes: 0
Reputation: 2189
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-Module -Name 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.
Upvotes: 11
Reputation: 75
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
Upvotes: 2
Reputation: 24130
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.
Upvotes: 27
Reputation: 79
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
Upvotes: 0
Reputation: 2056
It is possible that the module this command belongs to - 'Az' isn't present/have to be imported. In which case,
Case-1
Install-Module Az
Import-Module Az
Connect-AzAccount
should work now.For case-2
Import module using - Import-Module Az.Accounts
Upvotes: 70
Reputation: 40553
I would recommend you to switch to AzurePowershellTask as you find there preinstalled modules:
You can also try install modules on your own as it is shown here but this is pointless since you can leverage existing task.
Upvotes: 13