2

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
    }
}
sclarson
  • 4,362
  • 3
  • 32
  • 44

1 Answers1

2

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

snipsnipsnip
  • 2,268
  • 2
  • 33
  • 34
Keith Hill
  • 194,368
  • 42
  • 353
  • 369