我尝试使用内联模式的 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、函数、脚本文件或 可运行的程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确并尝试 又来了。
此命令所属的模块 - 'Az' 可能不存在/必须导入。在这种情况下,
案例1
Install-Module Az
Import-Module Az
Connect-AzAccount
现在应该可以工作了。对于案例2 使用 -
Import-Module Az.Accounts
导入模块
对我来说,这就是问题 - AzureRM 和 AZ 都已安装。
在 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 未成功安装,因为某些 AZ 模块已随 AzureRM 安装。我添加了参数
-AllowClobber
,现在所有 AZ 模块都已安装。
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -AllowClobber
使用命令
Uninstall-Module -Name AzureRM
卸载 AzureRM 也可能是一个很好的解决方案,因为您将不再使用 AzureRM。 Microsoft 有时会在 2024 年 2 月停止支持。
尝试使用
登录-AzAccount
而不是
连接-AzAccount
我已经奋斗了几个小时,从 powershell x86 切换到 powershell x64 位版本并按照 https://learn.microsoft.com/en-us/powershell/azure/install-azps- 中提到的步骤操作后,一切都成功了windows?view=azps-10.1.0&tabs=powershell&pivots=windows-psgallery 。最重要的是,当运行命令 Get-Module -Name AzureRM -ListAvailable 时,它应该返回空
第一个障碍。如上所述,首先确保您使用的 cmdlet 版本: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