Reputation: 17014
I have a mandatory parameter like:
[Parameter(Mandatory=$true)]$foo
I do want PowerShell to prompt to supply one if the user failed to do so. However, I would like to be able to supply a variable; that is if $bar
is supplied, it does not bind it as $foo = "$bar"
.
Upvotes: 4
Views: 212
Reputation: 59031
You could use the ExpandString
function to expand that variable like:
function Test-Param
{
Param(
[Parameter(Mandatory=$true)]$foo
)
$foo = $ExecutionContext.InvokeCommand.ExpandString($foo)
Write-Host $foo
}
You can test it using:
$bar = "hello"
Test-Param
Now just pass $bar
to the prompt and you will get hello
printed.
Upvotes: 4