连接-AzAccount: 未将术语“Connect-AzAccount”识别为cmdlet、函数、脚本文件或可操作程序的名称。

45

我正在尝试使用PowerShell任务并使用inline模式在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、函数、脚本文件或可执行程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。


2
从这个问题开始,尝试使用Azure PowerShell任务的v4版本。这将加载Az cmdlets。 - vernou
7个回答

57

有可能该命令所属的模块“Az”不存在/需要被导入。在这种情况下,

情况1

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

对于情况2

使用以下命令导入模块 - Import-Module Az.Accounts


1
实用提示:如果此解决方案无法正常工作,请尝试在 PowerShell 7 中运行上述步骤。由于 Windows 的默认版本为 PowerShell 5,您可能需要安装版本 7。这是我成功的方法。 - EzPz

22
对我来说,这是个问题 - 安装了 AzureRMAZ
在 Windows PowerShell 中,检查是否已安装 AzureRM: Get-InstalledModule -name AzureRM 使用命令 Uninstall-module -name AzureRM 来移除它。
如果上述命令不起作用,请使用以下命令: Get-Module -ListAvailable | Where-Object {$_.Name -like 'AzureRM*'} | Uninstall-Module 下一步 -> 将 powershell 的执行策略设置为 RemoteSigned Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser 下一步 -> 在 Windows PowerShell 中安装 Az PowerShell 模块 Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force 下一步 -> 输入 Connect-AzAccount 并完成登录流程。

2
对于那些好奇的人,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的方法可能是您最好的选择。 - Ryanman
我今天在全新安装的Windows 11上遇到了这个问题。我猜测是Visual Studio为我安装了AzureRM。 - Paul Wade

13

我建议您转用 AzurePowershellTask,因为其中安装了预设模块:

enter image description here

您也可以自行尝试安装模块,详见此处。但这是没必要的,因为您可以利用现有任务。


11

在我的情况下,AZ未成功安装,因为一些AZ模块已经与AzureRM一起安装。我添加了参数-AllowClobber现在所有的AZ模块都已安装。

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

使用命令Uninstall-AzureRM卸载 AzureRM 也是一个很好的解决方案,因为您将不再使用 AzureRM,微软计划在2024年2月停止对其提供支持。

3

尝试使用

Login-AzAccount

代替

Connect-AzAccount


1

0

第一个难点。如上所述,首先确保您正在使用的 cmdlets 版本是 Az 还是 AzureRM。上述建议非常版本特定,因此这一点非常重要。

然后,请确保您已经为 Azure RG 授予了正确的权限。

以下是一个示例,使用 MI 进行身份验证来运行 ADF 管道,效果很好,并包括简单的错误处理,因此如果 ps 命令失败,则 sqlagent 将失败(默认情况下不会失败)。

#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

Stackoverflow不允许编辑,真烦人...所以把这个放在评论里。上面使用的是目前支持的Az cmdlets,而不是已停用的AzureRM。此外,上面有一些建议对我来说很昂贵,因为它只安装给当前用户使用的Az cmdlets。这对于使用sqlagent服务帐户从Sqlagent运行是没有任何好处的。相反,执行以下命令以允许所有用户使用它们... Install-Module -Name Az -Scope AllUsers -Repository PSGallery -Force - Gary

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接