gpt4 book ai didi

powershell - 在 ValidateSet 中查找值

转载 作者:行者123 更新时间:2023-12-04 17:07:26 24 4
gpt4 key购买 nike

我想知道是否有办法检索子句 Param() 中使用的值。为 ValidateSet .像这样的事情会很棒:

Function Foo {
Param (
[ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
[String]$Type = 'Startup'
)

$Type.ValidateSet
}

当然, Type 上没有这样的属性。目的。是否可以检索 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 call 也适用于函数。

关于powershell - 在 ValidateSet 中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42697072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com