本文介绍了在 ValidateSet 中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有办法为 ValidateSet
检索子句 Param()
中使用的值.像这样的东西会很棒:
I was wondering if there was a way to retrieve the values used in the clause Param()
for ValidateSet
. Something like this would be great:
Function Foo {
Param (
[ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
[String]$Type = 'Startup'
)
$Type.ValidateSet
}
当然,Type
对象上没有这样的属性.是否可以检索 ValidateSet
中设置的值?
But of course there is no such property on the Type
object. Is it possible to retrieve the values set in ValidateSet
?
推荐答案
function Foo {
param (
[ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
[String]$Type = 'Startup'
)
$ParameterList = (Get-Command -Name $MyInvocation.MyCommand).Parameters
$ParameterList["Type"].Attributes.ValidValues
}
发表评论后:
param (
[ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
[String]$Type = 'Startup'
)
(Get-Variable "Type").Attributes.ValidValues
Get-Variable
调用也适用于函数.
这篇关于在 ValidateSet 中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!