1+ <#
2+ . SYNOPSIS
3+ Creates Proxy Commands
4+ . DESCRIPTION
5+ Generates a Proxy Command for an underlying PowerShell or PipeScript command.
6+ . EXAMPLE
7+ .\ProxyCommand.psx.ps1 -CommandName Get-Process
8+ . EXAMPLE
9+ {
10+ function [ProxyCommand<'Get-Process'>]GetProcessProxy {}
11+ } | .>PipeScript
12+ . EXAMPLE
13+ .>ProxyCommand -CommandName Get-Process -RemoveParameter *
14+ . EXAMPLE
15+ Invoke-PipeScript -ScriptBlock {[ProxyCommand('Get-Process')]param()}
16+ . EXAMPLE
17+ Invoke-PipeScript -ScriptBlock {
18+ [ProxyCommand('Get-Process',
19+ RemoveParameter='*',
20+ DefaultParameter={
21+ @{id='$pid'}
22+ })]
23+ param()
24+ }
25+ . EXAMPLE
26+ {
27+ function Get-MyProcess {
28+ [ProxyCommand('Get-Process',
29+ RemoveParameter='*',
30+ DefaultParameter={
31+ @{id='$pid'}
32+ })]
33+ param()
34+ }
35+ } | .>PipeScript
36+ #>
37+ param (
38+ # The ScriptBlock that will become a proxy command. This should be empty, since it is ignored.
39+ [Parameter (ValueFromPipeline )]
40+ [scriptblock ]
41+ $ScriptBlock ,
42+
43+ # The name of the command being proxied.
44+ [Parameter (Mandatory , Position = 0 )]
45+ [string ]
46+ $CommandName ,
47+
48+ # If provided, will remove any number of parameters from the proxy command.
49+ [string []]
50+ $RemoveParameter ,
51+
52+ # Any default parameters for the ProxyCommand.
53+ [Collections.IDictionary ]
54+ $DefaultParameter
55+ )
56+
57+ $resolvedCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand ($CommandName , ' Alias,Function,Cmdlet' )
58+ if (-not $resolvedCommand ) {
59+ Write-Error " Could not resolve -CommandName '$CommandName '"
60+ return
61+ }
62+
63+ $commandMetadata = [Management.Automation.CommandMetadata ]$resolvedCommand
64+
65+ if ($RemoveParameter ) {
66+ $toRemove = @ (
67+ foreach ($paramName in $commandMetadata.Parameters.Keys ) {
68+ if ($RemoveParameter -contains $paramName ) {
69+ $paramName
70+ } else {
71+ foreach ($rp in $RemoveParameter ) {
72+ if ($paramName -like $rp ) {
73+ $paramName
74+ break
75+ }
76+ }
77+ }
78+ }
79+ )
80+
81+ $null = foreach ($tr in $toRemove ) {
82+ $commandMetadata.Parameters.Remove ($tr )
83+ }
84+ }
85+
86+ $proxyCommandText = [Management.Automation.ProxyCommand ]::Create($commandMetadata )
87+
88+ if ($DefaultParameter ) {
89+ $toSplat = " @'
90+ $ ( ConvertTo-Json $DefaultParameter - Depth 100 )
91+ '@"
92+ $insertPoint = $proxyCommandText.IndexOf (' $scriptCmd = {& $wrappedCmd @PSBoundParameters }' )
93+ $proxyCommandText = $proxyCommandText.Insert ($insertPoint , @"
94+ `$ _DefaultParameters = ConvertFrom-Json $toSplat
95+ foreach (`$ property in `$ _DefaultParameters.psobject.properties) {
96+ `$ psBoundParameters[`$ property.Name] = `$ property.Value
97+ if (`$ property.Value -is [string] -and `$ property.Value.StartsWith('`$ ')) {
98+ `$ psBoundParameters[`$ property.Name] = `$ executionContext.SessionState.PSVariable.Get(`$ property.Value.Substring(1)).Value
99+ }
100+ }
101+ "@ )
102+
103+ }
104+
105+ [ScriptBlock ]::Create($proxyCommandText )
0 commit comments