Last active
August 9, 2017 00:06
-
-
Save jhg03a/42a13d224002cc25b24f6cf56d928597 to your computer and use it in GitHub Desktop.
Powershell missing int parameter detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function testnull { | |
param([System.Nullable[int]] $testint) | |
if($testint){ | |
Write-Host "Value was found to be null implicitly" | |
} else { | |
Write-Host "Value was found to not be null implicitly" | |
} | |
if($testint -eq $null){ | |
Write-Host "Value was found to be null by explicit null comparison" | |
} else { | |
Write-Host "Value was found to not be null by explicit null comparison" | |
} | |
if(($testint -eq $null) -and ($testint -ne 0)){ | |
Write-Host "Value was found to be null by explicit compound null comparison" | |
} else { | |
Write-Host "Value was found to not be null by explicit compound null comparison" | |
} | |
Write-Host "Value was found to be `"$testint`"" | |
return | |
} | |
Clear-Host | |
Write-Host "Missing Param case" | |
testnull | |
Start-Sleep -Seconds 2 | Out-Null | |
Write-Host "Zero value case" | |
testnull -testint 0 | |
Start-Sleep -Seconds 2 | Out-Null | |
Write-Host "Non-zero value case" | |
testnull -testint 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Missing Param case
Value was found to not be null implicitly
Value was found to be null by explicit null comparison
Value was found to be null by explicit compound null comparison
Value was found to be ""
Zero value case
Value was found to not be null implicitly
Value was found to not be null by explicit null comparison
Value was found to not be null by explicit compound null comparison
Value was found to be "0"
Non-zero value case
Value was found to be null implicitly
Value was found to not be null by explicit null comparison
Value was found to not be null by explicit compound null comparison
Value was found to be "2"