0

I need to write a function in powershell that tells apart a 'parameter not being passed' from one passed with string empty (or any other string)

I wrote it like this:

function Set-X {
    param(
    [AllowNull()][string]$MyParam = [System.Management.Automation.Language.NullString]::Value
    )

    if ($null -ne $MyParam) { write-host 'oops' }
    else { write-host 'ok' }
}

If I call Set-X without parameters from ISE, it works as I expect and prints 'ok'.

But if I do that from the normal console, it prints 'oops'.

What is going on? What is the proper way to do it?

6
  • Have you tried looking at [string]::IsNullOrEmpty()? That will allow you to check for a null or empty string. There is also [string]::IsNullOrWhiteSpace() Commented Sep 11, 2017 at 17:21
  • IsNullOrEmpty() does not tell apart null from empty. Commented Sep 11, 2017 at 17:23
  • What do you want to dispay if i type in : Set-X -MyParam "hey" ; Set-X -MyParam ; Set-X Commented Sep 11, 2017 at 17:39
  • I believe the problem is that you are casting your parameter to a [string]. As [string]$null -eq '' returns True vs $null -eq '' returns False Commented Sep 11, 2017 at 17:53
  • @ArcSet, I expect: Set-X -MyParam "hey" -> should display 'oops'; Set-X -> should display 'ok'; Set-X -MyParam -> I didn't think about this case, but since $MyParam is not [switch], I think it should result in some type of error in the function call Commented Sep 11, 2017 at 18:13

2 Answers 2

2

Allowing the user to pass in a parameter argument value of $null does not change the fact that powershell will attempt to convert it to a [string].

Converting a $null value in powershell to a string results in an empty string:

$str = [string]$null
$null -eq $str # False
'' -eq $str    # True

(same goes for $null -as [string] and "$null")

Remove the type constraint on the MyParam parameter if you not only want to allow $null but also accept $null as a parameter value:

function Set-X {
    param(
    [AllowNull()]$MyParam = [System.Management.Automation.Language.NullString]::Value
    )

    if ($null -ne $MyParam) { write-host 'oops' }
    else { write-host 'ok' }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer, as also pointed out by BenH in his comment. But you also have to change the default assignment to simply $null, instead of [System.Management.Automation.Language.NullString]::Value
0

As Mathias and BenH have written, the culprit is casting $null to the [string] type, which results in an empty string:

 [string]$null -eq '' #This is True

But for the sample code in Mathias answer to work correctly we also have to replace

[System.Management.Automation.Language.NullString]::Value

with $null

function Set-X {
    param(
    [AllowNull()]$MyParam = $null
    )

    if ($null -ne $MyParam) { write-host 'oops' }
    else { write-host 'ok' }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.