Using Proxy Commands in PowerShell

Jeffrey Snover just posted a great article on how to use Proxy Commands in CTP3. He also built a module called MetaProgramming that makes this much easier. I was able to take what he did and created a proxy command for get-childitem and added two switch parameters, -containersOnly and -NoContainersOnly.

I posted the code up on PoshCode here.

  1. Function Get-ChildItemProxy {
  2. [CmdletBinding(DefaultParameterSetName='Items', SupportsTransactions=$true)]
  3. param(
  4.     [Parameter(ParameterSetName='Items', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  5.     [System.String[]]
  6.     ${Path},
  7.  
  8.     [Parameter(ParameterSetName='LiteralItems', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
  9.     [Alias('PSPath')]
  10.     [System.String[]]
  11.     ${LiteralPath},
  12.  
  13.     [Parameter(Position=1)]
  14.     [System.String]
  15.     ${Filter},
  16.  
  17.     [System.String[]]
  18.     ${Include},
  19.  
  20.     [System.String[]]
  21.     ${Exclude},
  22.  
  23.     [Switch]
  24.     ${Recurse},
  25.  
  26.     [Switch]
  27.     ${Force},
  28.  
  29.     [Switch]
  30.     ${Name},
  31.    
  32.     [Switch]
  33.     ${ContainersOnly},
  34.    
  35.     [Switch]
  36.     ${NoContainersOnly}
  37.     )
  38.  
  39. begin
  40. {
  41.     try {
  42.         $outBuffer = $null
  43.         if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer) -and $outBuffer -gt 1024)
  44.         {
  45.             $PSBoundParameters['OutBuffer'] = 1024
  46.         }
  47.         $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-ChildItem', [System.Management.Automation.CommandTypes]::Cmdlet)
  48.        
  49.         if ($ContainersOnly)
  50.         {
  51.             [Void]$PSBoundParameters.Remove("ContainersOnly")
  52.             $scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object {$_.PSIsContainer -eq $true}}
  53.            
  54.         } elseif ($NoContainersOnly)
  55.                {
  56.                    [Void]$PSBoundParameters.Remove("NoContainersOnly")
  57.                    $scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object {$_.PSIsContainer -eq $false}}
  58.                }   
  59.         {
  60.             $scriptCmd = {& $wrappedCmd @PSBoundParameters }
  61.         }
  62.        
  63.  
  64.        
  65.         $steppablePipeline = $scriptCmd.GetSteppablePipeline()
  66.         $steppablePipeline.Begin($PSCmdlet)
  67.     } catch {
  68.         throw
  69.     }
  70. }
  71.  
  72. process
  73. {
  74.     try {
  75.         $steppablePipeline.Process($_)
  76.     } catch {
  77.         throw
  78.     }
  79. }
  80.  
  81. end
  82. {
  83.     try {
  84.         $steppablePipeline.End()
  85.     } catch {
  86.         throw
  87.     }
  88. }
  89. <#
  90.  
  91. .ForwardHelpTargetName Get-ChildItem
  92. .ForwardHelpCategory Cmdlet
  93.  
  94. #>
  95.  
  96. }
downloadThis Script brought to you by PoshCode
 

Comments (3) -

REALLY cool, Andy.

Hi,
add "Else" after the line 58.

With not only global security, but national and online security at high risk, precautions need to be taken in every aspect of life. If you are looking for a little bit of online security, you can use a http://bestpaidproxyservice.com/" rel="nofollow">paid proxy that will at least cut down on the risk involved in the cyber world.

Pingbacks and trackbacks (1)+

Comments are closed