Downloading an HTTP table to an array and insert to PRTG

Hi again, (a new try) thank you so much for your help!

I hope I can get some professional help since Im not coding that much :slight_smile:

Problem:

Im trying to extract information from a website with a Powershell script and then feeding these values to a PRTG monitoring server (reading values and inserting into the PRTG performance monitoring solution).

My code is based on this great work:
https://community.spiceworks.com/topic/543224-downloading-an-http-table-to-an-array

 

Im halfway :slight_smile: the error I get when running below code is:
"
WARNING: Parsing table data for Table #1
WARNING: Unable to determine headers for Table #1, skipping it
"

I have managed to get some parts of it it to work, extracting information from
innerHTML, innerText, outerHTML or outerText (below)

It seems I can select and split the lines (from innerText) with this code:

$InnerHTML = ($RawTable | select innerText).innerText -split "`n"

But then when ordering ($Fields) I get the error message since the IF clause doesnt work and ELSE clause kicks in:

$Fields = [ordered]@{}
ForEach ($Line in $Search)
{ ForEach ($Field in $Line.Matches)
{ $Fields.Add($Field.Groups[1].Value,$null)
}
}

Write-Warning "Parsing table data for Table #$TableNum"
If ($Fields.Count -gt 0)

{ $Search = $InnerHTML | Select-String -Pattern "(.*?)" -AllMatches

Write-Warning "Search2: "
$Search
$Row = [PSCustomObject]$Fields
$Index = 0
$Table = ForEach ($Match in $Search.Matches)
{ $Field = $Fields.GetEnumerator().Name[$Index]
$Row.$Field = ($Match.Groups[1].Value -replace "< .*?>","").Trim()
If ($Index -eq ($Fields.Count – 1))
{ $Index = 0
Write-Output $Row
$Row = [PSCustomObject]$Fields
}
Else { $Index ++
}
}
[PSCustomObject]@{
Raw = $InnerHTML
Data = $Table
Fields = $Fields
}
}
Else
{ Write-Warning "Unable to determine headers for Table #$TableNum, skipping it"
}

Full code below:

$ie = New-Object -ComObject "InternetExplorer.Application"
#$ie.visible = $true
$ie.silent = $true
$ie.Navigate("http://download.webpage.com/webpage/test.html")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
#while ($ie.Busy) {Start-Sleep -Milliseconds 50}

Write-Warning "Retrieving information from web"
$body = $ie.Document.getElementsByTagName("table")

Write-Warning "Tables : "
$tables = $body
$tables

$tables2 = $tables | select innerText
Write-Warning "Tables 2: "
$tables2

$RawTables = $body

Write-Verbose "Found $($RawTables.Count) tables in HTML"
$TableNum = 1

ForEach ($RawTable in $RawTables)
{ Write-Verbose "Parsing headers for Table #$TableNum"

$InnerHTML = ($RawTable | select innerText).innerText -split "`n"

Write-Warning "InnerHTML: "
$InnerHTML

$Search = $InnerHTML
Write-Warning "Search: "

$Search

$Fields = [ordered]@{}
ForEach ($Line in $Search)
{ ForEach ($Field in $Line.Matches)
{ $Fields.Add($Field.Groups[1].Value,$null)
}
}

Write-Warning "Parsing table data for Table #$TableNum"
If ($Fields.Count -gt 0)

{ $Search = $InnerHTML | Select-String -Pattern "<t[hd] ?.*?>(.*?)</t[hd]>" -AllMatches

Write-Warning "Search2: "
$Search
$Row = [PSCustomObject]$Fields
$Index = 0
$Table = ForEach ($Match in $Search.Matches)
{ $Field = $Fields.GetEnumerator().Name[$Index]
$Row.$Field = ($Match.Groups[1].Value -replace "<.*?>","").Trim()
If ($Index -eq ($Fields.Count - 1))
{ $Index = 0
Write-Output $Row
$Row = [PSCustomObject]$Fields
}
Else
{ $Index ++
}
}
[PSCustomObject]@{
Raw = $InnerHTML
Data = $Table
Fields = $Fields
}
}
Else
{ Write-Warning "Unable to determine headers for Table #$TableNum, skipping it"
}
$TableNum ++
}

# This code will need to be inserted as well (not finished)
# Sends results to PRTG for monitoring:

#"<prtg>"
#
# "<result>"
# "<channel>Javascript enabled</channel>"
# "<value>$yesno</value>"
# "</result>"

# "<result>"
# "<channel>Client IP address</channel>"
# "<value>$clientip</value>"
# "</result>"
# "<text>$returndescription</text>"

# "<result>"
# "<channel>Real connecting IP address</channel>"
# "<value>$realip</value>"
# "</result>"

# "<result>"
# "<channel>Server IP address</channel>"
# "<value>$serverip</value>"
# "</result>"

# "<text>$returndescription</text>"

# "<result>"
# "<channel>M_IP1</channel>"
# "<value>$m_ip1</value>"
# "</result>"

# "<result>"
# "<channel>M_IP2</channel>"
# "<value>$m_ip2</value>"
# "</result>"

# "<text>$returndescription</text>"

# "<result>"
# "<channel>M_IP1</channel>"
# "<value>$m_ip1</value>"
# "</result>"

# "<result>"
# "<channel>IP mapp DNS mapp</channel>"
# "<value>$imdm</value>"
# "</result>"

# "<text>$returndescription</text>"

# "<result>"
# "<channel>Serial</channel>"
# "<value>$serial</value>"
# "</result>"

# "<result>"
# "<channel>RTT Small hreq</channel>"
# "<value>$rttsmall</value>"
# "</result>"

# "<result>"
# "<channel>BW DL Small</channel>"
# "<value>$bwdlsmalldl</value>"
# "</result>"

# "<result>"
# "<channel>BW UL Med</channel>"
# "<value>$bwmedul</value>"
# "</result>"

# "<result>"
# "<channel>BW DL Large</channel>"
# "<value>$bwlargedl</value>"
# "</result>"

# "<result>"
# "<channel>BW UL Large</channel>"
# "<value>$bwdllargeul</value>"
# "</result>"

#"</prtg>"

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)
Remove-Variable ie