samedi 18 avril 2015

PowerShell - Empty parameter is not Null in function

Given this basic function:



Function TestFunction {
Param ( [int]$Par1, [string]$Par2, [string]$Par3 )
If ($Par1 -ne $Null) { Write-Output "Par1 = $Par1" }
If ($Par2 -ne $Null -or $Par2 -ne '') { Write-Output "Par2 = $Par2" }
If ($Par3 -ne $Null) { Write-Output "Par3 = $Par3" }
}
TestFunction -Par1 1 -Par3 'par3'


...the output is:



Par1 = 1
Par2 =
Par3 = par3


Even though I didn't pass anything into the Par2-variable, it still isn't Null or empty. What happened, and how can I rewrite the statement so that the second If-statement evaluates as False and the script-block does not get executed?


(I added the "-or $Par2 -ne ''" just to test, it behaves the same with and without it.)


Aucun commentaire:

Enregistrer un commentaire