om8000
om8000

Reputation: 21

Custom PowerShell function that accepts arguments and pipe input

Currently I have the following code below, which does not work for me. I would Like it to behave like this

some-output-from-some-function | FILTER_DATA

or

some-output-from-some-function | FILTER_DATA | Filter_01

With the function below (without the $id included) i need to catch the some-output-from-some-function int ovariable and execute it like this (which does NOT suit me):

$a = some-output-from-some-function
"$a" | FILTER_DATA

Please help. I want it to be universal, not specific to one or two passed arguments ...

Function FILTER_DATA ($id){
                      [CmdletBinding()]
                      Param(
                            [Parameter(ValueFromPipeline)]
                            $item
                           )
       $item | % {$PSItem.replace("&lt;","<").replace("&gt;",">")} |
               % {$PSItem -replace "<table>", "$description<table class=sortable id=$id" `
                          -replace "</table>","</table></span>" `
}}

Upvotes: 2

Views: 324

Answers (1)

Mudit Bahedia
Mudit Bahedia

Reputation: 246

Param() can't be used if the arguments are passed in function declaration. In your case you passed $ID while declaring function. You can use below code which should work fine.

Function FILTER_DATA {
                      [CmdletBinding()]
                      Param(
                            [Parameter(ValueFromPipeline)]$item,
                            $ID
                            )
       $item | % {$PSItem.replace("&lt;","<").replace("&gt;",">")} |
               % {$PSItem -replace "<table>", "$description<table class=sortable id=$id" `
                          -replace "</table>","</table></span>" `}
}

And now FILTER_DATA can accept input $Item from pipeline.

Upvotes: 1

Related Questions