I was hoping that I could setup a cmdlet so that it would use an environment variable for a parameter value if it exists, or otherwise prompt.
function Test-Mandatory
{
[CmdletBinding()]
param(
[Parameter(Mandatory = { [string]::IsNullOrEmpty($Env:TEST_PARAM) })]
[string]
$foo = $Env:TEST_PARAM
)
Write-Host $foo
}
Unfortunately, it seems that regardless of whether or not I have a $Env:TEST_PARAM set, the cmdlet always prompts for $foo.
I could rework the validation to use [ValidateScript({ #snip #}), but then I wouldn’t get Powershell prompting for the required value anymore, should $Env:TEST_PARAM not exist. I would simply get a validation error.
So 2 questions here
- Why can I even assign a scriptblock to Mandatory if it doesn’t appear to be honored?
- Is there a simple way to get the default PS prompting behavior given the criteria I specified?
FYI, here’s what Microsoft had to say about it: