-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcvt_runandcheck.psm1
227 lines (168 loc) · 7.28 KB
/
dcvt_runandcheck.psm1
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
function dcvt_runandcheck (){
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
$shell=New-Object -ComObject shell.application
$wshell=New-Object -ComObject wscript.shell
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
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
}
}
}
}
#### Run ##
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules"
}
else{
$scriptRoot=$PSScriptRoot
}
$tcpath=(Split-Path -Parent $scriptRoot)+"\currentjob\TC.txt"
$tcnumber=((get-content $tcpath).split(","))[0]
$tcstep=((get-content $tcpath).split(","))[1]
$action=((get-content $tcpath).split(","))[2]
$logpath=(Split-Path -Parent $scriptRoot)+"\logs\$($tcnumber)\"
if(-not(test-path $logpath)){new-item -ItemType directory -path $logpath |out-null}
$id0=(Get-Process cmd).Id
set-location "$scriptRoot\dcvt"
$runcommand="DCVT.exe"
start-process cmd
start-sleep -s 3
$id3=(Get-Process cmd).Id|Where-object{$_ -notin $id0}
start-sleep -s 1
[Microsoft.VisualBasic.interaction]::AppActivate($id3)|out-null
Set-Clipboard $runcommand
start-sleep -s 5
[System.Windows.Forms.SendKeys]::SendWait("^v")
start-sleep -s 2
[System.Windows.Forms.SendKeys]::SendWait("~")
start-sleep -s 5
$resultpathold=(Get-ChildItem "$scriptRoot\dcvt\results\DCVT*.zip" -ErrorAction SilentlyContinue|sort creationtime |select -Last 1).fullname
do{
start-sleep -s 5
$resultpath=(Get-ChildItem "$scriptRoot\dcvt\results\DCVT*.zip" -ErrorAction SilentlyContinue|sort creationtime |select -Last 1).fullname
$resultpathbase=(Get-ChildItem "$scriptRoot\dcvt\results\DCVT*.zip" -ErrorAction SilentlyContinue|sort creationtime |select -Last 1).basename
}until($resultpath.length -gt 0 -and $resultpath -ne $resultpathold )
start-sleep -s 5
taskkill /F /PID $id3
$resultpathnew=($resultpathbase.replace("[","(") -replace "]",")").replace(" ","")
## unzip and check ##
$zip= $resultpath
$dest= "$($logpath)$($resultpathnew)\"
if(-not(test-path $dest)){new-item -ItemType directory -path $dest |out-null}
$shell.NameSpace($dest).copyhere($shell.NameSpace($zip).Items(),16)
$htmlpath=(Get-ChildItem "$dest\AssessmentResults.html" -ErrorAction SilentlyContinue).fullname
$html = New-Object -Com "HTMLFile"
$content= get-content $htmlpath
try {
# This works in PowerShell with Office installed
$html.IHTMLDocument2_write($content)
}
catch {
# This works when Office is not installed
$src = [System.Text.Encoding]::Unicode.GetBytes($content)
$html.write($src)
}
$allTablesAsObject = $html.all.tags("table") | ?{$_.id -eq "ResultsSummaryTable"} | Foreach-Object { ConvertFrom-HTMLTable $_ }
# output just 'Log name' property
$results="OK"
foreach($value in $allTablesAsObject){
if($value."Pass Rate" -ne "100%"){
$results="NG"
}
}
$index= $htmlpath
######### write log #######
Get-Module -name "outlog"|remove-module
$mdpath=(Get-ChildItem -path "C:\testing_AI\modules\" -r -file |Where-object{$_.name -match "outlog" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
#write-host "Do $action!"
outlog $action $results $tcnumber $tcstep $index
}
export-modulemember -Function dcvt_runandcheck