Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect-AzAccount : The term 'Connect-AzAccount' is not recognized as the name of a cmdlet, function, script file, or operable program

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.

like image 488
Pradeep Avatar asked Oct 02 '20 13:10

Pradeep


4 Answers

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

like image 194
Yash Tamakuwala Avatar answered Oct 09 '22 03:10

Yash Tamakuwala


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.

like image 44
Krzysztof Madej Avatar answered Oct 09 '22 02:10

Krzysztof Madej


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

In Windows PowerShell, check that you have AzureRM installed:

Get-InstalledModule -name AzureRM

use command Uninstall-AzureRM to remove it.

If it doesn't work use below one

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

Next ->

Set executionPolicy to RemoteSigned

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

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.

like image 11
WitVault Avatar answered Oct 09 '22 03:10

WitVault


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.

like image 6
Auguste Avatar answered Oct 09 '22 04:10

Auguste