将APIM从stv1迁移至stv2时Pipeline报错:not enough values to unpack
问题:APIM从stv1迁移到stv2的Azure CLI脚本报错排查
我正尝试使用Azure CLI脚本将API管理(APIM)从平台版本stv1迁移至stv2,所用脚本如下:
- task: AzureCLI@2 displayName: "Migrating Platform Version of APIM to stv2" inputs: azureSubscription: ${{ parameters.Subscription }} scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | $APIMName = "$(api_management_service_name)" $ResourceGroup = "$(resource_group)" $APIM_PlatformVersion = az apim show --name $APIMName --resource-group $ResourceGroup --query platformVersion --output tsv if($APIM_PlatformVersion -contains 'stv1'){ $headers = @{ 'Content-Type' = 'application/json' } $APIM_ResourceId = az apim show --name $APIMName --resource-group $ResourceGroup --query id --output tsv Write-Host "Migrating to stv2 as the current version is $APIM_PlatformVersion" # Call REST API to migrate to stv2 and preserve VIP address az rest --method post --uri "$APIM_ResourceId/migrateToStv2?api-version=2023-03-01-preview" --body '{"mode": "PreserveIp"}' --headers $headers } else{ Write-Host "APIM already in $APIM_PlatformVersion" }
执行时触发如下错误:
ERROR: The command failed with an unexpected error. Here is the traceback: ERROR: not enough values to unpack (expected 2, got 1)
报错原因
这个错误是因为PowerShell哈希表无法直接传递给Azure CLI的--headers参数。Azure CLI要求--headers的参数格式是key=value的字符串键值对,直接传入PowerShell哈希表对象会导致CLI解析时无法正确拆解键值对,从而抛出"not enough values to unpack"异常。
解决方法
可以通过两种方式修复这个问题:
方式1:直接在az rest命令中指定headers字符串
不需要定义哈希表,直接用Content-Type=application/json的格式传递参数:
- task: AzureCLI@2 displayName: "Migrating Platform Version of APIM to stv2" inputs: azureSubscription: ${{ parameters.Subscription }} scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | $APIMName = "$(api_management_service_name)" $ResourceGroup = "$(resource_group)" $APIM_PlatformVersion = az apim show --name $APIMName --resource-group $ResourceGroup --query platformVersion --output tsv if($APIM_PlatformVersion -contains 'stv1'){ $APIM_ResourceId = az apim show --name $APIMName --resource-group $ResourceGroup --query id --output tsv Write-Host "Migrating to stv2 as the current version is $APIM_PlatformVersion" # Call REST API to migrate to stv2 and preserve VIP address az rest --method post --uri "$APIM_ResourceId/migrateToStv2?api-version=2023-03-01-preview" --body '{"mode": "PreserveIp"}' --headers Content-Type=application/json } else{ Write-Host "APIM already in $APIM_PlatformVersion" }
方式2:将哈希表转换为CLI兼容的格式
如果需要保留哈希表(比如多headers场景),可以把哈希表转换成key=value的字符串数组:
- task: AzureCLI@2 displayName: "Migrating Platform Version of APIM to stv2" inputs: azureSubscription: ${{ parameters.Subscription }} scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | $APIMName = "$(api_management_service_name)" $ResourceGroup = "$(resource_group)" $APIM_PlatformVersion = az apim show --name $APIMName --resource-group $ResourceGroup --query platformVersion --output tsv if($APIM_PlatformVersion -contains 'stv1'){ $headers = @{ 'Content-Type' = 'application/json' } # 转换为CLI兼容的键值对字符串数组 $cliHeaders = $headers.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" } $APIM_ResourceId = az apim show --name $APIMName --resource-group $ResourceGroup --query id --output tsv Write-Host "Migrating to stv2 as the current version is $APIM_PlatformVersion" # Call REST API to migrate to stv2 and preserve VIP address az rest --method post --uri "$APIM_ResourceId/migrateToStv2?api-version=2023-03-01-preview" --body '{"mode": "PreserveIp"}' --headers $cliHeaders } else{ Write-Host "APIM already in $APIM_PlatformVersion" }
额外建议:可以在脚本中增加Write-Host $APIM_PlatformVersion输出,确认该变量的取值是否正确,避免因为取值为空导致逻辑判断异常。
内容的提问来源于stack exchange,提问作者QuestingSoul
相关产品推荐
相关产品推荐

