0

I'm trying to create a cmdlet written in powershell that can accept each path from dir command and run a powershell function on this path. Example:

PS> dir *.ext | Convert-xyz 

Here's what I tried below. Except, it only processes the first item listed by dir *.ext and then exits.

function Convert-xyz {

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True)]
        [string[]]$path,

        [string[]]$function
    )

    foreach ($pathi in $path) {
        Write-Host -ForegroundColor "Yellow" "`nPath: $pathi"
        foreach ($funct in $function) {
            write-host -ForegroundColor "Green" "Function: $funct"
            switch($funct) {
                "reflow"   {reflow  ($pathi);  break}
                "desquare" {desquare($pathi);  break}

                default   {
                    write-host -ForegroundColor "Red" "Unknown function: $funct"
                }
            }
        }
    }
    Write-Host "End"

} #function
2
  • You'll need a foreach in there, something like dir *.exe | %{convert-xyz} Commented Jan 10, 2018 at 20:37
  • 1
    Aside: Your function calls are not correct semantically. PowerShell functions are not called with ( ). The calls should beshould be reflow $pathi and desquare $pathi, not reflow ($pathi) and desquare ($pathi). Commented Jan 10, 2018 at 20:39

1 Answer 1

1

For handling pipeline input you'll want to put your processing code in a Process {} block.

function Convert-xyz {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [string[]]$path,

        [string[]]$function
    )

    Process {
        foreach ($pathi in $path) {
            Write-Host -ForegroundColor "Yellow" "`nPath: $pathi"
            foreach ($funct in $function) {
                Write-Host -ForegroundColor "Green" "Function: $funct"
                switch ($funct) {
                    "reflow"   {reflow   $pathi; break}
                    "desquare" {desquare $pathi; break}
                    default    {
                        Write-Host -ForegroundColor "Red" "Unknown function: $funct"
                    }
                }
            }
        }
    }

    End {
        Write-Host "End"
    }
} #function

The content of that block is run for every input from the pipeline (which doesn't come as an array, but one item at a time).

See also this article from Don Jones.

Note that you'll still need to pass the function you want to invoke on the input as a parameter, though:

dir *.ext | Convert-xyz -Function 'reflow'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.