out-gridview open multiple instances while looping

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

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

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

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.

this is the result of your code

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

  •     $newobj = [PSCustomObject]@{
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:slight_smile: , RuntimeException
    • FullyQualifiedErrorId : NullArray

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

  •     $newobj = [PSCustomObject]@{
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:slight_smile: , RuntimeException
    • FullyQualifiedErrorId : NullArray

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.