Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

[PSCustomObject] does not return all colums (Powershell)

$
0
0

Hi

Experts, I am new in powershell and I did copy/paste and adjusted code following code from internet to accomplish my need, please excuse me for asking silly questions if it so.

I am just using following powershell code to get SQL version data from one portal but it should return 4 columns instead of 2 columns:

$url = 'http://sqlserverversions.blogspot.in'
$HTML= Invoke-WebRequest -Uri $URL
$HTMLTableNumber="3"
#$RawTables=$HTML.ParsedHtml.getElementsByTagName("TABLE")
$RawTables=@($HTML.ParsedHtml.getElementsByTagName("TABLE"))
$RawTable = $RawTables[$HTMLTableNumber] #temp
ForEach ($RawTable in $RawTables)
{  

 $titles = @()
 $rows = @($RawTable.Rows)


 foreach($row in $rows)

  {
    $cells = @($row.Cells)
    if($cells[0].tagName -eq "TH")
    {
        $titles = @($cells | % { ("" + $_.InnerText).Trim() })
        continue
    }

    if(-not $titles) {$titles = @(1..($cells.Count + 2) | % { "Col$_" })}

    $resultObject = [Ordered] @{}
    for($counter = 0; $counter -lt $cells.Count; $counter++)
    {
        $title = $titles[$counter]
        if(-not $title) { continue }
       
#$cells[$counter].InnerText
        $resultObject[$title] = ("" + $cells[$counter].InnerText).Trim()

    }

    [PSCustomObject] $resultObject    
  }
}

If I don't put the looping on table i.e. ForEach ($RawTable in $RawTables) and adjust the code to work to get only single HTML table then same code return correct data.

Can someone help on this?

I also wanted to know more on following statement as why we are using ("" in statement, seems it is one liner statement which always creates trouble:

$resultObject[$title] = ("" + $cells[$counter].InnerText).Trim()

Bobby


Viewing all articles
Browse latest Browse all 21975