Powershell: How do I pass variables to switch parameters when invoking powershell at the command line?

后端 未结 2 1542
暗喜
暗喜 2021-02-02 08:08

Normally, if you want to defer the specification of a switch parameter to some variable, you can pass an expression to the switch parameter, as seen with the WhatIf parameter.

2条回答
  •  心在旅途
    2021-02-02 08:36

    Use the IsPresent property of the switch. Example:

    function test-switch{
    param([switch]$test)
      function inner{
        param([switch]$inner_test)
        write-host $inner_test
      }
      inner -inner_test:$test.IsPresent
    }
    test-switch -test:$true
    test-switch -test
    test-switch -test:$false
    
    True
    True
    False
    

    BTW, I used functions rather than a script so it would be easier to test.

提交回复
热议问题