Shabar
Shabar

Reputation: 2823

Pass bool param from VSTS to Powershell script

If I need to pass a boolean value from VSTS to powershell script to do the deployment in CD. I get the below error though:

Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.

I pass the param from VSTS as inline script -ClientCertificateEnabled "$(ClientCertificateEnabled)"

And replcae values in template.json using replacetoken.ps1 via 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')]"

Upvotes: 2

Views: 4461

Answers (2)

Shabar
Shabar

Reputation: 2823

I managed to address the issue with below change and reverted back Boolean type back to string in all the ps1 files.

Changed parameters.local.json as below (just removed double quote)

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

So with the above change after executing replacetoken.ps1 parameters.local.json will look like below

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

Upvotes: 0

daughey
daughey

Reputation: 749

Assuming that you are writing a distributed task, VSTS/AzureDevOps will pass all the parameters as string. You need to declare your ps1 param block to accept strings and internally convert them.

I haven't used the PowerShell task to invoke scripts (only inline script) so I don't know how it passes parameters. It would be safe to assume it does the same passing of strings.

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

I wrote a Convert-ToBoolean function to handle the conversion and call it.

[bool]$shouldOverwriteReadOnlyFiles = Convert-ToBoolean $OverwriteReadOnlyFiles

The function is defined as:

<#
.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
}

Upvotes: 3

Related Questions