[英]Use ScriptBlock as determination for Mandatory
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. 我希望我可以设置一个cmdlet,以便它可以使用环境变量作为参数值(如果存在),或以其他方式提示。
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. 不幸的是,似乎无论我是否设置了
$Env:TEST_PARAM
,cmdlet总是会提示$ 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. 我可以重新使用验证来使用
[ValidateScript({ #snip #})
,但是如果$Env:TEST_PARAM
不存在,我就不会再获得Powershell提示所需的值。 I would simply get a validation error. 我只是得到一个验证错误。
So 2 questions here 所以这里有两个问题
For the second question, you can always validate the value within the script. 对于第二个问题,您始终可以验证脚本中的值。 Just set the value to the environment variable as the default value.
只需将值设置为环境变量作为默认值。 When I tried the validation with
($foo -eq $null)
it didn't work, so I switched it to ($foo -eq "")
. 当我尝试使用
($foo -eq $null)
进行验证时,它不起作用,所以我将其切换为($foo -eq "")
。 Here is a sample I tested to get what you were asking for: 这是我测试的样本,以获得您的要求:
function Test-Mandatory
{
[CmdletBinding()]
param(
[string] $foo = $Env:TEST_PARAM
)
begin {
if ($foo -eq "") {
$foo = Read-Host "Please enter foo: "
}
} #end begin
process {
Write-Host $foo
} #end process
} #end function
As for the mandatory question, I believe if you assign a default value (even if it is empty), it will satisfy the mandatory assignment. 至于强制性问题,我相信如果你指定一个默认值(即使它是空的),它将满足强制性赋值。 When it goes to check if the value is present, since it was assigned a value, the mandatory checks true, allowing it to move on.
当它检查值是否存在时,由于它被赋值,强制检查为true,允许它继续。
Of note, this doesn't work as you would expect... I would expect $foo to be marked as mandatory only if $Env:TEST_PARAM does not exist . 值得注意的是,这并不像你期望的那样工作......我希望只有当$ Env:TEST_PARAM不存在时才会将$ foo标记为强制。
However, even when $Env:TEST_PARAM exists, the shell prompts :( 但是,即使$ Env:TEST_PARAM存在,shell也会提示:(
function Test-Mandatory
{
[CmdletBinding()]
param(
[Parameter(Mandatory = { [string]::IsNullOrEmpty($Env:TEST_PARAM) })]
[string]
$foo
)
if (!$PsBoundParameters.foo) { $foo = $Env:TEST_PARAM }
Write-Host $foo
}
FYI, here's what Microsoft had to say about it: 仅供参考,这就是微软对此所说的话:
"It's not really a bug. Script blocks are valid attribute arguments – eg ValidateScriptBlock wouldn't work very well otherwise.
“这不是一个真正的错误。脚本块是有效的属性参数 - 例如,ValidateScriptBlock不会很好地工作。
Attribute arguments are always converted to the parameter type.属性参数始终转换为参数类型。 In the case of Mandatory – it takes a bool, and any time you convert a ScriptBlock to bool, you'll get the value $true.
在Mandatory的情况下 - 它需要一个bool,并且每当你将ScriptBlock转换为bool时,你将获得值$ true。 You never invoke a script block to do a conversion."
你永远不会调用脚本块来进行转换。“
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.