共用方式為


The difference between [Switch] and[Bool] in Powershell function parameters

#switch parameter cls function MyAwesomeFunction1 { [CMDLetBinding()] param ( [string] $foo, [string] $bar, [switch] $someVariable ) Write-Host "someVariable = $someVariable" + $someVariable.GetType() if($someVariable) { Write-Host $foo } else { Write-Host $bar } } #boolean parameter function MyAwesomeFunction2 { [CMDLetBinding()] param ( [string] $foo, [string] $bar, [bool] $someVariable ) Write-Host "someVariable = $someVariable" + $someVariable.GetType() if($someVariable) { Write-Host $foo } else { Write-Host $bar } } MyAwesomeFunction1 -foo "foo" -bar "bar" -someVariable:$true MyAwesomeFunction1 -foo "foo" -bar "bar" -someVariable:$false MyAwesomeFunction1 -foo "foo" -bar "bar" -someVariable MyAwesomeFunction1 -foo "foo" -bar "bar" MyAwesomeFunction2 -foo "foo" -bar "bar" -someVariable $true MyAwesomeFunction2 -foo "foo" -bar "bar" -someVariable $false MyAwesomeFunction2 -foo "foo" -bar "bar" -someVariable MyAwesomeFunction2 -foo "foo" -bar "bar"

Comments

  • Anonymous
    June 21, 2017
    Same result for both methods despite they are of different type...
    • Anonymous
      June 21, 2017
      Very similar. But did you notice the : next to the parameters for switch call? :) That is the main difference for the consumer for Switch parameter.
      • Anonymous
        June 21, 2017
        The comment has been removed
        • Anonymous
          June 21, 2017
          The comment has been removed
  • Anonymous
    March 14, 2018
    Nicely demonstrated Bulent.