Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

PowerShell IsPresent incorrectly reporting false for explicitly specified switch parameter

$
0
0

Why are PowerShell versions 3.0 and 4.0 unreliable when it comes to reporting the correct IsPresent status of a switch parameter?

Here's an example that illustrates the problem that IsPresent is reported as False when clearly it is present:

function TestMe {
param(
[Parameter(Mandatory=$false)]
[switch] $Hidden,
[Parameter(Mandatory=$false)]
[bool] $Hide=$false
)
Write-Verbose 'START----------------------'
Write-Verbose ('Hidden.IsPresent={0}' -f $Hidden.IsPresent)
Write-Verbose ('Hidden={0}' -f $Hidden.ToBool())
Write-Verbose ('Hide={0}' -f $Hide)
Write-Verbose 'END----------------------'
}
Clear-Host
TestMe -Hidden -Verbose
# Why does the following indicate IsPresent=false when clearly it should indicate IsPresent=true ?
TestMe -Hidden:$False -Verbose
TestMe -Verbose
TestMe -Hide $true -Verbose

The output from the above is as follows.  Pay close attention to the PowerShell bug shown in the output from the second call to TestMe:

VERBOSE: START----------------------
VERBOSE: Hidden.IsPresent=True
VERBOSE: Hidden=True
VERBOSE: Hide=False
VERBOSE: END----------------------
VERBOSE: START----------------------
VERBOSE: Hidden.IsPresent=False <--- THIS IS THE BUG IN POWERSHELL 3.0 and 4.0
VERBOSE: Hidden=False
VERBOSE: Hide=False
VERBOSE: END----------------------
VERBOSE: START----------------------
VERBOSE: Hidden.IsPresent=False  
VERBOSE: Hidden=False
VERBOSE: Hide=False
VERBOSE: END----------------------
VERBOSE: START----------------------
VERBOSE: Hidden.IsPresent=False
VERBOSE: Hidden=False
VERBOSE: Hide=True
VERBOSE: END----------------------



If you think development is tough now, think how hard it was using punched cards :-)



Viewing all articles
Browse latest Browse all 15028

Trending Articles



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