我一直在 Company-AD 模块中使用新的 New-CompanyADUser 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'
但是,如果我为“Floor”选择另一个包含多个单词的选项,则无法调用它,例如:
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 键,它会说:
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
}