How to pass $_ ($PSItem) in a ScriptBlock

后端 未结 3 634
执笔经年
执笔经年 2021-01-06 07:51

I\'m basically building my own parallel foreach pipeline function, using runspaces.

My problem is: I call my function like this:

somePipeline | MyNew         


        
3条回答
  •  孤街浪徒
    2021-01-06 08:14

    # I was looking for an easy way to do this in a scripted function,
    # and the below worked for me in PSVersion 5.1.17134.590
    
    function Test-ScriptBlock {
        param(
            [string]$Value,
            [ScriptBlock]$FilterScript={$_}
        )
        $_ = $Value
        & $FilterScript
    }
    Test-ScriptBlock -Value 'unimportant/long/path/to/foo.bar' -FilterScript { [Regex]::Replace($_,'unimportant/','') }
    

提交回复
热议问题