无法获取参数集以进行验证

时间:2015-07-03 20:44:54

标签: powershell

我有一个功能,我已经工作了一段时间,但由于让参数集正常工作的一些挫折而搁置了几个月。它旨在成为用于在站点之间传输AD帐户的内部工具包的一部分。有几个参数集需要:

Move-AccountOut -Username <String> [<CommonParameters>]

Move-AccountOut -Username <String> [-RetainGroups] [-TransferMDrive] [-OldServer <String>] [-NewServer <String>]
[<CommonParameters>]

Move-AccountOut -Username <String> [-RetainGroups] [<CommonParameters>]

Move-AccountOut -Username <String> [-RemoveFromAllGroups] [-TransferMDrive] [-OldServer <String>] [-NewServer
<String>] [<CommonParameters>]

Move-AccountOut -Username <String> [-RemoveFromAllGroups] [<CommonParameters>]

Move-AccountOut -Username <String> [-TransferMDrive] -OldServer <String> -NewServer <String> [<CommonParameters>]

基本上,RetainGroupsRemoveFromAllGroups必须互相排斥。 TransferMDrive可以包含在其中任何一个中,也可以在其自身上运行。

现在,如果我添加RetainGroups参数,我只能运行RemoveFromAllGroupsTransferHomeDrive。如果我自己尝试这3个中的任何一个,我会得到以下结果:

Move-AccountOut : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Move-AccountOut -Username testuser -RetainGroups
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Move-AccountOut], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Move-AccountOut

之前我已经发布过有关此功能问题的问题,但我仍然无法正确解析参数集。根据上面的get-help输出,就我所知,它应该可以正常工作。

以下是我param的样子:

[CmdletBinding(DefaultParameterSetName='OnlyUser')]
Param( 
    [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
    [string]$Username,

    [Parameter(ParameterSetName='RetainOnly')]
    [Parameter(ParameterSetName='RetainAndTransfer')]
    [switch]$RetainGroups,

    [Parameter(ParameterSetName='RemoveOnly')]
    [Parameter(ParameterSetName='RemoveAndTransfer')]
    [switch]$RemoveFromAllGroups,

    [Parameter(ParameterSetName='TransferMDrive')]
    [Parameter(ParameterSetName='RetainAndTransfer')]
    [Parameter(ParameterSetName='RemoveAndTransfer')]
    [switch]$TransferMDrive,

    [Parameter(ParameterSetName='TransferMDrive', Mandatory=$True)]
    [Parameter(ParameterSetName='RetainAndTransfer')]
    [Parameter(ParameterSetName='RemoveAndTransfer')]
    [string]$OldServer,

    [Parameter(ParameterSetName='TransferMDrive', Mandatory=$True)]
    [Parameter(ParameterSetName='RetainAndTransfer')]
    [Parameter(ParameterSetName='RemoveAndTransfer')]
    [string]$NewServer
)

非常感谢任何建议!

2 个答案:

答案 0 :(得分:2)

这是您的参数扩展的方式(见下文)。基本上只给出这两个参数(Username和RetainGroups) - 有两个有效的参数集(RetainOnly和RetainAndTransfer),它们都不是default参数集。我认为您需要为两个Transfer参数集强制使用TransferMDrive,例如:

[Parameter(ParameterSetName='TransferMDrive')]
[Parameter(ParameterSetName='RetainAndTransfer', Mandatory = $True)]
[Parameter(ParameterSetName='RemoveAndTransfer', Mandatory = $True)]
[switch]$TransferMDrive,

扩展(原始)参数:

    Command: Function:/Move-AccountOut
    Set:    OnlyUser *


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    RetainAndTransfer


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
NewServer              {N*}         Named    False     False    False  All     
OldServer              {Ol*}        Named    False     False    False  All     
RetainGroups           {Ret*}       Named    False     False    False  All     
TransferMDrive         {T*}         Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    RetainOnly


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
RetainGroups           {Ret*}       Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    RemoveAndTransfer


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
NewServer              {N*}         Named    False     False    False  All     
OldServer              {Ol*}        Named    False     False    False  All     
RemoveFromAllGroups    {Rem*}       Named    False     False    False  All     
TransferMDrive         {T*}         Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    RemoveOnly


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
RemoveFromAllGroups    {Rem*}       Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    TransferMDrive


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
NewServer              {N*}         Named    True      False    False  All     
OldServer              {Ol*}        Named    True      False    False  All     
TransferMDrive         {T*}         Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     

答案 1 :(得分:2)

你可能有点过于复杂了。您正在使用开关来保留或删除组。你能把它们组合成一个开关吗?如果是这样,您可以删除所有其他参数集。

[CmdletBinding()]
Param( 
    [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
    [string]$Username,

    [switch]$RetainGroups,

    [Parameter(ParameterSetName='TransferMDrive')]
    [switch]$TransferMDrive,

    [Parameter(ParameterSetName='TransferMDrive', Mandatory=$True)]
    [string]$OldServer,

    [Parameter(ParameterSetName='TransferMDrive', Mandatory=$True)]
    [string]$NewServer
)

因此默认情况下它不会保留组,您可以设置标志以保留它们。或者如果它需要另一种方式,请翻转它。