powershell - TFS/VSTS不能使用非字符串的自定义变量

标签 powershell variables tfs azure-devops task

我一直在使用TFS从PowerShell创建一些发布变量。然后,我可以在TFS中的后续任务中使用'$ Env:Server'来引用此输出,这似乎很棒!例如:

任务1返回服务器名称,然后创建TFS变量:

Write-Host "##vso[task.setvariable variable=Server]"MySevrer

任务2使用以下信息:
Write-Output $env:Server
MyServer

在以其他格式(例如数组或哈希表)输出某些内容时,此功能将无效。所创建的变量只是documentation使用“写入主机”所声明的字符串。

Task1返回:
##vso[task.setvariable variable=Server] System.Collections.DictionaryEntry

任务2不能使用此功能:
Write-Output $env:Server    
System.Collections.DictionaryEntry

Output that is created

我尝试将其输出为数组格式的字符串EG:
[String]$Server = '@{MyServer=@("192.168.0.1")}'
Write-Host "##vso[task.setvariable variable=Server]"$Server

当我提及此内容时,我尝试将其转换回PowerShell中的数组,但是在脚本内部执行此操作时遇到了问题,因为它将字符串视为单个数组对象。 (不是带有值的数组)

有谁知道是否可以根据Powershell任务输出的信息来解析Team Service的任务/任务任务组之间的哈希表或数组?

我目前通过编写包装器/编排函数来解决此问题,但这对我们来说不是理想的方法。我们当前使用的是版本15.117.26714.0,但在新版本中看不到任何内容。

最佳答案

丹尼尔·曼(Daniel Mann)提到,ConvertTo-Json可以解决问题。

例:
在Azure Devops发布管道中,有一个包含以下代码的Azure PowerShell任务:

    $apps = Get-AzureRmResource -ResourceGroupName "$(ResourceGroupName)" -ResourceType Microsoft.Web/sites
    $objectIdArray = New-Object System.Collections.ArrayList
    foreach ($app in $apps) {
    if (!$app.Identity) {
        Write-Host "Enabling identity for $($app.Name) app service..."
        $app = Set-AzureRmWebApp -Name $($app.Name) -ResourceGroupName "$(ResourceGroupName)" -AssignIdentity $true
    } else {
        Write-Host "$($app.Name) app service has $($app.Identity.PrincipalId) as the identity."
    }
    $objectIdArray.Add($($app.Identity.principalId))
    }

$objectIdArrayJson = ($objectIdArray | ConvertTo-Json -Compress)
Write-Host "##vso[task.setvariable variable=objectIdArray;]$objectIdArrayJson"

Write-Host $env:objectIdArray

以下任务是具有覆盖参数的Azure资源组部署任务:
-objectIdArray "$(objectIdArray)"

发布变量"$(objectIdArray)"的值类似于:
["0512706a-0344-418a-9f25-5708d95e44aa","6047cbe6-c109-4aa7-8cfb-b473c088b1b1","68ee0d25-351f-44c8-aecf-cfc259f3cd97","44a6f3d6-23a3-443d-824b-445e0141f09c","805c3e6d-ab31-41f4-9d6c-8c9fc13ce
460","aa13b9db-200d-4c38-abf8-562a915ed8cd","8d1d7ec1-faa6-4af6-b732-331e51e86a90","02222b28-6370-4995-a633-29a1cdd08fd0","a48c21b1-b6ef-4582-b9a0-050965cb3614","9111421b-8535-4326-bbe9-1e891
33a0b56","5b1f6fca-599c-4895-ae4b-fabc0d3a4dd3","b12a935a-b1c3-4dec-b764-7c0a5307a766","8af7d615-c042-43b5-8ac0-736c6cf4ec3f","f0dd4dd9-e540-4e13-a8be-69ce08a6221c","b131e123-a87e-4faf-afed-4
37d6dbae4ab","af7f679b-1ac8-4991-b467-93ba4a16ec22","1bbb649c-b5e6-4f5c-a050-3a0cee0af985","4a7b728e-e8c6-49c0-bde2-54811708d5ab","3b190d28-c390-43c7-9269-1177afaf7b00","49f3777f-8668-4c72-82
60-753f65b933aa","727db5c4-ad56-457e-ad87-47b17c71e29b","801efff8-a852-4e7b-bc81-3d81d3bcfeb5","0947556e-7ece-4a36-a687-3c50f59e32f6"]

关于powershell - TFS/VSTS不能使用非字符串的自定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50931004/

相关文章:

xml - 使用 PowerShell 将 Excel 文件保存为 XML

c++ - `char _3 = ' c'` 在 C 和 C++ 中有效吗?

tfs - 在 tfs 上构建失败,因为工作区无法填充变更集删除的文件

tfs - 如何在TFS中轻松比较/比较2个货架集?

powershell - 有什么理由不在 powershell param() block 中抛出 ArgumentException?

.net - Powershell 卸载模块...完全

powershell - 在powershell studio中清除复选框列表中的复选框

javascript - 变量在我的函数中变得未定义

php - 如何检查在 php 中完成选择后存储的值

tfs - VSTS 错误 : "Publishing build artifacts failed with an error: Unable to create directory ' {p}'