Pass bool param from VSTS to Powershell script












0















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')]"









share|improve this question




















  • 2





    I don't know about VSTS, but the error seems pretty simple. Your using the value "0" or "1" which is considered a string while 0 1 $true $false are actual boolean values in Powershell (Notice the lack of quotations). Please include your code so we can determine where the error occurs.

    – Shadowzee
    Nov 28 '18 at 6:40








  • 2





    And the code behind it all looks like what?

    – notjustme
    Nov 28 '18 at 6:41
















0















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')]"









share|improve this question




















  • 2





    I don't know about VSTS, but the error seems pretty simple. Your using the value "0" or "1" which is considered a string while 0 1 $true $false are actual boolean values in Powershell (Notice the lack of quotations). Please include your code so we can determine where the error occurs.

    – Shadowzee
    Nov 28 '18 at 6:40








  • 2





    And the code behind it all looks like what?

    – notjustme
    Nov 28 '18 at 6:41














0












0








0








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')]"









share|improve this question
















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')]"






powershell azure-devops azure-pipelines-release-pipeline






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 10:06







SMPH

















asked Nov 28 '18 at 6:22









SMPHSMPH

73742050




73742050








  • 2





    I don't know about VSTS, but the error seems pretty simple. Your using the value "0" or "1" which is considered a string while 0 1 $true $false are actual boolean values in Powershell (Notice the lack of quotations). Please include your code so we can determine where the error occurs.

    – Shadowzee
    Nov 28 '18 at 6:40








  • 2





    And the code behind it all looks like what?

    – notjustme
    Nov 28 '18 at 6:41














  • 2





    I don't know about VSTS, but the error seems pretty simple. Your using the value "0" or "1" which is considered a string while 0 1 $true $false are actual boolean values in Powershell (Notice the lack of quotations). Please include your code so we can determine where the error occurs.

    – Shadowzee
    Nov 28 '18 at 6:40








  • 2





    And the code behind it all looks like what?

    – notjustme
    Nov 28 '18 at 6:41








2




2





I don't know about VSTS, but the error seems pretty simple. Your using the value "0" or "1" which is considered a string while 0 1 $true $false are actual boolean values in Powershell (Notice the lack of quotations). Please include your code so we can determine where the error occurs.

– Shadowzee
Nov 28 '18 at 6:40







I don't know about VSTS, but the error seems pretty simple. Your using the value "0" or "1" which is considered a string while 0 1 $true $false are actual boolean values in Powershell (Notice the lack of quotations). Please include your code so we can determine where the error occurs.

– Shadowzee
Nov 28 '18 at 6:40






2




2





And the code behind it all looks like what?

– notjustme
Nov 28 '18 at 6:41





And the code behind it all looks like what?

– notjustme
Nov 28 '18 at 6:41












2 Answers
2






active

oldest

votes


















0














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
}





share|improve this answer
























  • I've always used .net Boolean TryParse. Does that not cover your normal use cases?

    – SheldonH
    Feb 9 at 16:58






  • 1





    No. TryParse does not recognise the strings 1 and 0 as true and false values. It only wants to convert the string values true and false into boolean.

    – daughey
    Feb 10 at 21:11



















0














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
},





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513316%2fpass-bool-param-from-vsts-to-powershell-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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
    }





    share|improve this answer
























    • I've always used .net Boolean TryParse. Does that not cover your normal use cases?

      – SheldonH
      Feb 9 at 16:58






    • 1





      No. TryParse does not recognise the strings 1 and 0 as true and false values. It only wants to convert the string values true and false into boolean.

      – daughey
      Feb 10 at 21:11
















    0














    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
    }





    share|improve this answer
























    • I've always used .net Boolean TryParse. Does that not cover your normal use cases?

      – SheldonH
      Feb 9 at 16:58






    • 1





      No. TryParse does not recognise the strings 1 and 0 as true and false values. It only wants to convert the string values true and false into boolean.

      – daughey
      Feb 10 at 21:11














    0












    0








    0







    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
    }





    share|improve this answer













    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
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 28 '18 at 14:12









    daugheydaughey

    43039




    43039













    • I've always used .net Boolean TryParse. Does that not cover your normal use cases?

      – SheldonH
      Feb 9 at 16:58






    • 1





      No. TryParse does not recognise the strings 1 and 0 as true and false values. It only wants to convert the string values true and false into boolean.

      – daughey
      Feb 10 at 21:11



















    • I've always used .net Boolean TryParse. Does that not cover your normal use cases?

      – SheldonH
      Feb 9 at 16:58






    • 1





      No. TryParse does not recognise the strings 1 and 0 as true and false values. It only wants to convert the string values true and false into boolean.

      – daughey
      Feb 10 at 21:11

















    I've always used .net Boolean TryParse. Does that not cover your normal use cases?

    – SheldonH
    Feb 9 at 16:58





    I've always used .net Boolean TryParse. Does that not cover your normal use cases?

    – SheldonH
    Feb 9 at 16:58




    1




    1





    No. TryParse does not recognise the strings 1 and 0 as true and false values. It only wants to convert the string values true and false into boolean.

    – daughey
    Feb 10 at 21:11





    No. TryParse does not recognise the strings 1 and 0 as true and false values. It only wants to convert the string values true and false into boolean.

    – daughey
    Feb 10 at 21:11













    0














    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
    },





    share|improve this answer






























      0














      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
      },





      share|improve this answer




























        0












        0








        0







        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
        },





        share|improve this answer















        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
        },






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 29 '18 at 10:09

























        answered Nov 29 '18 at 10:04









        SMPHSMPH

        73742050




        73742050






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513316%2fpass-bool-param-from-vsts-to-powershell-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Futebolista

            Jornalista