Out-gridview with foreach

Does anyone have a good way to take the results from a foreach loop and output the data using out-gridview?

A very simple example is

1..10|foreach{$_}|Out-GridView

The principle is each time the loop runs you return the data so the out-gridview can display it. For a better example see http://mrbodean.azurewebsites.net/2015/10/06/get-the-source-location-for-all-packages-in-a-sccm-task-sequence/
Any function built to return data to the pipeline with be able to use the out-gridview cmdlet .

Mike, it would be helpful to see your code if it’s not 100’s of lines long.

Jonathan, i get an error when I do this.

At line:6 char:3

  • } | Out-GridView
  • ~
    An empty pipe element is not allowed.
    • CategoryInfo : ParserError: (:slight_smile: , ParentContainsErrorRecordException
    • FullyQualifiedErrorId : EmptyPipeElement

I figured it out:

$processes = (‘csrss’,‘svchost’,‘explorer’)

foreach($process in $processes){
$Output += Get-Process -Name $process | select ProcessName,VM
}
$Output | Out-GridView

It doesn’t work like that with Get-WmiObject though…
Any ideas?

$Server = (‘localhost’,‘Server1’,‘Server2’)

foreach($Server in $Servers){
$Output += Get-WmiObject -Class Win32_Networkadapter -ComputerName $Server | select PSComputerName,Manufacturer,Description,NetConnectionID,Speed,IPAddress,IPSubnet,DefaultIPGateway,DnsServerSearchOrder,DNSDomainSuffixSearchOrder
}
$Output | Out-GridView

Based on the code you posted, the problem is that your variable for your list of servers is missing an “s”

$Server = (‘localhost’,‘Server1’,‘Server2’)

Should be

$Servers = (‘localhost’,‘Server1’,‘Server2’)

You’re making it way harder than it needs to be.

Get-Process 'csrss','svchost','explorer' | 
    select ProcessName,VM | Out-GridView

Or have I lost the bubble here because the topic has morphed?

Thanks Curtis!

I figured it out, I needed to specify $Output as an array - @()

$Servers = (‘localhost’,‘Server1’,‘Server2’)
$Output = @()

foreach($Server in $Servers){
$Output += Get-WmiObject -Class Win32_Networkadapter -ComputerName $Server | select PSComputerName,Manufacturer,Description,NetConnectionID,Speed,IPAddress,IPSubnet,DefaultIPGateway,DnsServerSearchOrder,DNSDomainSuffixSearchOrder
}
$Output | Out-GridView