I have a question about cmdlets parameters.
Let say I have a function like this
function Do-Something
{
param (
[Parameter(Mandatory=$false)] $fParam
)
get-result -clParam $fParam
}
Then I call this function like this
$var = "somedata"
Do-Something -fParam $var
Code executed without erros (if $var has the appropriate data type, of course)
When I call function like this
$var = $null #or Variable var does not defined at all
Do-Something -fParam $var
I gets error, something about “incorrect data type for -clParam” or “-clParam can’t be empty” and so on.
To avoid such an errors as far as I understand my function should test if -fParam is empty or not:
function Do-Something
{
param (
[Parameter(Mandatory=$false)] $fParam
)
if($fParam -ne $Null)
{
get-result -clParam $fParam #if fParam is defined, run cmdlet with param
}
else
{
get-result #if fParam isn't define or $Null, run cmdlet without param
}
}
My question is: Is it possible to avoid such bulky structures? Is there a way to force the cmdlet to independently determine that an empty variable was passed to the parameter and therefore be executed without the parameter?