3

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

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

的输出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{}
4

1 回答 1

4

不幸的是,您不能为每个参数声明一个以上的验证集属性,这也是它的名称分开的原因之一。

您也许可以使用动态参数来获得您想要的东西。为了清楚起见,我删除了很多东西。

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 #>
    }
}
于 2018-04-13T01:07:18.403 回答