Seeker
Seeker

Reputation: 327

combine multiple powershell cmdlet outputs

I have a requirement to combine outputs of multiple Power Shell scripts to be given as pipeline output using CmdLets in .NET.

For example :

Command-GetA  -combine  Command-GetB | Command-GetD

So I want to send outputs of GetA and GetB to GetD via pipeline. Is there a compact way to do this using powershell scripts or .NET cmdlets? Other than ways like storing the outputs in an array and then passing it to pipeline?

Another complex example could be:

Command-GetA  -combine  ( Command-GetB | Command-GetD ) | Command-GetF

This should combine GetA and GetB|GetD to send as pipeline input to GetF

EDIT:

It would be good if I could do something like this - @( GetA ; GetB) -ConvertToASingleList | GetC

So OutputOfGetA and OutputOfGetB shouldn't get called separately on GetC. Both should, as a combined array or list object, be passed to GetC

Upvotes: 5

Views: 6301

Answers (4)

S. Melted
S. Melted

Reputation: 293

In case anyone has some weird edge case that the above doesn't work for, you can use -join as well.

EG.

-join($(Command-GetA), "," , $(Command-GetB))

This results in a string suitable for a csv.

Upvotes: 0

Seeker
Seeker

Reputation: 327

This gives me my desired end result :

$( Command-GetA; Command-GetB )| Group Result | Command-GetC

Thus, if "GetC" is a concatenating cmdlet, entire "GetC" cmdlet code runs only once for all my input files (my output file is created anew every time I run "concat")

Though all the above answers here are correct, my requirement was a bit different :)

EDIT:

This is perfect!

,@(Command-GetA;Command-GetB)|Command-GetC

Thanks @PetSerAl

Upvotes: 1

user1192666
user1192666

Reputation:

The solution suggested by @PetSerAl seems to meet your request:

PS C:\> function Foo { 1..3 }
PS C:\> function Bar { 2..4 }
PS C:\> & { Foo; Bar | % { $_ + 2 } } | oh
1
2
3
4
5
6

A script block combines all the output into an array. This behavior is documented in about_Script_Blocks:

A script block returns the output of all the commands in the script block, either as a single object or as an array.

Upvotes: 5

Saimonsais
Saimonsais

Reputation: 11

Well not really compact, but I would do like this:

    $alldata = @()

    $geta = command-geta
    $getb = command-getb
    $getc = command-getc


    #of course creating a function would be much more nicer in case the structure of A, B and C is the same

    foreach ($a in $geta)
        {
        $temp = New-Object System.Object
        $temp | Add-Member -MemberType NoteProperty -Name "aonedata" -Value $a.one
        $temp | Add-Member -MemberType NoteProperty -Name "atwodata" -Value $a.two
        $alldata += $temp   
        }

    foreach ($b in $getb)
        {
        $temp = New-Object System.Object
        $temp | Add-Member -MemberType NoteProperty -Name "bonedata" -Value $b.one
        $temp | Add-Member -MemberType NoteProperty -Name "btwodata"    -Value $b.two
        $alldata += $temp   
        }

    foreach ($c in $getc)
        {
        $temp = New-Object System.Object
        $temp | Add-Member -MemberType NoteProperty -Name "conedata" -Value $c.one
        $temp | Add-Member -MemberType NoteProperty -Name "ctwodata" -Value $c.two
        $alldata += $temp   
        }

    write-host $alldata
    $alldata | convertto-csv | out-file "c:\temp\lotsofdata.txt"

Upvotes: 0

Related Questions