2015-07-03 16 views
1

我有一段時間以來一直在工作的功能,但由於讓參數集正常工作而遭受一些挫折,因此擱置了幾個月。它旨在成爲用於在站點間傳輸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可以包含在其中任何一箇中,也可以獨立運行。

現在,我只能運行RetainGroupsRemoveFromAllGroups,如果我包含TransferHomeDrive參數。如果我嘗試對自己的任何這些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

這就是你的參數展開的方式(見下文)。基本上只給出這兩個參數(用戶名和保留組) - 有兩個有效的參數集(RetainOnly和RetainAndTransfer),這兩個參數集都不是default參數集。我認爲你需要做TransferMDrive強制兩個轉移parametersets例如爲:

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

向外擴張(原件)parametersets:

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  
+0

謝謝!我比過去幾個月還要進步。我的6個參數集中有5個正在工作,唯一沒有的是'Move-AccountOut -Username [-TransferMDrive] -OldServer -NewServer []'。嘗試製作'TransferMDrive'是強制性的,但這並沒有幫助。有什麼建議麼? – RyanL

+1

根據您的參數設計,如果您指定-TransferMDrive以及-Old/NewServer,則PowerShell可將其縮小到TransferMDrive參數集。但是,如果未指定-TransferMDrive,給定params -Username,-OldServer和-NewServer,則PowerShell無法在RetainAndTransfer和RemoveAndTransfer參數集之間消除歧義。我認爲你需要'[參數(ParameterSetName ='RetainAndTransfer',強制= $真)] [轉] $ RetainGroups'。 RemoveGroups參數的同上。 –

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 
) 

所以默認情況下它不會保留組,您可以設置標誌來保留它們。或者如果需要以其他方式,請翻轉它。

+0

我完全同意。那是我最初的設計。然而我改變了它,因爲在我的腳本中有更多的代碼會在沒有指定參數的情況下確認每個組的刪除。使用其中一個保留/刪除開關可以繞過該操作,並自動採取正確的操作。**但是,**現在我正在考慮這個問題,是否會增加另一個開關來觸發確認是更好的做法?就像,我可以翻轉你的例子,並添加一個'-PromptForConfirmation'開關。 – RyanL