0

I'm having problems understanding how I can work with output I create in a script.

The main cause is that for this example:

$Output = @()
$Output += New-Object psobject -Property @{FirstName="Harry";LastName="Potter";Age=11}
$Output += New-Object psobject -Property @{FirstName="Hermione";LastName="Granger";Age=11}
$Output += New-Object psobject -Property @{FirstName="Ron";LastName="Weasley";Age=11}
$Output += New-Object psobject -Property @{FirstName="Ginny";LastName="Weasley";Age=10}
$Output += New-Object psobject -Property @{FirstName="Albus";LastName="Dumbledore";Age=150}
$Output += New-Object psobject -Property @{FirstName="Severus";LastName="Snape";Age=40}
Write-Output $Output

I could take the table I've created, and pipe into a new scripts' parameter.

Say I'm creating a new script like this

Param(
  [Parameter(ValueFromPipeline=$true,Mandatory=$true,Position=1)]
  $in
)

Write-Output $in

The first script is a.ps1 and the second is named b.ps1 I want to prompt:

.\a.ps1 | .\b.ps1

And get the full table printed:

Age LastName                                            FirstName
--- --------                                            ---------
 11 Potter                                              Harry
 11 Granger                                             Hermione
 11 Weasley                                             Ron
 10 Weasley                                             Ginny
150 Dumbledore                                          Albus
 40 Snape                                               Severus

This is the output of the a.ps1 script, but if I'll pipe it to b.ps1 script, il recieve:

Age LastName                                            FirstName
--- --------                                            ---------
 40 Snape                                               Severus

I know that I'm doing something wrong, but what I want basicly is functionality similar to:

Get-Service | Stop-Service

Where the Stop-Service is an independant command that can accept parameters, but could also accept piped output from a different command and work fine as well.

YuvalS
  • 5
  • 1
  • 1
    Possible duplicate of [How to write a Powershell script that accepts pipeline input?](http://stackoverflow.com/questions/885349/how-to-write-a-powershell-script-that-accepts-pipeline-input) – TessellatingHeckler Aug 22 '16 at 22:51

1 Answers1

1

I would do it like this. Content of b.ps1:

Param( 
  [Parameter(ValueFromPipeline=$true,Mandatory=$true,Position=1)]
  $in
)
begin
{
    $all = @()
}
process
{
    $all += $in
}
end
{
    $all | % { Write-Output $_ }
}

Basically initializing a new array in begin, then regrouping all objects piped in process, and finally looping through the array to display its elements.

Result:

.\a.ps1 | .\b.ps1

Age FirstName LastName  
--- --------- --------  
11 Harry     Potter    
11 Hermione  Granger   
11 Ron       Weasley   
10 Ginny     Weasley   
150 Albus     Dumbledore
40 Severus   Snape   
Micky Balladelli
  • 9,781
  • 2
  • 33
  • 31