You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps中AzurePowerShell@5执行失败,提示需运行az login

问题解决:Azure DevOps管道启动/停止Web App失败提示"az login"

核心问题诊断

从错误日志可定位三个关键问题:

  1. 云环境不匹配:日志显示The subscription 'xxx' doesn't exist in cloud 'AzureCloud',结合服务连接名称s148d.azdo-deployment-dfe-gov,说明目标订阅属于Azure政府云,但任务默认使用公共云环境,导致无法找到订阅。
  2. 参数传递错误:YAML中webAppNames的多行列表格式,传递给脚本时被识别为单个字符串,导致脚本把整个列表当成一个WebApp名称处理。
  3. 上下文不共享:AzurePowerShell@5任务通过Az模块完成身份验证,但脚本使用az CLI命令,az CLI不会自动继承Az模块的登录上下文,因此提示需要az login。

修复步骤

1. 配置正确的云环境

在AzurePowerShell@5任务中指定目标云环境(Azure政府云),确保服务连接能访问对应订阅:

  • 在任务输入中添加environmentName: AzureUSGovernment(根据实际政府云区域调整)

2. 修复WebApp名称参数传递

将YAML中webAppNames改为逗号分隔的字符串,脚本内再拆分为数组:

  • YAML变量改为:value: 's148d01-as-vyed-admin-01,s148d01-as-vyed-pub-01'
  • 脚本内拆分:$webAppList = $webAppNames -split ',' | ForEach-Object { $_.Trim() }

3. 统一使用Az模块命令

直接用Az模块命令替代az CLI,利用任务已完成的身份验证上下文:

  • 替换az webapp系列命令为Get-AzWebApp、Start-AzWebApp、Stop-AzWebApp
  • 替换az functionapp系列命令为Get-AzFunctionApp、Start-AzFunctionApp、Stop-AzFunctionApp

修改后的管道YAML

trigger: none

schedules:
  - cron: "0 7 * * Mon-Fri"
    displayName: "Start WebApp at 7 AM (Mon-Fri)"
    branches:
      include:
        - main
    always: true

  - cron: "0 19 * * Mon-Fri"
    displayName: "Stop WebApp at 7 PM (Mon-Fri)"
    branches:
      include:
        - main
    always: true

stages:
- stage: dev
  displayName: 'Start and Stop Dev Environment'
  variables:
  - name: subscriptionId
    value: '12345678-abcd-6789-1122-ab12bc34de56ef'
  - name: resourceGroupName
    value: 's148d01-vyed'
  - name: webAppNames
    value: 's148d01-as-vyed-admin-01,s148d01-as-vyed-pub-01'
  - name: functionAppName
    value: 's148d01-fa-vyed-01'
  jobs:
  - job: Dev_Webapp
    steps:
    - task: AzurePowerShell@5
      displayName: 'Powershell task'
      inputs:
        azureSubscription: s148d.azdo-deployment-dfe-gov
        ScriptType: filePath
        ScriptPath: $(Build.SourcesDirectory)/scripts/s148Autoschdulers.ps1
        ScriptArguments: '-subscriptionId "$(subscriptionId)" -resourceGroupName "$(resourceGroupName)" -webAppNames "$(webAppNames)" -functionAppName "$(functionAppName)"'
        azurePowerShellVersion: 'LatestVersion'
        pwsh: true
        environmentName: AzureUSGovernment # 匹配目标云环境

修改后的PowerShell脚本

param (
    [Parameter(Mandatory=$true)]
    [string]$subscriptionId,
    [Parameter(Mandatory=$true)]
    [string]$resourceGroupName,
    [Parameter(Mandatory=$true)]
    [string]$webAppNames, # 改为字符串,后续拆分
    [Parameter(Mandatory=$true)]
    [string]$functionAppName
)

# 切换到目标订阅
Set-AzContext -SubscriptionId $subscriptionId | Out-Null

# 拆分WebApp名称为数组
$webAppList = $webAppNames -split ',' | ForEach-Object { $_.Trim() }

# 获取当前时间和日期
$currentHour = (Get-Date).Hour
$currentDay = (Get-Date).DayOfWeek

foreach ($webAppName in $webAppList) {
    $webApp = Get-AzWebApp -Name $webAppName -ResourceGroupName $resourceGroupName
    $webAppStatus = $webApp.State
    Write-Output "Current status of Azure Web App $webAppName: $webAppStatus"

    if ($currentHour -eq 7) {
        if ($webAppStatus -eq "Stopped") {
            Start-AzWebApp -Name $webAppName -ResourceGroupName $resourceGroupName
            Write-Output "Azure Web App $webAppName started successfully."
        } else {
            Write-Output "Azure Web App $webAppName is already running."
        }
    } elseif ($currentHour -eq 19) {
        if ($webAppStatus -eq "Running") {
            Stop-AzWebApp -Name $webAppName -ResourceGroupName $resourceGroupName
            Write-Output "Azure Web App $webAppName stopped successfully."
        } else {
            Write-Output "Azure Web App $webAppName is already stopped."
        }
    } else {
        Write-Output "Current time is not 7 AM or 7 PM. WebApp status will not be changed. It will automatically start or stop the webapp at 7AM and 7PM on Monday to Friday"
    }
}

# 处理Function App
$functionApp = Get-AzFunctionApp -Name $functionAppName -ResourceGroupName $resourceGroupName
$functionAppStatus = $functionApp.State
Write-Output "Current status of Azure Function App $functionAppName: $functionAppStatus"

if ($currentDay -eq 'Monday') {
    if ($currentHour -eq 7) {
        if ($functionAppStatus -eq "Stopped") {
            Start-AzFunctionApp -Name $functionAppName -ResourceGroupName $resourceGroupName
            Write-Output "Azure Function App $functionAppName started successfully."
        } else {
            Write-Output "Azure Function App $functionAppName is already running."
        }
    } elseif ($currentHour -eq 19) {
        if ($functionAppStatus -eq "Running") {
            Stop-AzFunctionApp -Name $functionAppName -ResourceGroupName $resourceGroupName
            Write-Output "Azure Function App $functionAppName stopped successfully."
        } else {
            Write-Output "Azure Function App $functionAppName is already stopped."
        }
    } else {
        Write-Output "Current time is not 7 AM or 7 PM on Monday. The function app status cannot be changed."
    }
} else {
    Write-Output "Today is not Monday. Function App status will not be changed. It will automatically start or stop the function app at 7AM and 7PM on Monday"
}

内容的提问来源于stack exchange,提问作者Nish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 15:55:57