azure - Azure 凭据尚未设置或已过期,请运行 Connect-AzAccount
问题描述
与以下问题相关(Azure DevOps - Custom Task - PowerShell with Azure Authentification)我现在Connect-AzAccount
使用 Azure DevOps Custom Task 登录。
在下一步中,我想并行运行多个作业,以首先操作然后通过模板部署某些 Azure 资源。使用 AzureRM 这工作没有问题,但切换到 Az 后我总是得到错误
您的 Azure 凭据尚未设置或已过期,请运行 Connect-AzAccount 以设置您的 Azure 凭据。
我已经将 Az 模块更新为最新版本。
这是我的做法:
认证
try {
$endpoint = Get-VstsEndpoint -Name $serviceName -Require
if (!$endpoint) {
throw "Endpoint not found..."
}
$subscriptionId = $endpoint.Data.SubscriptionId
$tenantId = $endpoint.Auth.Parameters.TenantId
$servicePrincipalId = $endpoint.Auth.Parameters.servicePrincipalId
$servicePrincipalKey = $endpoint.Auth.Parameters.servicePrincipalKey
$spnKey = ConvertTo-SecureString $servicePrincipalKey -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($servicePrincipalId,$spnKey)
Connect-AzAccount -ServicePrincipal -TenantId $tenantId -Credential $credentials
Select-AzSubscription -SubscriptionId $subscriptionId -Tenant $tenantId
$ctx = Get-AzContext
Write-Host "Connected to subscription '$($ctx.Subscription)' and tenant '$($ctx.Tenant)'..."
} catch {
Write-Host "Authentication failed: $($_.Exception.Message)..."
}
并行部署
foreach ($armTemplateFile in $armTemplateFiles) {
$logic = {
Param(
[object]
[Parameter(Mandatory=$true)]
$ctx,
[object]
[Parameter(Mandatory=$true)]
$armTemplateFile,
[string]
[Parameter(Mandatory=$true)]
$resourceGroupName
)
####################################################################
# Functions
function Format-ValidationOutput {
param ($ValidationOutput, [int] $Depth = 0)
Set-StrictMode -Off
return @($ValidationOutput | Where-Object { $_ -ne $null } | ForEach-Object { @(' ' * $Depth + ': ' + $_.Message) + @(Format-ValidationOutput @($_.Details) ($Depth + 1)) })
}
####################################################################
# Logic
# Some template manipulation
$paramFileContent | ConvertTo-Json -Depth 100 | Set-Content -Path $paramTemplateFile.FullName
$templateFileContent | ConvertTo-Json -Depth 100 | Set-Content -Path $armTemplateFile.FullName
####################################################################
# Test Deployment
$ErrorMessages = Format-ValidationOutput (Test-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName `
-TemplateFile $armTemplateFile.FullName `
-TemplateParameterFile $paramTemplateFile.FullName `
-DefaultProfile $ctx)
if ($ErrorMessages) {
Write-Host '', 'Validation returned the following errors:', @($ErrorMessages), '', 'Template is invalid.'
}
else { # Deploy
New-AzResourceGroupDeployment -Name (($armTemplateFile.Name).Split(".")[0] + ((Get-Date).ToUniversalTime()).ToString('MMddHHmm')) `
-ResourceGroupName $resourceGroupName `
-TemplateFile $armTemplateFile.FullName `
-TemplateParameterFile $paramTemplateFile.FullName `
-Force `
-ErrorVariable ErrorMessages `
-DefaultProfile $ctx
if ($ErrorMessages) {
Write-Host '', 'Template deployment returned the following errors:', @(@($ErrorMessages) | ForEach-Object { $_.Exception.Message.TrimEnd("`r`n") })
}
}
}
Start-Job $logic -ArgumentList (Get-AzContext), $armTemplateFile, $ResourceGroupName
}
While (Get-Job -State "Running")
{
Start-Sleep 10
Write-Host "Jobs still running..."
}
Get-Job | Receive-Job
解决方案
出于某种原因(我不知道),自从移至 Az 模块(也使用最新版本 (1.4.0))后,将上下文传递给后台作业不再起作用。我尝试使用Save-AzContext
withImport-AzContext
或通过获取上下文Get-AzContext
并将其传递给后台作业(使用Set-AzContext
或直接使用-DefaultProfile
(此方法与 AzureRm 模块一起使用))。
现在对我有用的解决方法是单独验证每个后台作业。
对于工作定义:
foreach ($armTemplateFile in $armTemplateFiles) {
$logic = {
Param(
[object]
[Parameter(Mandatory=$true)]
$endpointInput,
[object]
[Parameter(Mandatory=$true)]
$armTemplateFile,
[string]
[Parameter(Mandatory=$true)]
$resourceGroupName
)
###########################
#Login
Write-Host "Authenticating..."
try {
$endpoint = $endpointInput
if (!$endpoint) {
throw "Endpoint not found..."
}
$subscriptionId = $endpoint.Data.SubscriptionId
$tenantId = $endpoint.Auth.Parameters.TenantId
$servicePrincipalId = $endpoint.Auth.Parameters.servicePrincipalId
$servicePrincipalKey = $endpoint.Auth.Parameters.servicePrincipalKey
$spnKey = ConvertTo-SecureString $servicePrincipalKey -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($servicePrincipalId,$spnKey)
Connect-AzAccount -ServicePrincipal -TenantId $tenantId -Credential $credentials
Select-AzSubscription -SubscriptionId $subscriptionId -Tenant $tenantId
$ctx = Get-AzContext
Write-Host "Connected to subscription '$($ctx.Subscription)' and tenant '$($ctx.Tenant)'..."
} catch {
Write-Host "Authentication failed: $($_.Exception.Message)..."
}
开始工作:
Start-Job $logic -Name $jobName -ArgumentList $endpoint, $armTemplateFile, $ResourceGroupName
推荐阅读
- java - 如何为开源服务器端(java)文件开发服务器端(jsp)代码?
- python - 如何获取正在使用的 ipython 笔记本的文件路径?(相当于 __file__)
- xcode - 如何让我的 SwiftUI UIViewRepresentable 在预览中尊重intrinsicContentSize?
- android - java.lang.IncompatibleClassChangeError:类 'org.apache.http.message.BasicHeader' 未实现接口 'org.apache.http.NameValuePair'
- algorithm - 如何找到对数组进行排序所需的最小交换次数?
- javascript - 如何解决检索数据时由于操作顺序不同导致javascript+firebase函数返回值不正确的问题?
- r - 使用 ggplot 绘制多色垂直线,以将每种类型的平均时间显示为构面。每种类型都有不同的垂直线
- javascript - 如何通过图像查找元素
- ios - 如何从 Azure Pipeline 在 Bitbucket 中更新构建状态?
- linux - 如何将 shell 脚本调用命令传递给日志文件?