powershell - 将 bool 参数从 VSTS 传递到 Powershell 脚本

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

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

Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 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)

部署.ps1

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

模板.json

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

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

最佳答案

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

我没有使用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
}

关于powershell - 将 bool 参数从 VSTS 传递到 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53513316/

相关文章:

tfs - VSTS : Execute conditional tasks on builds

azure - Azure Devops YAML 管道中的手动触发阶段

.net - 如何卸载 "Microsoft .NET Core 1.0.0 RC2 - VS 2015 Tooling Preview 1"?

powershell - 如何使用 PowerShell Desired State Configuration 升级 Windows 服务

powershell - 在 Windows 容器上使用复杂的 powershell 脚本构建 Dockerfile

powershell - Visual Studio Team Services - 发布定义 - 以管理员身份运行任务 (PowerShell)

azure-devops - Visual Studio Online 自动生成版本号增量和程序集修补

azure - 如何找到哪个触发器真正启动了Azure管道?

powershell - 如何持续集成 Powershell 脚本

powershell - 如何解析来自Invoke-WebRequest的响应