by nohandle at 2012-10-15 05:07:35
Hello,by poshoholic at 2012-10-15 06:25:02
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:param(
[Parameter(ParameterSetName='query', Mandatory=$true, Position=0)]
[Parameter(ParameterSetName='list', Position=1)]
[System.String]
${Class},
I am wondering if there is a reason why the parameter names are enclosed in ${}?
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:by nohandle at 2012-10-15 07:39:21function Test-UglyParameterName {
[CmdletBinding()]
param(
[Parameter()]
[System.String]
${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.
Thanks,by poshoholic at 2012-10-15 07:53:08
is there an easy way to call the function with parameter that contains spaces?
I guess it depends on what you call "easy". Splatting is the only way I know of that allows you to use named parameters like this.by nohandle at 2012-10-15 08:02:09PS 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
Thanks,by poshoholic at 2012-10-15 08:17:40
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 the way, if you are proxying Get-WmiObject, you should have a look at wmix as well as pspx