How to pass boolean values to a PowerShell script from a command prompt

前端 未结 10 2831
逝去的感伤
逝去的感伤 2020-12-08 12:46

I have to invoke a PowerShell script from a batch file. One of the arguments to the script is a boolean value:

C:\\Windows\\System32\\WindowsPowerShell\\v1.0         


        
10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 13:17

    Try setting the type of your parameter to [bool]:

    param
    (
        [int]$Turn = 0
        [bool]$Unity = $false
    )
    
    switch ($Unity)
    {
        $true { "That was true."; break }
        default { "Whatever it was, it wasn't true."; break }
    }
    

    This example defaults $Unity to $false if no input is provided.

    Usage

    .\RunScript.ps1 -Turn 1 -Unity $false
    

提交回复
热议问题