Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

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

$
0
0

#switch parameter
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

MyAwesomeFunction2 -foo "foo" -bar "bar" -someVariable $true
MyAwesomeFunction2 -foo "foo" -bar "bar" -someVariable $false


Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>