-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBetterFormat.ps1
More file actions
107 lines (96 loc) · 4.56 KB
/
BetterFormat.ps1
File metadata and controls
107 lines (96 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function ConvertFrom-HTMLTable {
<#
.SYNOPSIS
Function for converting ComObject HTML object to common PowerShell object.
.DESCRIPTION
Function for converting ComObject HTML object to common PowerShell object.
ComObject can be retrieved by (Invoke-WebRequest).parsedHtml or IHTMLDocument2_write methods.
In case table is missing column names and number of columns is:
- 2
- Value in the first column will be used as object property 'Name'. Value in the second column will be therefore 'Value' of such property.
- more than 2
- Column names will be numbers starting from 1.
.PARAMETER table
ComObject representing HTML table.
.PARAMETER tableName
(optional) Name of the table.
Will be added as TableName property to new PowerShell object.
.EXAMPLE
$pageContent = Invoke-WebRequest -Method GET -Headers $Headers -Uri "https://docs.microsoft.com/en-us/mem/configmgr/core/plan-design/hierarchy/log-files"
$table = $pageContent.ParsedHtml.getElementsByTagName('table')[0]
$tableContent = @(ConvertFrom-HTMLTable $table)
Will receive web page content >> filter out first table on that page >> convert it to PSObject
.EXAMPLE
$Source = Get-Content "C:\Users\Public\Documents\MDMDiagnostics\MDMDiagReport.html" -Raw
$HTML = New-Object -Com "HTMLFile"
$HTML.IHTMLDocument2_write($Source)
$HTML.body.getElementsByTagName('table') | % {
ConvertFrom-HTMLTable $_
}
Will get web page content from stored html file >> filter out all html tables from that page >> convert them to PSObjects
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[System.__ComObject] $table,
[string] $tableName
)
$twoColumnsWithoutName = 0
if ($tableName) { $tableNameTxt = "'$tableName'" }
$columnName = $table.getElementsByTagName("th") | % { $_.innerText -replace "^\s*|\s*$" }
if (!$columnName) {
$numberOfColumns = @($table.getElementsByTagName("tr")[0].getElementsByTagName("td")).count
if ($numberOfColumns -eq 2) {
++$twoColumnsWithoutName
Write-Verbose "Table $tableNameTxt has two columns without column names. Resultant object will use first column as objects property 'Name' and second as 'Value'"
} elseif ($numberOfColumns) {
Write-Warning "Table $tableNameTxt doesn't contain column names, numbers will be used instead"
$columnName = 1..$numberOfColumns
} else {
throw "Table $tableNameTxt doesn't contain column names and summarization of columns failed"
}
}
if ($twoColumnsWithoutName) {
# table has two columns without names
$property = [ordered]@{ }
$table.getElementsByTagName("tr") | % {
# read table per row and return object
$columnValue = $_.getElementsByTagName("td") | % { $_.innerText -replace "^\s*|\s*$" }
if ($columnValue) {
# use first column value as object property 'Name' and second as a 'Value'
$property.($columnValue[0]) = $columnValue[1]
} else {
# row doesn't contain <td>
}
}
if ($tableName) {
$property.TableName = $tableName
}
New-Object -TypeName PSObject -Property $property
} else {
# table doesn't have two columns or they are named
$table.getElementsByTagName("tr") | % {
# read table per row and return object
$columnValue = $_.getElementsByTagName("td") | % { $_.innerText -replace "^\s*|\s*$" }
if ($columnValue) {
$property = [ordered]@{ }
$i = 0
$columnName | % {
$property.$_ = $columnValue[$i]
++$i
}
if ($tableName) {
$property.TableName = $tableName
}
New-Object -TypeName PSObject -Property $property
} else {
# row doesn't contain <td>, its probably row with column names
}
}
}
}
$URL = "https://www.cvedetails.com/vulnerability-search.php?f=1&vendor=&product=Excel%25&cveid=&msid=&bidno=&cweid=&cvssscoremin=&cvssscoremax=&psy=&psm=&pey=&pem=&usy=&usm=&uey=&uem="
$page = Invoke-WebRequest -Uri $URL
$CVEtable = $page.ParsedHtml.getElementById("vulnslisttable")
$TableData = ConvertFrom-HTMLTable $CVEtable | ? `# -match "\d"
#$TableData | ? Complexity -eq "Medium"