1

I have a python script that I wrapped in a function call. The script is scr and the function is function scr {python (Join-Path $ENV:HOME "bin\scr") $args}. When I call python directly, it accepts stdin fine. So echo hello | python bin\scr works. But when I call the function (echo hello | scr), then stdin is not passed from the pipe to the python script but seems to still come from the terminal. How can I fix this?

IttayD
  • 28,271
  • 28
  • 124
  • 178
  • 2
    You can extend your function with the `[CmdletBinding]` attribute. This will allow you to pipe to it. [See here](https://technet.microsoft.com/en-us/library/hh847872.aspx) for more info – arco444 Jul 16 '15 at 13:51

1 Answers1

2

You can use $input automatic variable, which represent pipeline input to your function. Also you can use $MyInvocation.ExpectingInput to find out did your function expect any pipeline input or not.

function scr {
    $MyArgs=(Join-Path $ENV:HOME "bin\scr"),$args
    if($MyInvocation.ExpectingInput){
        $input | python @MyArgs
    }else{
        python @MyArgs
    }
}
user4003407
  • 21,204
  • 4
  • 50
  • 60