PowerShell wrapper to direct piped input to Python script

后端 未结 1 716
天涯浪人
天涯浪人 2021-01-24 14:45

I\'m trying to write a little tool that will let me pipe command output to the clipboard. I\'ve read through multiple answers on Stack Overflow, but they didn\'t work for m

相关标签:
1条回答
  • 2021-01-24 14:53

    First, in PowerShell, a multi-line text is an array, so you need a [String[]] parameter. To solve your problem, try using the process block:

    function Out-ClipBoard() {
        Param(
            [Parameter(ValueFromPipeline=$true)]
            [String[]] $Text
        )
        Begin
        {
            #Runs once to initialize function
            pushd
            cd \My\Profile\PythonScripts
            $output = @()
        }
        Process
        {
            #Saves input from pipeline.
            #Runs multiple times if pipelined or 1 time if sent with parameter
            $output += $Text
        }
        End
        {
            #Turns array into single string and pipes. Only runs once
            $output -join "`r`n" | python copyToClipboard.py
            popd
        }
    }
    

    I don't have Python here myself, so I can't test it. When you need to pass multiple items (an array) through the pipeline, you need the process block for PowerShell to handle it. More about process block and advanced functions is at TechNet.

    0 讨论(0)
提交回复
热议问题