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

前端 未结 10 2902
逝去的感伤
逝去的感伤 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:18

    This is an older question, but there is actually an answer to this in the PowerShell documentation. I had the same problem, and for once RTFM actually solved it. Almost.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe

    Documentation for the -File parameter states that "In rare cases, you might need to provide a Boolean value for a switch parameter. To provide a Boolean value for a switch parameter in the value of the File parameter, enclose the parameter name and value in curly braces, such as the following: -File .\Get-Script.ps1 {-All:$False}"

    I had to write it like this:

    PowerShell.Exe -File MyFile.ps1 {-SomeBoolParameter:False}
    

    So no '$' before the true/false statement, and that worked for me, on PowerShell 4.0

提交回复
热议问题