Out-gridview with foreach

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Mike Eyler Mike Eyler 1 year, 7 months ago.

  • Author
    Posts
  • #30972
    Profile photo of Mike Eyler
    Mike Eyler
    Participant

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

  • #30973
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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 .

  • #30974
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

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

  • #30977
    Profile photo of Mike Eyler
    Mike Eyler
    Participant

    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

  • #30979
    Profile photo of Mike Eyler
    Mike Eyler
    Participant

    I figured it out:

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

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

  • #30983
    Profile photo of Mike Eyler
    Mike Eyler
    Participant

    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

  • #30998
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

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

  • #30999
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    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?

  • #31294
    Profile photo of Mike Eyler
    Mike Eyler
    Participant

    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

You must be logged in to reply to this topic.