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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With