Azure 凭据尚未设置或已过期,请 运行 Connect-AzAccount
Azure credentials have not been set up or have expired, please run Connect-AzAccount
与以下问题()相关,我现在正在使用 Connect-AzAccount
通过使用 Azure DevOps 自定义任务登录。
下一步我想 运行 并行执行多个作业,首先操作然后通过模板部署某些 Azure 资源。使用 AzureRM 这没有问题,但是在切换到 Az 之后我总是得到错误
Your Azure credentials have not been set up or have expired, please
run Connect-AzAccount to set up your Azure credentials.
我已经将 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
我个人使用这个命令:
Connect-AzureRmAccount
我首先使用 ISE 控制台 直接登录 ,然后会话保持不变,我可以 运行 任何脚本都没有任何问题(无需在脚本中使用此命令).
希望对你有帮助。
编辑。
我看你也是选择订阅,我用的就是这个
Select-AzureRmSubscription -Subscription 'Subscription ID'
由于某种原因(我不知道),自从移动到 Az 模块(也是最新版本 (1.4.0))后,将上下文传递给后台作业不再有效。我尝试使用 Save-AzContext
和 Import-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
与以下问题(Connect-AzAccount
通过使用 Azure DevOps 自定义任务登录。
下一步我想 运行 并行执行多个作业,首先操作然后通过模板部署某些 Azure 资源。使用 AzureRM 这没有问题,但是在切换到 Az 之后我总是得到错误
Your Azure credentials have not been set up or have expired, please run Connect-AzAccount to set up your Azure credentials.
我已经将 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
我个人使用这个命令:
Connect-AzureRmAccount
我首先使用 ISE 控制台 直接登录 ,然后会话保持不变,我可以 运行 任何脚本都没有任何问题(无需在脚本中使用此命令).
希望对你有帮助。
编辑。
我看你也是选择订阅,我用的就是这个
Select-AzureRmSubscription -Subscription 'Subscription ID'
由于某种原因(我不知道),自从移动到 Az 模块(也是最新版本 (1.4.0))后,将上下文传递给后台作业不再有效。我尝试使用 Save-AzContext
和 Import-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