0

This is the content of the my_script.ps1 file:

function InitDotnet {
    param (
        [Parameter(Mandatory)]
        [string]
        $ProjectName,

        [Parameter(Mandatory)]
        [ValidateSet("cli", "lib")]
        [string]
        $ProjectType,

        [Parameter]
        [bool]
        $Xunit = $false,

        [Parameter]
        [bool]
        $Src = $false
    )

    Write-Host "Name:" $ProjectName
    Write-Host "Type:" $ProjectType
    Write-Host "Create a xunit project:" $Xunit
    Write-Host "Create a src folder:" $Src
}

InitDotnet $args[0] $args[1] $args[2] $args[3]

First, I don't know why the default bool values don't work.

.\my_script.ps1 Test lib

Name: Test
Type: lib
Needs xUnit:
Create a src folder:

And I can't pass Boolean arguments to my .ps1 script without casting errors.

.\my_script.ps1 Test lib $true

InitDotnet : Cannot process argument transformation on parameter 'Xunit'.
Cannot convert value "True" to type "System.Management.Automation.ParameterAttribute".
Error: "Invalid cast from 'System.Boolean' to 'System.Management.Automation.ParameterAttribute'."

What am I doing wrong?

0lan
  • 179
  • 1
  • 14
  • Use the `[Switch]` parameter, see also: [How to pass a switch parameter to another PowerShell script?](https://stackoverflow.com/q/38009106/1701026) – iRon Jan 01 '20 at 10:57
  • I tried the [switch] parameter but it still doesn't use the default value. I tried using the semicolon syntax too without any success. – 0lan Jan 01 '20 at 11:15
  • 4
    if you look at your code in the ISE you will note the `[Parameter]` items for the two booleans are hilited because they lack the `()` that should be there. add them and the error will go away. [*grin*] – Lee_Dailey Jan 01 '20 at 11:33
  • 2
    Thanks Lee! It's crazy that vscode with the powershell extension didn't alert me about it! – 0lan Jan 01 '20 at 11:41
  • you are quite welcome! glad to help a bit ... [*grin*] – Lee_Dailey Jan 01 '20 at 15:44

0 Answers0