Hi!
Recently I ran into the problem with validate set and strings containing spaces.
Found this great post by Rohn Edwards with a solution using a C# class.
I’m wondering if it would be possible to use a native approach instead?
Tried the following code, but the overloaded ToString() from a PSCustomObject does not seem to be called when the ValidateSet is enumerated.
Does it have something to do with ToString() being overloaded by a ScriptMethod?
Function _New-CustomObjForQuotedString {
[CmdletBinding()]
Param(
[string]$InputString,
[string]$QuoteCharacter="'"
)
$obj = [PSCustomObject]@{
Value = $InputString
QuoteCharacter = $QuoteCharacter
}
$obj | Add-Member -MemberType ScriptMethod -Name ToString -Value {
if ( ($this.Value).contains(' ') ) {
return $($this.QuoteCharacter)+$($this.Value)+$($this.QuoteCharacter)
} else {
return $this.Value
}
} -Force
return $obj
}
function TestDynamicSetWithSpace_PSNative {
[CmdletBinding()]
param(
)
dynamicparam {
# from https://powershell.org/forums/topic/dynamic-parameter-question/#post-22381
$ParamDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$Attributes = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$Attributes.Add( (New-Object System.Management.Automation.ParameterAttribute) )
# Convert each object into a DynParamQuotedString:
$Attributes.Add( (New-Object System.Management.Automation.ValidateSetAttribute($__DynamicParamValidateSet | % { _New-CustomObjForQuotedString -InputString $_ })) )
$ParamDictionary.$__DynamicParamName = New-Object System.Management.Automation.RuntimeDefinedParameter (
$__DynamicParamName,
[PSCustomObject], # Notice the type here
$Attributes
)
return $ParamDictionary
}
process {
$ParamValue = $null
if ($PSBoundParameters.ContainsKey($__DynamicParamName)) {
# Get the original string back:
$ParamValue = $PSBoundParameters.$__DynamicParamName
}
"$__DynamicParamName = $ParamValue"
}
}