Powershell [ValidateSet()] 在使用相同参数名称的单独参数集之间

Powershell [ValidateSet()] Between Separate Parameter Sets Using Same Parameter Name

好的,所以我正在尝试编写一个使用两个不同参数集名称的高级函数,一个是 Default 另一个是 TestAccountsOnly.

其中大部分工作正常,但是,这是我的问题:

Get-Help New-SecondaryAccount 的输出在 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>]

从外观上看,这正是我想要的 - 一个参数集,我可以在其中验证少数不同 -AccountTypes 的列表,并在我有密码、描述等的地方移动,另一个我只验证 AccountType 的一个值并且有一个 CoreOrReserve 参数只属于 TestAccountsOnly 参数集。

不幸的是,当我尝试在 ISE 中对此进行测试时,如果我键入:

New-SecondaryAccount -Name mehSomeAccount -AccountType,我从 IntelliSense 得到的唯一建议是 Test

您能不能按照我尝试的方式使用 [ValidateSet()],还是我只是做错了?

如果有人能指出这一点,我们将不胜感激!

Function New-SecondaryAccount(DefaultParameterSetName="Default")
{
<#
.Synopsis
   Creates a new secondary account based on the parameters
.DESCRIPTION
    Creates a secondary AD user account based on parameters
    specified. This includes several different types of accounts,
    and determines the employeeType, OU, and description values
    of the account created.

    The CoreOrReserved parameter can only be used for accounts
    where AccountType is set to Test
.INPUTS
   [String]
.OUTPUTS
   [ADObject]
.NOTES

.COMPONENT
    MyModule_Part1
.FUNCTIONALITY
   Active Directory Things
#>
    [cmdletBinding(DefaultParameterSetName="Default")]
    param(
        [Parameter(Mandatory=$True,
                    Position=0,
                    ParameterSetName="Default",
                    ValueFromPipeline=$True,
                    ValueFromPipelineByPropertyName=$True)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Parameter(Mandatory=$True,
                    Position=0,
                    ParameterSetName="TestAccountsOnly",
                    ValueFromPipeline=$True,
                    ValueFromPipelineByPropertyName=$True)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [String]$Name,

        [Parameter(Mandatory=$True,
                    Position=1,
                    ParameterSetName="Default")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('ADAdmin','ServerAdmin','ServiceAccount','ChuckNorris')]
        [Parameter(Mandatory=$True,
                    Position=1,
                    ParameterSetName="TestAccountsOnly")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("Test")]
        [String]$AccountType,

        [Parameter(Mandatory=$True,
                    Position=2,
                    ParameterSetName="Default")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateScript(
        {
            if($_.Length -ge 12)
            {
                $True
            }
            else
            {
                throw "Password must be at least 12 characters"
                $False
            }
        })]
        [Parameter(Mandatory=$True,
                    Position=3,
                    ParameterSetName="TestAccountsOnly")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateScript(
        {
            if($_.Length -ge 12)
            {
                $True
            }
            else
            {
                throw "Password must be at least 12 characters"
                $False
            }
        })]
        [String]$Password,

        [Parameter(Mandatory=$True,
                    Position=2,
                    ParameterSetName="TestAccountsOnly")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("Core","Reserved")]
        [String]$CoreOrReserved,

        [Parameter(Mandatory=$True,
                    Position=3,
                    ParameterSetName="Default")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateScript(
        {
            if($_ -match "^TASK\d{7}\b")
            {
                $True
            }
            else
            {
                throw "Description must be a TASK number only`nEx. TASK1234567"
                $False
            }
        })]
        [Parameter(Mandatory=$True,
                    Position=4,
                    ParameterSetName="TestAccountsOnly")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateScript(
        {
            if($_ -match "^TASK\d{7}\b")
            {
                $True
            }
            else
            {
                throw "Description must be a TASK number only`nEx. TASK1234567"
                $False
            }
        })]
        [String]$Description,

        [Parameter(Mandatory=$True,
                    Position=4,
                    ParameterSetName="Default")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateScript(
        {
            if($(Get-ADUser -Filter {EmployeeID -eq $_ -and EmployeeType -eq "E"}) -ne $NULL)
            {
                $True
            }
            else
            {
                throw "$_ must correspond to a valid FTE user's employeeID number"
                $False
            }
        })]
        [Parameter(Mandatory=$True,
                    Position=5,
                    ParameterSetName="TestAccountsOnly")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateScript(
        {
            if($(Get-ADUser -Filter {EmployeeID -eq $_ -and EmployeeType -eq "E"}) -ne $NULL)
            {
                $True
            }
            else
            {
                throw "$_ must correspond to a valid FTE user's employeeID number"
                $False
            }
        })]
        [String]$OwnerEmployeeID,

        [Parameter(Mandatory=$False,
                    ParameterSetName="Default",
                    Position=5)]
        [Parameter(Mandatory=$False,
                    ParameterSetName="TestAccountsOnly",
                    Position=6)]
        [Switch]$AdditionalDescription

    )
    BEGIN{}
    PROCESS{# implementation doing all the things here}
    END{}

遗憾的是,您不能为每个参数声明一个以上的验证集属性,这是单独指定其名称的原因之一。

您或许可以尝试使用动态参数来获得您想要的结果。为了清楚起见,我删除了很多内容。

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 #>
    }
}