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

Pra*_*eep 5 azure-powershell azure-devops azure-pipelines

我正在尝试通过使用内联模式的 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;
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

Connect-AzAccount :术语“Connect-AzAccount”不被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

Yas*_*ala 61

该命令所属的模块 - 'Az' 可能不存在/必须导入。在这种情况下,

情况1

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

对于情况 2 导入模块使用 -Import-Module Az.Accounts

  • 有用的提示:如果此解决方案不起作用,请尝试在 PowerShell 7 中运行上述步骤。您可能必须安装版本 7,因为 Windows 上的默认版本是 PowerShell 5。这对我有用。 (2认同)

Wit*_*ult 25

对我来说,这就是问题所在 - AzureRMAZ都已安装。

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

Get-InstalledModule -name AzureRM

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

如果上面的命令不起作用,请使用下面的命令

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

下一页->

将 powershell 的executionPolicy 设置为RemoteSigned

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

下一页->

在 Windows PowerShell 中安装 Az PowerShell 模块

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

下一页->

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

  • 对于那些好奇的人来说,AZ 是 AzureRM 的后继者,您可以在这里看到: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。这可能是我的想象,但看起来 powershell 工具已经经历了多次修订。截至 2022 年,WitVault 的方法可能是您最好的选择。 (3认同)

Krz*_*tof 13

我建议您切换到AzurePowershellTask ​​,因为您发现有预安装的模块:

在此输入图像描述

您还可以尝试自行安装模块,如此处所示,但这毫无意义,因为您可以利用现有任务。


Aug*_*ste 11

就我而言,AZ 未成功安装,因为某些 AZ 模块已随 AzureRM 安装。我添加了参数-AllowClobber,现在所有 AZ 模块均已安装。

 Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -AllowClobber
Run Code Online (Sandbox Code Playgroud)

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