Downloading an HTTP table to an array and insert to PRTG

Welcome Forums General PowerShell Q&A Downloading an HTTP table to an array and insert to PRTG

This topic contains 0 replies, has 1 voice, and was last updated by  jayal197235 1 month ago.

  • Author
    Posts
  • #122282

    Participant
    Points: 8
    Rank: Member

    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 🙂

    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 🙂 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 "(.*?)" -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:
    
    #""
    #
    # ""
    # "Javascript enabled"
    # "$yesno"
    # ""
    
    # ""
    # "Client IP address"
    # "$clientip"
    # ""
    # "$returndescription"
    
    # ""
    # "Real connecting IP address"
    # "$realip"
    # ""
    
    # ""
    # "Server IP address"
    # "$serverip"
    # ""
    
    # "$returndescription"
    
    # ""
    # "M_IP1"
    # "$m_ip1"
    # ""
    
    # ""
    # "M_IP2"
    # "$m_ip2"
    # ""
    
    # "$returndescription"
    
    # ""
    # "M_IP1"
    # "$m_ip1"
    # ""
    
    # ""
    # "IP mapp DNS mapp"
    # "$imdm"
    # ""
    
    # "$returndescription"
    
    # ""
    # "Serial"
    # "$serial"
    # ""
    
    # ""
    # "RTT Small hreq"
    # "$rttsmall"
    # ""
    
    # ""
    # "BW DL Small"
    # "$bwdlsmalldl"
    # ""
    
    # ""
    # "BW UL Med"
    # "$bwmedul"
    # ""
    
    # ""
    # "BW DL Large"
    # "$bwlargedl"
    # ""
    
    # ""
    # "BW UL Large"
    # "$bwdllargeul"
    # ""
    
    #""
    
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)
    Remove-Variable ie

You must be logged in to reply to this topic.