14

I have a command Get-Testdata that retrieves test data from different sources and stores these into a PSObject with the different values as properties. The total number of objects are then stored as an array, for easy manipulation, sorting, calculating etc.

My problem is that I want to be able to present this data as (color-coded) HTML, for which I've written another command, Show-TestResults. The input parameter looks like this

[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
[PSObject[]]$InputObject

UPDATE 1

This function itself is very basic, it simply sets some parameters for ConvertTo-HTML and then pipe the objects into that command:

$head = "<style>[...]" #styling with javascript etc
$header = "<H1>Test Results</H1>
$title = "Test results"
$InputObject | ConvertTo-HTML -head $head -body $header -title $title | Out-File $Filename

END UPDATE 1

However, when I try to use the ValueFromPipeline property, using the call

Get-Testdata [...] | Show-TestResults 

only the first object in the array is shown. But if I instead call the command like

$td = Get-Testdata [...]
Show-TestResults $td 

The whole array is presented, as expected. Can someone explain this - and hopefully guide me into correcting it?

2
  • Would you like to show how your function looks like (approximately) and how you use $InputObject there? If there is a bug there we'll see it. Feb 17, 2012 at 13:34
  • Updated now with function details Feb 17, 2012 at 14:52

4 Answers 4

13

You probably process data in end block, not process block.

Look at an example:

function getdata {
    1
    2
    3
    4
}
function show-data {
    param(
        [Parameter(mandatory=$true, ValueFromPipeline=$true)]$InputObject,
        [Parameter(mandatory=$true)]$FileName
    )

    # this is process block that is probably missing in your code
    begin { $objects = @() }
    process { $objects += $InputObject }
    end {
        $head = "<style></style>"
        $header = "<H1>Test Results</H1>"
        $title = "Test results"
        $objects | ConvertTo-HTML -head $head -body $header -title $title | Out-File $Filename
    }
}

getdata | show-data -file d:\temp\test.html
4
  • Thanks, but as you can see from my update above, putting the HTML conversion into a process block simply makes only the last post to "get through" Feb 17, 2012 at 14:54
  • Updated. You need to collect the objects and pass in the end block. Other method would be probably using proxy functions, but this one is much more easier.
    – stej
    Feb 17, 2012 at 14:59
  • Thanks, collecting input into another array during process { } did the trick. But I'm still curious to know why the behavior is different depending on how the parameter receives its data, to me they should behave the same way. Feb 20, 2012 at 11:33
  • 1
    Having some more Powershell experience under the belt, I now understand the difference: Pipelining sends results to the process {} block, while 'standard' parameters sends them to the end {} block (which means the whole function if you don't use any of begin{}, process {} or end {} blocks) Jan 8, 2018 at 9:31
7

If an advanced function is the requirement then I would go in the way proposed by @stej.

Otherwise I would consider this simple technique when a function accepts both pipeline and parameter input:

function Show-Data
(
    $FileName,
    $InputObject
)
{
    # this is the trick:
    if ($InputObject) { $input = $InputObject }

    # process the input (from pipeline or parameter)
    $input | ConvertTo-HTML > $FileName
}

# pipe data
Get-ChildItem | Show-Data Test1.htm

# pass via parameter
Show-Data Test2.htm (Get-ChildItem)

N.B. $input in this case is an automatic variable for the pipeline input.

1
  • This works as well (I have tried previously with $input), but since I want control over the parameter, @stej's answer above suits me better. Feb 20, 2012 at 11:36
4

I came across the same issue/question and the way I usually resolve this is like:

Function Show-Data {
    param(
        [Parameter(mandatory=$true, ValueFromPipeline=$true)]$InputObject,
        [Parameter(mandatory=$true)]$FileName
    )
    $PipeLine = $Input | ForEach {$_}; If ($PipeLine) {$InputObject = $PipeLine}
    ...

As I don't think it is a good idea to overwrite the automatic $Input variable.

Anyway, I haven't seen an answer to the part of the question: "Can someone explain this?"
I guess that it has something to do with the Strongly Encouraged Development Guidelines which states:

Support the ProcessRecord Method
To accept all the records from the preceding cmdlet in the pipeline, your cmdlet must implement the ProcessRecord method. Windows PowerShell calls this method multiple times, once for every record that is sent to your cmdlet.

The ProcessRecord method appears to me as a C# method which I presume is invoked by the process block as in the solution from stej. But this doesn't explain why this works this way for a PSCustomObject array and not for e.g. system objects, like:

Get-psdrive | Show-Data

Or even:

@(Get-psdrive) | Show-Data
2

I think the problem is that the pipeline is unrolling your array into a stream of objects, and presenting them to your function one at a time, instead of as an array.

Does it work if you do this:

,(Get-Testdata [...]) | Show-TestResults 

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.