sclarson
sclarson

Reputation: 4422

How do you modify arguments sent to built in command in powershell v2?

In powershell v3 you can use $executionContext.SessionState.InvokeCommand.PostCommandLookupAction and CommandScriptBlock to modify the arguments. How can you accomplish the same thing in v2?

Powershell 3 example:

$executionContext.SessionState.InvokeCommand.PostCommandLookupAction = {
    param($CommandName, $CommandLookupEventArgs)
    if($CommandLookupEventArgs.CommandOrigin -eq "Runspace" -and $CommandName -eq "cd"){
        $CommandLookupEventArgs.CommandScriptBlock = {
            if($args){
            $x = ModifyPathOrDoSomethingHere($x)
            $x = Resolve-Path($args)
            Set-Location $x
            }
        }
        $CommandLookupEventArgs.StopSearch = $true
    }
}

Upvotes: 2

Views: 193

Answers (1)

Keith Hill
Keith Hill

Reputation: 201952

In V2 (and higher), you can intercept commands using a technique called proxy commands. Check out this PowerShell team blog post on the subject.

Upvotes: 2

Related Questions