Private/Write-WordReportSection.ps1

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
Function Write-WordReportSection {
    param (
        [parameter()] $HealthCheckXML,
        [parameter()] $Section,
        [parameter()] $Detailed = $false,
        [parameter()] $Doc,
        [parameter()] $Selection,
        [parameter()] $LogFile
    )
    Write-Log -Message "(Write-WordReportSection)" -LogFile $logfile
    Write-Log -Message "section....... $section" -LogFile $logfile
    Write-Log -Message "detail........ $($detailed.ToString())" -LogFile $logfile
    
    foreach ($healthCheck in $HealthCheckXML.dtsHealthCheck.HealthCheck) {
        if ($healthCheck.Section.ToLower() -ne $Section) { continue }
        $Description = $healthCheck.Description -replace("@@NumberOfDays@@", $NumberOfDays)
        if ($healthCheck.IsActive.ToLower() -ne 'true') { continue }
        if ($healthCheck.IsTextOnly.ToLower() -eq 'true') {
            if ($Section -eq 5) {
                if ($detailed -eq $false) { 
                    $Description += " - Overview" 
                } 
                else { 
                    $Description += " - Detailed"
                }            
            }
            Write-WordText -WordSelection $selection -Text $Description -Style $healthCheck.WordStyle -NewLine $true
            Continue;
        }
        Write-Log -Message "description... $Description" -LogFile $logfile
        Write-WordText -WordSelection $selection -Text $Description -Style $healthCheck.WordStyle -NewLine $true
        $bFound = $false
        $tableName = $healthCheck.XMLFile
        if ($Section -eq 5) {
            if (!($detailed)) { 
                $tablename += "summary" 
            } 
            else { 
                $tablename += "detail"
            }            
        }
        foreach ($rp in $ReportTable) {
            if ($rp.TableName -eq $tableName) {
                $bFound = $true
                $filename = $rp.XMLFile
                Write-Log -Message "xmlfile....... $filename" -LogFile $logfile
                if ($filename.IndexOf("_") -gt 0) {
                    $xmltitle = $filename.Substring(0,$filename.IndexOf("_"))
                    $xmltile = ($rp.TableName.Substring(0,$rp.TableName.IndexOf("_")).Replace("@","")).Tolower()
                    switch ($xmltile) {
                        "sitecode"   { $xmltile = "Site Code: " }
                        "servername" { $xmltile = "Server Name: " }
                    }
                    switch ($healthCheck.WordStyle) {
                        "Heading 1" { $newstyle = "Heading 2" }
                        "Heading 2" { $newstyle = "Heading 3" }
                        "Heading 3" { $newstyle = "Heading 4" }
                        default { $newstyle = $healthCheck.WordStyle }
                    }
                    $xmltile += $filename.Substring(0,$filename.IndexOf("_"))
                    Write-WordText -WordSelection $selection -Text $xmltile -Style $newstyle -NewLine $true
                }
                if (!(Test-Path ($reportFolder + $filename))) {
                    Write-WordText -WordSelection $selection -Text $healthCheck.EmptyText -NewLine $true
                    Write-Log -Message "Table does not exist" -LogFile $logfile -Severity 2
                    $selection.TypeParagraph()
                }
                else {
                    #Write-Log -Message "importing XML file: $filename" -LogFile $logfile
                    $datatable = Import-CliXml -Path ($reportFolder + $filename)
                    $count = 0
                    $datatable | Where-Object { $count++ }
                    if ($count -eq 0) {
                        Write-WordText -WordSelection $selection -Text $healthCheck.EmptyText -NewLine $true
                        Write-Log -Message "Table......... 0 rows" -LogFile $logfile -Severity 2
                        $selection.TypeParagraph()
                        continue
                    }
                    switch ($healthCheck.PrintType.ToLower()) {
                        "table" {
                            Write-Log -Message "table type.... table" -LogFile $logfile
                            $Table = $Null
                            $TableRange = $Null
                            $TableRange = $doc.Application.Selection.Range
                            $Columns = 0
                            foreach ($field in $HealthCheck.Fields.Field) {
                                if ($section -eq 5) {
                                    if (($detailed) -and ($field.groupby -notin ('1','2'))) { continue }
                                    elseif ((!($detailed)) -and ($field.groupby -notin ('2','3'))) { continue }
                                }
                                $Columns++
                            } # foreach
                            $Table = $doc.Tables.Add($TableRange, $count+1, $Columns)
                            Write-Log -Message "table style... $TableStyle" -LogFile $logfile
                            $table.Style = $TableStyle
                            # added to force table width consistency in 1.0.4 (Issue 13)
                            $table.PreferredWidthType = 2
                            $table.PreferredWidth = 100
                            $i = 1;
                            Write-Log -Message "structure..... $count rows and $Columns columns" -LogFile $logfile
                            Write-Log -Message "writing table column headings..." -LogFile $logfile
                            foreach ($field in $HealthCheck.Fields.Field) {
                                if ($section -eq 5) {
                                    if (($detailed) -and ($field.groupby -notin ('1','2'))) { continue }
                                    elseif ((!($detailed)) -and ($field.groupby -notin ('2','3'))) { continue }
                                }
                                $Table.Cell(1, $i).Range.Font.Bold = $True
                                $Table.Cell(1, $i).Range.Text = $field.Description
                                #Write-Log -Message "--column: $($field.Description)" -LogFile $logfile
                                $i++
                            } # foreach
                            $xRow = 2
                            $records = 1
                            $y=0
                            Write-Log -Message "writing data rows for table body..." -LogFile $logfile
                            foreach ($row in $datatable) {
                                if ($records -ge 500) {
                                    Write-Log -Message ("Exported..... $(500*($y+1)) records") -LogFile $logfile
                                    $records = 1
                                    $y++
                                }
                                $i = 1;
                                foreach ($field in $HealthCheck.Fields.Field) {
                                    if ($section -eq 5) {
                                        if (($detailed) -and ($field.groupby -notin ('1','2'))) { continue }
                                        elseif ((!($detailed)) -and ($field.groupby -notin ('2','3'))) { continue }
                                    }
                                    $Table.Cell($xRow, $i).Range.Font.Bold = $false
                                    $TextToWord = "";
                                    switch ($field.Format.ToLower()) {
                                        "message" {
                                            $TextToWord = Get-MessageInformation -MessageID ($row.$($field.FieldName))
                                        }
                                        "messagesolution" {
                                            $TextToWord = Get-MessageSolution -MessageID ($row.$($field.FieldName))
                                        }                                        
                                        default {
                                            $TextToWord = $row.$($field.FieldName);
                                        }
                                    }
                                    #Write-Log -Message "--value: $($TextToWord.ToString())" -LogFile $logfile
                                    if ([string]::IsNullOrEmpty($TextToWord)) { 
                                        $TextToWord = " " 
                                        $val = " "
                                    }
                                    elseif (Test-Numeric $TextToWord) {
                                        #Write-Log -Message "rounding numeric value precision" -LogFile $logfile
                                        $val = ([math]::Round($TextToWord,2)).ToString()
                                    }
                                    else {
                                        $val = $TextToWord.ToString()
                                    }
                                    $Table.Cell($xRow, $i).Range.Text = $val
                                    $i++
                                } # foreach
                                $xRow++
                                $records++
                            } # foreach
                            $selection.EndOf(15) | Out-Null
                            $selection.MoveDown() | Out-Null
                            $doc.ActiveWindow.ActivePane.view.SeekView = 0
                            $selection.EndKey(6, 0) | Out-Null
                            if ($count -gt 2) {
                                Write-Verbose "SORT OPERATION - SORTING TABLE"
                                $Tables.Sort
                                Write-Log -Message "NEW: appending row count label below table" -LogFile $logfile
                                Write-WordText -WordSelection $selection -Text "$count items found" -Style "Normal" -NewLine $true
                                $selection.TypeParagraph()
                            }
                            $selection.TypeParagraph()
                        }
                        "simpletable" {
                            Write-Log -Message "table type.... simpletable" -LogFile $logfile
                            $Table = $Null
                            $TableRange = $Null
                            $TableRange = $doc.Application.Selection.Range
                            $Columns = 0
                            foreach ($field in $HealthCheck.Fields.Field) {
                                if ($section -eq 5) {
                                    if (($detailed) -and ($field.groupby -notin ('1','2'))) { continue }
                                    elseif ((!($detailed)) -and ($field.groupby -notin ('2','3'))) { continue }
                                }
                                $Columns++
                            } # foreach
                            $Table = $doc.Tables.Add($TableRange, $Columns, 2)
                            $table.Style = $TableSimpleStyle
                            # added to force table width consistency in 1.0.4 (Issue 13)
                            $table.PreferredWidthType = 2
                            $table.PreferredWidth = 100
                            $i = 1;
                            Write-Log -Message "structure..... $Columns rows and 2 columns" -LogFile $logfile
                            $records = 1
                            $y=0
                            foreach ($field in $HealthCheck.Fields.Field) {
                                if ($section -eq 5) {
                                    if (($detailed) -and ($field.groupby -notin ('1','2'))) { continue }
                                    elseif ((!($detailed)) -and ($field.groupby -notin ('2','3'))) { continue }
                                }
                                if ($records -ge 500) {
                                    Write-Log -Message ("Exported..... $(500*($y+1)) records") -LogFile $logfile
                                    $records = 1
                                    $y++
                                }
                                $Table.Cell($i, 1).Range.Font.Bold = $true
                                $Table.Cell($i, 1).Range.Text = $field.Description
                                $Table.Cell($i, 2).Range.Font.Bold = $false
                                if ($poshversion -ne 3) { 
                                    $TextToWord = "";
                                    switch ($field.Format.ToLower()) {
                                        "message" {
                                            $TextToWord = Get-MessageInformation -MessageID ($datatable.Rows[0].$($field.FieldName))
                                        }
                                        "messagesolution" {
                                            $TextToWord = Get-MessageSolution -MessageID ($datatable.Rows[0].$($field.FieldName))
                                        }                                            
                                        default {
                                            $TextToWord = $datatable.Rows[0].$($field.FieldName)
                                        }
                                    } # switch
                                    if ([string]::IsNullOrEmpty($TextToWord)) { $TextToWord = " " }
                                    $Table.Cell($i, 2).Range.Text = $TextToWord.ToString()
                                }
                                else {
                                    $TextToWord = "";
                                    switch ($field.Format.ToLower()) {
                                        "message" {
                                            $TextToWord = Get-MessageInformation -MessageID ($datatable.$($field.FieldName))
                                        }
                                        "messagesolution" {
                                            $TextToWord = Get-MessageSolution -MessageID ($datatable.$($field.FieldName))
                                        }                                            
                                        default {
                                            $TextToWord = $datatable.$($field.FieldName) 
                                        }
                                    } # switch
                                    if ([string]::IsNullOrEmpty($TextToWord)) { $TextToWord = " " }
                                    $Table.Cell($i, 2).Range.Text = $TextToWord.ToString()
                                }
                                $i++
                                $records++
                            } # foreach
                            $selection.EndOf(15) | Out-Null
                            $selection.MoveDown() | Out-Null
                            $doc.ActiveWindow.ActivePane.View.SeekView = 0
                            $selection.EndKey(6, 0) | Out-Null
                            $selection.TypeParagraph()
                        }
                        default {
                            Write-Log -Message "table type.... default" -LogFile $logfile
                            $records = 1
                            $y=0
                            foreach ($row in $datatable) {
                                if ($records -ge 500) {
                                    Write-Log -Message ("Exported...... $(500*($y+1)) records") -LogFile $logfile
                                    $records = 1
                                    $y++
                                }
                                foreach ($field in $HealthCheck.Fields.Field) {
                                    $TextToWord = "";
                                    switch ($field.Format.ToLower()) {
                                        "message" {
                                            $TextToWord = ($field.Description + " : " + (Get-MessageInformation -MessageID ($row.$($field.FieldName))))
                                        }
                                        "messagesolution" {
                                            $TextToWord = ($field.Description + " : " + (Get-MessageSolution -MessageID ($row.$($field.FieldName))))
                                        }                                                
                                        default {
                                            $TextToWord = ($field.Description + " : " + $row.$($field.FieldName))
                                        }
                                    } # switch
                                    if ([string]::IsNullOrEmpty($TextToWord)) { $TextToWord = " " }
                                    Write-WordText -WordSelection $selection -Text ($TextToWord.ToString()) -NewLine $true
                                } # foreach
                                $selection.TypeParagraph()
                                $records++
                            } # foreach
                        } # end of default switch case
                    } # switch
                    Write-WordTableGrid -Caption "Review Comments" -Rows 3 -ColumnHeadings $ReviewTableCols -StyleName $ReviewTableStyle
                }
            }
        } # foreach
        if ($bFound -eq $false) {
            Write-WordText -WordSelection $selection -Text $healthCheck.EmptyText -NewLine $true
            Write-Log -Message ("Table does not exist") -LogFile $logfile -Severity 2
            $selection.TypeParagraph()
        }
    } # foreach
}