Hi all! Having a weird problem and have spent a lot of time troubleshooting with no luck, so I’m hoping someone can assist. Here’s the deal:
Trying to include a dynamic parameter in my function to allow my users to select from a validated list of OU distinguished names given a previously-input $Domain parameter.
The problem is that distinguished names have commas in them, and that’s causing errors after I tab-complete the OU DN and hit Enter. (The actual dynamic variable list works fine… lists the distinguished names correctly etc.)
If I tab-complete the OU DN, then go back and manually add single or double quotes around that value, it works as intended.
If I add
| foreach {“‘$_’”}to the end of the line defining the $arrSet variable, the validated list shows up with single quotes around each DN, but when I hit Enter it fails, telling me that the value (with single quotes) does not belong to the set (shows me a set with the exact value with quotes). So that seems weird, because it’s saying ‘A’ isn’t part of set “‘A’,‘B’,‘C’”
So I’m out of ideas. Anybody have anything else?
Code below is a functional snippet of the much larger script, and throws the same errors I’m seeing. To make it work in your domain, you’ll need to modify
[ValidateSet(‘mydomain.local’)]and
-SearchBase ‘DC=mydomain,DC=local’
Thanks!
function Thing { [CmdletBinding()] Param( [Parameter(Mandatory=$false,Position=1,ValuefromPipeline=$true,ValueFromPipelineByPropertyName=$true,HelpMessage='Please enter a valid domain name.')] [ValidateSet('mydomain.local')] [string] $Domain ) DynamicParam { # Set the dynamic parameters' name $ParameterName = 'OrganizationalUnit' # 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 $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $true $ParameterAttribute.Position = 2 # Add the attributes to the attributes collection $AttributeCollection.Add($ParameterAttribute) # Generate and set the ValidateSet $arrSet = (Get-ADOrganizationalUnit -Filter * -SearchBase 'DC=mydomain,DC=local' -SearchScope OneLevel -Server $Domain).DistinguishedName $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, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) return $RuntimeParameterDictionary } begin { # Bind the parameter to a friendly variable $OrganizationalUnit = $PsBoundParameters[$ParameterName] } process { # Your code goes here Write-Output "Domain: $Domain" Write-Output "" Write-Output "Selected OU: $OrganizationalUnit" Write-Output "" Write-Output "OU List: $arrset" } }