gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:36:42 25 4
gpt4 key购买 nike

如果我需要将VSTS的 bool(boolean) 值传递给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)"
然后通过 template.json使用 replacetoken.ps1替换 parameters.local.jason中的值。

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 / AzureDevOps将把所有参数作为字符串传递。您需要声明ps1 param块以接受字符串并在内部对其进行转换。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com