$rawdata = @'
First name |
Last name |
Position |
Office |
Age |
Start date |
Salary |
Extn. |
E-mail |
Tiger |
Nixon |
System Architect |
Edinburgh |
61 |
2011/04/25 |
$320,800 |
5421 |
t.nixon@datatables.net |
Garrett |
Winters |
Accountant |
Tokyo |
63 |
2011/07/25 |
$170,750 |
8422 |
g.winters@datatables.net |
Ashton |
Cox |
Junior Technical Author |
San Francisco |
66 |
2009/01/12 |
$86,000 |
1562 |
a.cox@datatables.net |
Cedric |
Kelly |
Senior Javascript Developer |
Edinburgh |
22 |
2012/03/29 |
$433,060 |
6224 |
c.kelly@datatables.net |
Airi |
Satou |
Accountant |
Tokyo |
33 |
2008/11/28 |
$162,700 |
5407 |
a.satou@datatables.net |
'@
$th = $rawdata -split "`r`n"| where-object {$_ -match ''}
$td = $rawdata -split "`r`n"| where-object {$_ -match ' | '}
$thead = (($th -replace ' | ', '') -replace ' | ', '').trim()
$tdata = (($td -replace '', '') -replace ' | ', '').trim()
$groupOf =
$result = New-Object System.Collections.ArrayList
for ($i = 0; $i -le $tdata.count; $i+= ($thead.count - 1))
{
if ($tdata.count -eq $i)
{
break
}
$group9 = $i + ($thead.count - 1)
#"{0} {1}" -f $i, $group9
[void]$result.Add($tdata[$i..$group9])
#$result = @($tdata[$i..$group9]) #-join ', '
$i++
}
$finalResult = @()
foreach ($data in $result)
{
$finalResult += [pscustomobject]@{
$thead[0] = $data[0]
$thead[1] = $data[1]
$thead[2] = $data[2]
$thead[3] = $data[3]
$thead[4] = $data[4]
$thead[5] = $data[5]
$thead[6] = $data[6]
$thead[7] = $data[7]
$thead[8] = $data[8]
}
}
$finalResult | ft -Auto
#$finalResult | ConvertTo-Json | Out-File c:\temp\database.json
#$finalResult | Export-Csv c:\temp\database.csv -NoTypeInformation
####################################################################################
####################################################################################
#Open website
$webSite = Invoke-WebRequest -Uri https://datatables.net/
#Find table in the website
$tableHeader = $webSite.AllElements | Where-Object {$_.tagname -eq 'th'}
$tableData = $webSite.AllElements | Where-Object {$_.tagname -eq 'td'}
#Table header and data
$thead = $tableHeader.innerText[0..(($tableHeader.innerText.count/2) - 1)]
$tdata = $tableData.innerText
#Break table data into smaller chuck of data.
$dataResult = New-Object System.Collections.ArrayList
for ($i = 0; $i -le $tdata.count; $i+= ($thead.count - 1))
{
if ($tdata.count -eq $i)
{
break
}
$group = $i + ($thead.count - 1)
[void]$dataResult.Add($tdata[$i..$group])
$i++
}
#Html data into powershell table format
$finalResult = @()
foreach ($data in $dataResult)
{
$newObject = New-Object psobject
for ($i = 0; $i -le ($thead.count - 1); $i++) {
$newObject | Add-Member -Name $thead[$i] -MemberType NoteProperty -value $data[$i]
}
$finalResult += $newObject
}
$finalResult | ft -AutoSize