Jan
04
2009
Using Proxy Commands in PowerShell
Posted by: Andy Schneider in CTP 3, Powershell, Proxy CommandsJeffrey 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.
-
Function Get-ChildItemProxy {
-
[CmdletBinding(DefaultParameterSetName='Items', SupportsTransactions=$true)]
-
param(
-
[Parameter(ParameterSetName='Items', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
-
[System.String[]]
-
${Path},
-
-
[Parameter(ParameterSetName='LiteralItems', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
-
[Alias('PSPath')]
-
[System.String[]]
-
${LiteralPath},
-
-
[Parameter(Position=1)]
-
[System.String]
-
${Filter},
-
-
[System.String[]]
-
${Include},
-
-
[System.String[]]
-
${Exclude},
-
-
[Switch]
-
${Recurse},
-
-
[Switch]
-
${Force},
-
-
[Switch]
-
${Name},
-
-
[Switch]
-
${ContainersOnly},
-
-
[Switch]
-
${NoContainersOnly}
-
)
-
-
begin
-
{
-
try {
-
$outBuffer = $null
-
if ($PSBoundParameters.TryGetValue(‘OutBuffer’, [ref]$outBuffer) -and $outBuffer -gt 1024)
-
{
-
$PSBoundParameters['OutBuffer'] = 1024
-
}
-
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand(‘Get-ChildItem’, [System.Management.Automation.CommandTypes]::Cmdlet)
-
-
if ($ContainersOnly)
-
{
-
[Void]$PSBoundParameters.Remove(“ContainersOnly”)
-
$scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object {$_.PSIsContainer -eq $true}}
-
-
} elseif ($NoContainersOnly)
-
{
-
[Void]$PSBoundParameters.Remove(“NoContainersOnly”)
-
$scriptCmd = {& $wrappedCmd @PSBoundParameters | Where-Object {$_.PSIsContainer -eq $false}}
-
}
-
{
-
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
-
}
-
-
-
-
$steppablePipeline = $scriptCmd.GetSteppablePipeline()
-
$steppablePipeline.Begin($PSCmdlet)
-
} catch {
-
throw
-
}
-
}
-
-
process
-
{
-
try {
-
$steppablePipeline.Process($_)
-
} catch {
-
throw
-
}
-
}
-
-
end
-
{
-
try {
-
$steppablePipeline.End()
-
} catch {
-
throw
-
}
-
}
-
<#
-
-
.ForwardHelpTargetName Get-ChildItem
-
.ForwardHelpCategory Cmdlet
-
-
#>
-
-
}
Entries (RSS)
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 paid proxy that will at least cut down on the risk involved in the cyber world.