Effectively my problem comes down to this:
I can\'t have a function with a mandatory parameter and pass $null to that parameter:
Function Foo
{
P
You should be able to use the [AllowEmptyString()] parameter attribute for [string] parameters and/or the [AllowNull()] parameter attribute for other types. I've added the [AllowEmptyString()] attribute to the function from your example:
Function Foo {
Param
(
[Parameter(Mandatory = $true)]
[AllowEmptyString()] <#-- Add this #>
[string] $Bar
)
Write-Host $Bar
}
Foo -Bar $null
For more info, check out the about_Functions_Advanced_Parameters help topic.
Be aware that PowerShell will coerce a $null value into an instance of some types during parameter binding for mandatory parameters, e.g., [string] $null
becomes an empty string and [int] $null
becomes 0. To get around that you have a few options: