Powershell[ValidateSet()]在使用相同参数名的不同参数集之间
好的,我正在尝试编写一个高级函数,它使用两个不同的参数集名称,一个是Powershell[ValidateSet()]在使用相同参数名的不同参数集之间,powershell,validateset,Powershell,Validateset,好的,我正在尝试编写一个高级函数,它使用两个不同的参数集名称,一个是Default,另一个是testaccountly 大多数方法都很有效,但我的问题是: Get Help New secondary account的输出在SYNTAX部分提供了以下信息: SYNTAX New-SecondaryAccount [-Name] <String> [-AccountType] <String> [-Password] <String> [-Descrip
Default
,另一个是testaccountly
大多数方法都很有效,但我的问题是:
Get Help New secondary account
的输出在SYNTAX
部分提供了以下信息:
SYNTAX
New-SecondaryAccount [-Name] <String> [-AccountType] <String> [-Password] <String> [-Description] <String> [-OwnerEmployeeID] <String>
[[-AdditionalDescription]] [<CommonParameters>]
New-SecondaryAccount [-Name] <String> [-AccountType] <String> [-Password] <String> [-CoreOrReserved] <String> [-Description] <String>
[-OwnerEmployeeID] <String> [[-AdditionalDescription]] [<CommonParameters>]
不幸的是,每个参数不能声明多个validate set属性,这就是为什么它的指定是独立的 您可能可以使用动态参数来获得所需的内容。为了清晰起见,我删掉了很多东西
function New-SecondaryAccount() {
[cmdletBinding()]
param (
[Parameter(Mandatory,
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[string] $Name,
[Parameter(Mandatory, Position = 1)]
[string] $Password,
[Parameter(Position = 2)]
[switch] $TestAccount
)
DynamicParam {
$attribute = New-Object System.Management.Automation.ParameterAttribute
$attribute.Mandatory = $true
$collection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$collection.Add($attribute)
if ($TestAccount) {
$validationSet = @("Test")
} else {
$validationSet = @("ADAdmin", "ServerAdmin", "ServiceAccount", "ChuckNorris")
}
$collection.Add((New-Object System.Management.Automation.ValidateSetAttribute($validationSet)))
$param = New-Object System.Management.Automation.RuntimeDefinedParameter('AccountType', [string], $collection)
$dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$dictionary.Add('AccountType', $param)
return $dictionary
}
PROCESS {
<# implementation doing all the things here #>
}
}
function New-secondarycount(){
[cmdletBinding()]
param(
[参数(强制性,
位置=0,
价值来自管道,
值fromPipelineByPropertyName)]
[字符串]$Name,
[参数(必需,位置=1)]
[字符串]$Password,
[参数(位置=2)]
[开关]$TestAccount
)
动态存储器{
$attribute=新对象System.Management.Automation.ParameterAttribute
$attribute.Mandatory=$true
$collection=新对象System.Collections.ObjectModel.collection[System.Attribute]
$collection.Add($attribute)
如果($TestAccount){
$validationSet=@(“测试”)
}否则{
$validationSet=@(“ADAdmin”、“ServerAdmin”、“ServiceAccount”、“ChuckNorris”)
}
$collection.Add((新对象系统、管理、自动化、ValidateStatAttribute($validationSet)))
$param=新对象System.Management.Automation.RuntimeDefinedParameter('AccountType',[string],$collection)
$dictionary=新对象System.Management.Automation.RuntimeDefinedParameterDictionary
$dictionary.Add('AccountType',$param)
返回$dictionary
}
过程{
}
}
考虑使用一个单独的参数(可能是一个[switch]
)来控制该逻辑。不幸的是@srstate这只会在控制台上提示输入AccountType,而且,作为验证集的一部分,通过Intellisense没有可用的值…我只是将-AccountType
和-TestAccount
分离到单独的参数集名称中。不过,学习dynamicparams非常酷,非常感谢
function New-SecondaryAccount() {
[cmdletBinding()]
param (
[Parameter(Mandatory,
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[string] $Name,
[Parameter(Mandatory, Position = 1)]
[string] $Password,
[Parameter(Position = 2)]
[switch] $TestAccount
)
DynamicParam {
$attribute = New-Object System.Management.Automation.ParameterAttribute
$attribute.Mandatory = $true
$collection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$collection.Add($attribute)
if ($TestAccount) {
$validationSet = @("Test")
} else {
$validationSet = @("ADAdmin", "ServerAdmin", "ServiceAccount", "ChuckNorris")
}
$collection.Add((New-Object System.Management.Automation.ValidateSetAttribute($validationSet)))
$param = New-Object System.Management.Automation.RuntimeDefinedParameter('AccountType', [string], $collection)
$dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$dictionary.Add('AccountType', $param)
return $dictionary
}
PROCESS {
<# implementation doing all the things here #>
}
}