# out-gridview open multiple instances while looping

**URL:** <https://forums.powershell.org/t/out-gridview-open-multiple-instances-while-looping/8709>\
**Category:** PowerShell Help\
**Created:** [May 26, 2017, 11:56am UTC](https://forums.powershell.org/t/out-gridview-open-multiple-instances-while-looping/8709 "2017-05-26T11:56:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fadygh](https://avatars.discourse-cdn.com/v4/letter/f/3e96dc/32.png) [@fadygh](https://forums.powershell.org/u/fadygh)\
**Post date:** [May 26, 2017, 11:56am UTC](https://forums.powershell.org/t/out-gridview-open-multiple-instances-while-looping/8709/1 "2017-05-26T11:56:02Z")

</div>

Hello Everyone

I’m studying powershell and I’m trying to build a tool that scans computers on the network and gives informations about their network configuration but I’m facing 1 problem is that after I pipeline the result to out-gridview it opens multiple instances for each computer I also tried to export the result to csv I only see the last computer in the list

$list = ( ‘pc1’, ‘pc2’)  
$TargetHost = @($newList | foreach { Test-Connection -count 1 -computer $\_ -ErrorAction SilentlyContinue })  
$computers = $TargetHost | select address -ExpandProperty address  
$computers  
foreach ($computer in $Computers){  
$network = Get-WmiObject -Class Win32\_NetworkAdapterConfiguration -ComputerName $computer -Filter “IPEnabled = ‘$true’” -Property ‘\*’ -ErrorAction SilentlyContinue

```
$props = [ordered]@{'computerName' = $Computer;
          
           'IP Address' = $network.IPAddress -split (" ") | select -First 1;
           'Default Gateway' = $network.DefaultIPGateway -split (" ") | select -First 1;
           'DNS' = $network.DNSServerSearchOrder -split (" ") | select -First 1;
           'DNS 2' = $network.DNSServerSearchOrder -split (" ") | select -Last 1;
           
           }

$obj = New-Object -TypeName PSObject -Property $props | Out-GridView -OutputMode Single

      }

```

thanks in advance

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 26, 2017, 1:06pm UTC](https://forums.powershell.org/t/out-gridview-open-multiple-instances-while-looping/8709/2 "2017-05-26T13:06:09Z")

</div>

I cannot test this at the moment but it should be enough for you to start playing around

```
$ResultObject = New-Object System.Collections.Generic.List[object]
$ComputerList = ‘pc1’, ‘pc2’
Foreach($ComputerName in $ComputerList){
If(Test-Connection -ComputerName $ComputerName -Quiet -Count 1){
$NetConfig = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -Filter “IPEnabled=true” -Property ‘*’ -ErrorAction SilentlyContinue
$NetConfig
$newobj = [PSCustomObject]@{
‘ComputerName’ = $ComputerName
‘IP Address’ = $NetConfig.IPAddress[0]
‘Default Gateway’ = $NetConfig.DefaultIPGateway[1]
‘DNS’ = $NetConfig.DNSServerSearchOrder[1]
‘DNS 2’ = $NetConfig.DNSServerSearchOrder[-1]
}
$ResultObject.Add($newobj)
}
}
$ResultObject
```
  
Please use the code formatting options here in the forum. It’s explained here: [How to Format Code in the Forum](https://powershell.org/forums/topic/how-to-format-code-in-the-forums/)

---

<div class="post-metadata">

**Author:** ![fadygh](https://avatars.discourse-cdn.com/v4/letter/f/3e96dc/32.png) [@fadygh](https://forums.powershell.org/u/fadygh)\
**Post date:** [May 27, 2017, 3:19am UTC](https://forums.powershell.org/t/out-gridview-open-multiple-instances-while-looping/8709/3 "2017-05-27T03:19:02Z")

</div>

thanks for your reply but it gave me an error anyway I want to output my script to csv and out-gridview and it will be great if I can export it to csv I can later open it in out-gridview or any other file but my problem is that while it’s looping it only shows me the last result I don’t want to change my too muchscript since I only posted part of it any idea

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 27, 2017, 5:51pm UTC](https://forums.powershell.org/t/out-gridview-open-multiple-instances-while-looping/8709/4 "2017-05-27T17:51:51Z")

</div>

It’s almost impossible to find the problem if you don’t show the code and errors the code produces. If you get only the last result you have an error in your loop.

---

<div class="post-metadata">

**Author:** ![fadygh](https://avatars.discourse-cdn.com/v4/letter/f/3e96dc/32.png) [@fadygh](https://forums.powershell.org/u/fadygh)\
**Post date:** [May 28, 2017, 1:22am UTC](https://forums.powershell.org/t/out-gridview-open-multiple-instances-while-looping/8709/5 "2017-05-28T01:22:43Z")

</div>

this is the result of your code

Cannot index into a null array.  
At line:7 char:9

- 

```
    $newobj = [PSCustomObject]@{

```

- 

```
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : InvalidOperation: (🙂 , RuntimeException
  - FullyQualifiedErrorId : NullArray

Cannot index into a null array.  
At line:7 char:9

- 

```
    $newobj = [PSCustomObject]@{

```

- 

```
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : InvalidOperation: (🙂 , RuntimeException
  - FullyQualifiedErrorId : NullArray

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 28, 2017, 11:19am UTC](https://forums.powershell.org/t/out-gridview-open-multiple-instances-while-looping/8709/6 "2017-05-28T11:19:23Z")

</div>

OK, try it this way:

```
$ResultObject = New-Object System.Collections.Generic.List[object]
$ComputerList = ‘pc1’, ‘pc2’
Foreach($ComputerName in $ComputerList){
Write-Verbose $ComputerName
If(Test-Connection -ComputerName $ComputerName -Quiet -Count 1){
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -Filter “IPEnabled=true” -Property ‘*’ -ErrorAction SilentlyContinue |
ForEach-Object{
$newobj = [PSCustomObject]@{
‘ComputerName’ = $ComputerName
‘IP Address’ = @($_.IPAddress)[0]  
‘Default Gateway’ = @($_.DefaultIPGateway)[0]
‘DNS’ = @($_.DNSServerSearchOrder)[0]  
‘DNS 2’ = @($_.DNSServerSearchOrder)[-1]
}
$ResultObject.Add($newobj)
}
}
}
$ResultObject | Format-Table -AutoSize
```
  
Instead of the last line you can pipe the **$ResultObject** to **Export-CSV** , **Out-GridView** or whatever you like.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/out-gridview-open-multiple-instances-while-looping/8709/7 "2024-05-16T20:38:41Z")

</div>


