${paramName} in proxy functions

by nohandle at 2012-10-15 05:07:35

When I get a definiton of a proxy function using
$Cmd = Get-Command get-WmiObject
$CmdMetadata = New-Object System.Management.Automation.CommandMetaData $Cmd
[System.Management.Automation.ProxyCommand]::Create($CmdMetadata) | Write-Output

The params are defined like this:
[Parameter(ParameterSetName='query', Mandatory=$true, Position=0)]
[Parameter(ParameterSetName='list', Position=1)]

I am wondering if there is a reason why the parameter names are enclosed in ${}?
by poshoholic at 2012-10-15 06:25:02
The proxy function generator uses curly braces so that it will work for any command you are proxying, even if that command contains parameter names that contain spaces or other characters that would be deemed invalid unless it was closed in curly braces. For example, you can do this:

function Test-UglyParameterName {
${What the heck was he thinking when he made this parameter name?}
${What the heck was he thinking when he made this parameter name?}

If you were to proxy that without the proxy generator putting curly braces around parameter names, then the proxy function that was generated wouldn’t parse properly.

All that said, I really don’t like that it puts curly braces around everything. They could have just checked the parameter name and if it was normal, use it as is, otherwise wrap it in curly braces.
by nohandle at 2012-10-15 07:39:21
is there an easy way to call the function with parameter that contains spaces?
by poshoholic at 2012-10-15 07:53:08
I guess it depends on what you call "easy". :slight_smile: Splatting is the only way I know of that allows you to use named parameters like this.

PS C:> $splat = @{
'What the heck was he thinking when he made this parameter name?' = 'This is not very easy but it works'

PS C:> Test-UglyParameterName @splat
This is not very easy but it works
by nohandle at 2012-10-15 08:02:09
the splatting worked for me also, the proxy definition uses it so it was obvious way to go. I should have mentioned it, sorry.

I was afraid I am missing some character escaping/curly bracing voodoo.
by poshoholic at 2012-10-15 08:17:40
By the way, if you are proxying Get-WmiObject, you should have a look at wmix as well as pspx