使用已验证的集参数后不显示动态参数



我一直在公司-AD 模块中玩我的新公司 ADUser cmdlet,我的动态参数遇到了一个奇怪的问题:在具有多个单词的已验证集参数中选择一个选项后,它根本不会出现。

例如,如果我写:

New-CompanyUser -UKUser -FirstName Test -LastName User -Title "Office Admin" -Manager StackOverflow -Floor Annex    

然后,我可以继续使用"组"参数,该参数会自动从AD中提取信息,例如:

New-CompanyUser -UKUser -FirstName Test -LastName User -Title "Office Admin" -Manager StackOverflow -Floor Annex -Groups Accounts,'Office Admins','26B Street Name'    

但是,如果我为"楼层"选择另一个包含多个单词的选项,则无法调用它,例如:

New-CompanyUser -UKUser -FirstName Test -LastName User -Title "Office Admin" -Manager StackOverflow -Floor 'Second Floor' -(Groups should appear as you tab through, but it doesn't)    

现在,如果我只按 Enter 而不按 groups 参数,它会说:

cmdlet New-CompanyADUser at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Groups[0]:

但是,动态参数的 Tab 补全不起作用。以前有没有其他人遇到过这个问题?

我的代码如下(注意,它从问题区域开始;如果它有助于诊断问题,我可以发布完整的内容):

[Parameter(ParameterSetName = "UK User", Position = 7,
HelpMessage = "Please enter the phone number of the new user.")]
[Parameter(ParameterSetName = "US User", Position = 7,
HelpMessage = "Please enter the phone number of the new user.")]
[Parameter(ParameterSetName = "Australian User", Position = 7,
HelpMessage = "Please enter the phone number of the new user.")]
[Parameter(ParameterSetName = "Remote User", Position = 7,
HelpMessage = "Please enter the phone number of the new user.")]
[ValidateNotNullOrEmpty()]
[string]$Phone,
[Parameter(ParameterSetName = "UK User", Position = 8, Mandatory = $True,
HelpMessage = "Please choose which floor the user will be working on.")]
[ValidateNotNullorEmpty()]
[ValidateSet(
    "Annex",
    "Second Floor",
    "Third Floor")]
[String]$Floor
)
DynamicParam{
# Set the dynamic parameters' name.
$ParameterName = 'Groups'
 # Create the dictionary 
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Create the collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes. You may also want to change these.
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $True
$ParameterAttribute.Position = 9
$ParameterAttribute.HelpMessage = "Please select the groups you want the user to be a member of."
# Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute)
# Generate and set the ValidateSet. You definitely want to change this. This part populates your set. 
Get-ADGroup -SearchBase 'OU=Company Groups,DC=Company,DC=Co,DC=UK' -Filter * | Select-Object $_.SamAccountName | 
Foreach{
    [array]$arrSet += $_.SamAccountName
}
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
# Add the ValidateSet to the attributes collection
$AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [array], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary
}

我设法通过将 ValidateSet 选项放在单引号和双引号中来解决此问题。然后,这使我能够正确显示组动态参数。显然,这是此处记录的一些错误的一部分:https://social.technet.microsoft.com/Forums/lync/en-US/91d85311-f1a6-4c21-ad9d-f909c468bea2/validateset-and-tab-completion-does-not-work-on-strings-with-spaces?forum=winserverpowershell

相关内容

  • 没有找到相关文章

最新更新