Skip to content

Instantly share code, notes, and snippets.

@jhg03a
Last active August 9, 2017 00:06
Show Gist options
  • Save jhg03a/42a13d224002cc25b24f6cf56d928597 to your computer and use it in GitHub Desktop.
Save jhg03a/42a13d224002cc25b24f6cf56d928597 to your computer and use it in GitHub Desktop.
Powershell missing int parameter detection
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
@jhg03a
Copy link
Author

jhg03a commented Aug 9, 2017

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment