I've been playing about with my new New-CompanyADUser cmdlet in our Company-AD module, and I'm experiencing a weird problem with my dynamic parameter: it won't appear at all after I've chosen an option within a validated set parameter that has more than one word.
For example, if I write:
New-CompanyUser -UKUser -FirstName Test -LastName User -Title "Office Admin" -Manager StackOverflow -Floor Annex
I can then continue using the 'Groups' parameter, which automatically pulls the information from AD, like:
New-CompanyUser -UKUser -FirstName Test -LastName User -Title "Office Admin" -Manager StackOverflow -Floor Annex -Groups Accounts,'Office Admins','26B Street Name'
But if I select another option for 'Floor' that has more than one word, I can't call it, like:
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)
Now, if I just press Enter without the groups parameter, it will say:
cmdlet New-CompanyADUser at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Groups[0]:
But then the tab completion of the dynamic parameter doesn't work. Has anyone else experienced this problem before?
My code is as follows (note, it begins around the problematic area; I can post the full thing if it helps ti diagnose the issue):
[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
}