Author Posts

October 17, 2015 at 11:56 pm

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

October 18, 2015 at 2:45 am

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 .

October 18, 2015 at 4:05 am

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

October 18, 2015 at 9:35 am

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: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : EmptyPipeElement

October 18, 2015 at 9:39 am

I figured it out:

$processes = ('csrss','svchost','explorer')

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

October 18, 2015 at 10:09 am

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

October 18, 2015 at 3:31 pm

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')

October 18, 2015 at 4:05 pm

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?

October 24, 2015 at 7:04 am

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