Connect-AzAccount:术语 'Connect-AzAccount' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称

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

我正在尝试使用具有内联模式的 PowerShell 任务在 Azure DevOps 管道中执行以下 PowerShell 脚本。

$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;

但我收到以下错误:

Connect-AzAccount:术语 'Connect-AzAccount' 未被识别为 cmdlet、函数、脚本文件或 可运行的程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确并尝试 再次.

我建议您切换到 AzurePowershellTask,因为您发现其中预装了模块:

您也可以尝试自己安装模块,如图所示here,但这毫无意义,因为您可以利用现有任务。

可能是此命令所属的模块 - 'Az' 未被导入 present/have。在这种情况下,

案例一

  1. 以管理员身份打开 Powershell
  2. 安装模块 - Install-Module Az
  3. Import-Module Az
  4. 您的命令 - Connect-AzAccount 现在应该可以使用了。

对于案例 2 使用 - Import-Module Az.Accounts

导入模块

对我来说这就是问题 - AzureRMAZ 都安装了。

在 Windows PowerShell 中,检查您是否安装了 AzureRM:

Get-InstalledModule -name AzureRM

使用命令 Uninstall-AzureRM 将其删除。

如果不起作用,请使用以下一个

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

下一个 ->

将执行策略设置为 RemoteSigned

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

在 Windows PowerShell

中安装 Az PowerShell 模块

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

下一个 ->

键入 Connect-AzAccount 并完成签名流程。

在我的例子中,AZ 没有成功安装,因为一些 AZ 模块已经与 AzureRM 一起安装。我添加了参数-AllowClobber,现在所有的AZ模块都安装好了。

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

使用命令 Uninstall-AzureRM 卸载 AzureRM 也可能是一个很好的解决方案,因为您将不再使用 AureRM。微软有时会在 2024 年 2 月停止支持它。

尝试使用

Login-AzAccount

而不是

Connect-AzAccount