Outputting to datagridview

Afternoon all

I’m having a slight issue displaying on a datagridview. This is what happens
My scripts checks desktops and currently writes the output to a csv. the input is from a txt file with hostanmes.

I’m trying to do away with the csv file and output to a datagridview.
Currently I only get an output from 1 hostname but the script does check the rest.

Here’s the script

It’s messy as I still got the csv part in there till I get this working

function checkmulti {


$computers = (Get-Content -Path C:\temp\computers.txt)
$resultsarray =@()
del c:\temp\info.csv -force -recurse
$outputMulti.Text = write "Checking computers in txt file"
sleep 3
foreach ($HostName In $computers){
$ping = (Test-Connection -ComputerName $HostName -Count 1 -quiet)
$outputBoxping.text = $ping
if  (-not  $ping)  {
    continue  
    }

 			
 			$outputboxappqry.text = write "Checking Security Software"
            (Get-WmiObject -computerName $HostName Win32_Service -Filter "Name='RemoteRegistry'").StartService() | Out-Null
            $osTC_type = (Get-WmiObject -ComputerName $HostName -Class Win32_ComputerSystem).SystemType -match '(x86)'
            $osTC = (Get-WmiObject -ComputerName $HostName -Class Win32_OperatingSystem).caption -match "7"    
            $osXP_type = (Get-WmiObject -ComputerName $HostName -Class Win32_ComputerSystem).SystemType -match '(x86)'
            $osXP = (Get-WmiObject -ComputerName $HostName -Class Win32_OperatingSystem).caption -match "XP"
            $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$HostName)
            $RegKeySEP= $Reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{A5DCF955-5D4A-471D-8CB3-DCFDF5C5DEE7}")
            $osVDI = (Get-WmiObject -ComputerName $HostName -Class Win32_ComputerSystem).name -match "VDI"
            $osXPVDI_type = (Get-WmiObject -ComputerName $HostName -Class Win32_ComputerSystem).SystemType -match '(x86)'
            $osXPVDI = (Get-WmiObject -ComputerName $HostName -Class Win32_OperatingSystem).caption -match "XP"
            $RegKeyVDA= $Reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{7B41B963-60ED-4D6A-8031-42E69B13F482}")
            if ($osXPVDI -eq "True" -and $osXPVDI_type -eq "True"){$RegKeyVDA= $Reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AB15CDB8-F490-4476-B106-631CBF13F6C1}")}
            if ($osTC_type -eq "True" -and $osTC -eq "True") {$RegKeyBuild= $Reg.OpenSubKey("SOFTWARE\StandardPlatform")}
            if ($osTC_type -eq "True" -and $osTC -eq "True"){$RegKeySEP= $Reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0E251D4D-316C-4F8B-A4C5-2722000764BE}")}
            if ($osXP_type -eq "True" -and $osXP -eq "True") {$RegKeySEP= $Reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0E251D4D-316C-4F8B-A4C5-2722000764BE}")}
            if ($osXP_type -eq "True" -and $osXP -eq "True") {$RegKeyBuild= $Reg.OpenSubKey("SOFTWARE\StandardPlatform")}
            $RegKeyDefs = $Reg.OpenSubKey("SOFTWARE\Symantec\Symantec Endpoint Protection\CurrentVersion\public-opstate")
            if ($osWIn7_type -eq "True" -and $osWin7 -eq "True") {$RegKeyBuild= $Reg.OpenSubKey("SOFTWARE\Wow6432Node\StandardPlatform")}
            $Baselines = Get-WmiObject -ComputerName $HostName -Namespace root\ccm\dcm -Class SMS_DesiredConfiguration | select DisplayName,LastComplianceStatus | where {$_.DisplayName -match “Windows*”}
            $Comp = $Baselines.Lastcompliancestatus -replace "1", "Yes" -replace "0", "No" -replace "2", "Submitted" -replace "3", "Detecting" -replace "4", "Detecting" -replace "5", "Not Evaluated"
            $RegKeyLum= $Reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{19E071C7-1823-498D-8ECC-BEE07D809CCB}")
            if ($osXP_type -eq "True" -and $osXP -eq "True") {$RegKeyLum= $Reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{5715DF37-C820-464A-9F01-32AD0470272B}")}
            $VersionLum = $RegKeyLum.GetValue("DisplayVersion")
            $computerSystem = get-wmiobject Win32_ComputerSystem -ComputerName $HostName
            $computerOS = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
            $OS = $computerOS.caption -replace "Microsoft Windows 7 Enterprise", "Win 7" -replace "Microsoft Windows XP Professional", "XP"
            $obj = new-object psobject
            $obj | add-member noteproperty Name ($computerSystem.Name)
            $obj | add-member noteproperty OS ($OS)
            $obj | add-member noteproperty Comp ($Comp)
            $obj | add-member noteproperty SEP ($RegKeySEP.GetValue("DisplayVersion") -replace "12.1.5337.5000", "12")
            $obj | add-member noteproperty AV_On ($RegKeyDefs.GetValue("AVRunningStatus") -replace "1", "Yes" -replace "2", "No")
            $obj | add-member noteproperty Lum ($VersionLum)
            $obj | add-member noteproperty VDA ($RegKeyVDA.GetValue("DisplayVersion") -replace "5.6.300.9", "5.6" -replace "5.6.400.9", "5.6")
            $resultsarray += $obj
            foreach ($HostName in $computers){
            $rows + @($resultsarray)
            $array.addrange($resultsarray)  
            $dataGridView.DataSource = $array
            $dataGridView.AutoResizeColumns()}
            $resultsarray | Export-csv c:\temp\info.csv -notypeinformation
			$outputboxappqry.text = write "Done"
}
$outputMulti.Text = write "Finished doing checks"
}

Here’s the datagridview

$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Location = New-Object System.Drawing.Size(5,80)
$dataGridView.Size=New-Object System.Drawing.Size(480,180)
$dataGridView.DataBindings.DefaultDataSourceUpdateMode = 0
$dataGridView.RowHeadersVisible = $false
$dataGridView.ColumnHeadersVisible = $true
$BuildCheckMulti.Controls.Add($dataGridView)

Also I can’t seem to clear the grid view when I run the function again

Any help would be nice

As near as I can tell, you have a nested ForEach that’s adding data to a gridview. You probably don’t want that. I’m guessing you want that nested ForEach to actually follow the first ForEach, rather than being nested inside of it.

It’s a little hard to troubleshoot this as I can’t even begin to run your script, though. Honestly, I would have built this as a completely discrete set of functions.

Get-MyData -ComputerName 1,2,3,4 | Out-MyDataGrid

Where Get-MyData does all your querying, produces a custom object as you’re doing, and then writes it to the pipeline. Out-MyDataGrid would, in a BEGIN block, instantiate the grid. Its PROCESS block would take the pipeline input and add a data grid row. This would neatly separate the two pieces of functionality and make debugging about a million times easier. Right now, it’s not modular enough to make debugging very easy.