【问题标题】:Pass bool param from VSTS to Powershell script将布尔参数从 VSTS 传递到 Powershell 脚本
【发布时间】:2018-11-28 06:22:50
【问题描述】:

如果我需要将一个布尔值从 VSTS 传递到 powershell 脚本以在 CD 中进行部署。我收到以下错误:

无法将值“System.String”转换为类型“System.Boolean”。布尔参数只接受布尔值和数字,例如 $True、$False、1 或 0。

我将 VSTS 中的参数作为内联脚本 -ClientCertificateEnabled "$(ClientCertificateEnabled)"

并通过parameters.local.jason 使用replacetoken.ps1 复制template.json 中的值。

parameters.local.jason

"clientCertEnabled": {
      "value": "{{clientCertificateEnabled}}"
    },

replacetoken.ps1

[Parameter(Mandatory=$true)]
    [bool]
    $ClientCertificateEnabled

$depParametersFile = $depParametersFile.Replace('{{clientCertificateEnabled}}', $ClientCertificateEnabled)

deploy.ps1

[Parameter(Mandatory=$true)]
  [bool]
  $ClientCertificateEnabled

template.json

"clientCertEnabled": {
      "type": "bool",
      "defaultValue": true,
      "metadata": {
        "description": "Indicates if client certificate is required on web applications on Azure."
      }
    }

 "clientCertEnabled": "[parameters('clientCertEnabled')]"

【问题讨论】:

  • 我不知道 VSTS,但错误似乎很简单。您使用的值 "0""1" 被视为字符串,而 0 1 $true $false 是 Powershell 中的实际布尔值(请注意缺少引号)。请包含您的代码,以便我们确定错误发生的位置。
  • 它背后的代码看起来像什么?

标签: powershell azure-devops azure-pipelines-release-pipeline


【解决方案1】:

假设您正在编写分布式任务,VSTS/AzureDevOps 会将所有参数作为字符串传递。您需要声明您的 ps1 参数块以接受字符串并在内部转换它们。

我没有使用 PowerShell 任务来调用脚本(只有内联脚本),所以我不知道它是如何传递参数的。假设它执行相同的字符串传递是安全的。

param
(
    [string]$OverwriteReadOnlyFiles = "false"
)

我写了一个 Convert-ToBoolean 函数来处理转换并调用它。

[bool]$shouldOverwriteReadOnlyFiles = Convert-ToBoolean $OverwriteReadOnlyFiles

函数定义为:

<#
.SYNOPSIS 
    Converts a value into a boolean
.DESCRIPTION 
    Takes an input string and converts it into a [bool]
.INPUTS
    No pipeline input.
.OUTPUTS
    True if the string represents true
    False if the string represents false
    Default if the string could not be parsed
.PARAMETER StringValue
    Optional.  The string to be parsed.
.PARAMETER Default
    Optional.  The value to return if the StringValue could not be parsed.
    Defaults to false if not provided.
.NOTES
.LINK
#>
function Convert-ToBoolean
(
    [string]$StringValue = "",
    [bool]$Default = $false
)
{
    [bool]$result = $Default

    switch -exact ($StringValue)
    {
         "1"     { $result = $true;  break; }
         "-1"    { $result = $true;  break; }
         "true"  { $result = $true;  break; }
         "yes"   { $result = $true;  break; }
         "y"     { $result = $true;  break; }
         "0"     { $result = $false; break; }
         "false" { $result = $false; break; }
         "no"    { $result = $false; break; }
         "n"     { $result = $false; break; }
    }

    Write-Output $result
}

【讨论】:

  • 我一直使用 .net Boolean TryParse。这不涵盖您的正常用例吗?
  • 没有。 TryParse 不会将字符串 1 和 0 识别为真值和假值。它只想将字符串值 true 和 false 转换为布尔值。
【解决方案2】:

我设法通过以下更改解决了这个问题,并将所有 ps1 文件中的布尔类型恢复为字符串。

parameters.local.json改成如下(只是去掉了双引号)

"clientCertEnabled": {
      "value": {{clientCertificateEnabled}}
    },

因此在执行replacetoken.ps1 后进行上述更改,parameters.local.json 将如下所示

"clientCertEnabled": {
      "value": true
    }, 

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2021-12-12
    • 2016-08-28
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    相关资源
    最近更新 更多