Cmdlet Out-GridView doesn't display data

Hello everybody!

I’m really confused facing such a problem:
I get a number of similar web-pages from a web-site, pull out a table from them and create an array of objects with properties corresponding with data from that tables. At the end of a script I’d like to output the array via Out-GridView but it outputs just the property that I programmatically set in the script. All properties that were taken from web-site are empty despite I can see them in a console output. The property names are displayed in the column headers correctly, but the property values seem to be empty.
Here is my script for you can check this behavior yourself:

$bdus = [regex]::Matches((Get-Clipboard),'BDU:\d{4}-\d{5}').value
$Result = @()
$bdus | `
    %{  
        $ht = [ordered]@{}
        $ht.Name = $_
        $url = "https://bdu.fstec.ru/vul/{0}" -f ($_ -replace 'BDU:(.+)','$1')
        $wpage = Invoke-WebRequest $url
        $vul = $wpage.ParsedHtml.GetElementsByTagName('TABLE')[0]
        $vul.rows | %{$ht.($_.Cells[0].InnerText) = $_.Cells[1].InnerText}
        $Result += New-Object -TypeName:psobject -Property:$ht
     }
$Result | Out-GridView

I’ve thought this was caused by the fact that property names are in quote marks in cyrilic with spaces but not. A simple test with similar data works fine! The next assumption was that the problem in codepages: web-site is utf-8 and system locale is windows-1251, so I tried to convert encodings:

$encodingclass = [System.Text.Encoding]
$wpencoding = $encodingclass::GetEncoding('utf-8')
$OutEncoding = $encodingclass::GetEncoding('windows-1251')
$bdus = [regex]::Matches((Get-Clipboard),'BDU:\d{4}-\d{5}').value
$Result = @()
$bdus | `
    %{  
        $ht = [ordered]@{}
        $ht.Name = $_
        $url = "https://bdu.fstec.ru/vul/{0}" -f ($_ -replace 'BDU:(.+)','$1')
        $wpage = Invoke-WebRequest $url
        $vul = $wpage.ParsedHtml.GetElementsByTagName('TABLE')[0]
        $vul.rows | %{$ht.($_.Cells[0].InnerText) = $OutEncoding.getstring($encodingclass::Convert($wpencoding, $OutEncoding, $OutEncoding.GetBytes($_.Cells[1].InnerText)))}
        $Result += New-Object -TypeName:psobject -Property:$ht
     }
$Result | Out-GridView

But result is the same((((
Please do help!!! What am I doing wrong???

Without a source or even a sample of it, I’m not sure we can offer much meaningful advice. One thing that sticks out is the old, terrible practice of

$array = @()
$array += something

This is extremely costly especially in larger data sets as it recreates the array every time you add to it. It’s best to just let Powershell collect the items for you

$array = some loop or code that outputs objects

Also, the pipe character is a natural line break, no need for a backtick. It’s also a bad habit to use backticks for formatting.

3 Likes

I wrote this script … a long time ago. I would probably rewrite it totally differently today.

But this should convert tables to PsObject.

function ConvertFrom-HtmlTable {
	param(
		[Parameter(Mandatory = $true)][Microsoft.PowerShell.Commands.HtmlWebResponseObject]$WebRequest,
		[Parameter(Mandatory = $true)][int]$TableNumber=1
	)

	$tables = @($WebRequest.ParsedHtml.getElementsByTagName("TABLE"))
	$table = $tables[$TableNumber]
	$titles = @()
	$rows = @($table.Rows)

	## Go through all of the rows in the table
	$Result = @()
	foreach($row in $rows) {
		Write-Progress -Activity "Converting HTML Table to Data" -Status " > Converting ..." -PercentComplete (100 * $rows.IndexOf($row) / $rows.count)
		$cells = @($row.Cells)
		if($cells[0].tagName -eq "TH") {
			$titles = @($cells | % { ("" + $_.InnerText).Trim() })
			continue
		}
		if(!$titles) { $titles = @(1..($cells.Count + 2) | % { "P$_" }) }
		$resultObject = [Ordered] @{}
		for($counter = 0; $counter -lt $cells.Count; $counter++) {
			$title = $titles[$counter]
			if(!$title) { continue }
			$resultObject[$title] = ("" + $cells[$counter].InnerText).Trim()
		}
		$Result += [PSCustomObject]$resultObject
	}
	return $Result


}

You can have a try, it may work … or not

1 Like

Hello!
You’re quite sure! I’ve didn’t mention that you can’t get MY clipboard))) My bad…
The user ZaMotH ansewered below with his own script that gave me a thought of what was wrong in mine one. If you wish, take a look at my answer to his post where I’ll describe the solution and my problem’s origins.
Anyway, thank you very much for your attention and answer! Special thanks for tip about backtick & a pipeline character – without backticks the sourcecode looks much elegant!
About arrays – quite whrite again, but it’s a deal of habbit: old school)))

Hello!
Thanks a lot for your help!
Your script really helped me! I’ve noticed the only but principal difference between our sourcecodes: I didn’t trimmed the strings from the web site that I’ve made an object’s properties. I’ve added this here:

and everything began to work fine! Just as wanted & needed)))
A funny thing is that I’ve noticed a whitespace at the end of property names but unfortunately have made little of it((( Live and learn)))