Динамический параметр не отображается после использования проверенного набора параметров - PullRequest
0 голосов
/ 02 января 2019

Я играл с моим новым командлетом New-CompanyADUser в нашем модуле Company-AD, и у меня возникла странная проблема с моим динамическим параметром: он вообще не появится после того, как я выбрал опциюв проверенном наборе параметров, который имеет более одного слова.

Например, если я напишу:

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]:

Но тогда завершение табуляции динамического параметра не работает.Кто-нибудь еще сталкивался с этой проблемой раньше?

Мой код выглядит следующим образом (обратите внимание, он начинается вокруг проблемной области; я могу опубликовать полную информацию, если это поможет диагностировать проблему):

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

1 Ответ

0 голосов
/ 03 января 2019

Мне удалось это исправить, поместив параметры ValidateSet в одинарные и двойные кавычки. Затем это позволило мне правильно отобразить динамический параметр groups. Очевидно, это часть некоторой ошибки, записанной здесь: 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

...