Functions/Public/Export-AMHealthCheck.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
function Export-AMHealthCheck {
    <#
        .SYNOPSIS
            Exports a health check against Automate Enterprise to a file.
 
        .DESCRIPTION
            Export-AMHealthCheck exports Automate Enterprise health check results to a file.
 
        .PARAMETER HealthCheckResult
            The health check results to export.
 
        .PARAMETER IncludeZeroResult
            Include health checks with zero results, otherwise, don't include them.
 
        .PARAMETER OutputPath
            The location to save the health check results.
 
        .PARAMETER OutputFormat
            The format to save the health check results in: HTML, Word, Text, XML.
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $HealthCheckResult,

        [ValidateNotNullOrEmpty()]
        [switch]$IncludeZeroResult,
        
        [Parameter(Mandatory = $true)]
        [ValidateScript({Test-Path -Path $_})]
        $OutputPath,

        [ValidateSet("HTML","Word","Text","XML")]
        $OutputFormat = "HTML"
    )

    foreach ($connection in ($HealthCheckResult | Group-Object -Property ConnectionAlias).Name) {
        Document "Automate_Health_Check_$($connection.Replace(":","_"))_$(Get-Date -Format "yyyyMMddhhmmss")" {
            Style -Name "InformationHealthCheckHeader" -Size 12 -Color Gray
            Style -Name "WarningHealthCheckHeader" -Size 12 -Color Orange
            Style -Name "ErrorHealthCheckHeader" -Size 12 -Color Red
            DocumentOption -Margin 24
            Paragraph "Automate Health Check" -Style Heading1
            Paragraph "Generated on $(Get-Date -Format G)" -Style Heading2
            PageBreak
            TOC -Name "Overview"

            foreach ($category in ($HealthCheckResult | Where-Object {$_.ConnectionAlias -eq $connection} | Group-Object Category).Name) {
                $thisCategoryResults = $HealthCheckResult | Where-Object {$_.Category -eq $category -and $_.ConnectionAlias -eq $connection}
                if (($thisCategoryResults | Measure-Object).Count -gt 0) {
                    if (($thisCategoryResults.Results | Measure-Object).Count -gt 0 -or $IncludeZeroResult.IsPresent) {
                        PageBreak
                        Section $category {
                            foreach ($healthCheck in ($HealthCheckResult | Where-Object {$_.Category -eq $category -and $_.ConnectionAlias -eq $connection})) {
                                $thisHealthCheckResult = $thisCategoryResults | Where-Object {$_.Function -eq $healthCheck.Function -and $_.ConnectionAlias -eq $connection}
                                if (($thisHealthCheckResult | Measure-Object).Count -gt 0) {
                                    $healthCheckResultCount = ($thisHealthCheckResult.Results | Measure-Object).Count
                                    if ($healthCheckResultCount -gt 0 -or $IncludeZeroResult.IsPresent) {
                                        LineBreak
                                        if ($healthCheck.ShowCount) {
                                            $healthCheckHeader = "$($healthCheck.Name) : $healthCheckResultCount"
                                        } else {
                                            $healthCheckHeader = $healthCheck.Name
                                        }
                                        Section $healthCheckHeader {
                                            Paragraph $healthCheck.Description
                                            if ($healthCheckResultCount -gt 0) {
                                                switch ($thisHealthCheckResult.Results[0].GetType().FullName) {
                                                    "AMHealthCheckItem" {
                                                        $thisHealthCheckResult.Results | Table -Columns "Name","Value"
                                                    }
                                                    "AMConstructHealthCheckItem" {
                                                        $thisHealthCheckResult.Results | Table -Columns "Name","Path","ID","Hint"
                                                    }
                                                }
                                            }
                                        } -Style "$($healthCheck.Importance)HealthCheckHeader"
                                    } else {
                                        Write-Verbose "Health check $($healthCheck.Function) in category $($category) returned no results, will skip."
                                    }
                                } else {
                                    Write-Warning "Could not find health check $($healthCheck.Function) in category $($category) when formatting document!"
                                }
                            }
                        } -Style "Heading2"
                    } else {
                        Write-Verbose "Category $($category) had no health checks return any results, will skip."
                    }
                } else {
                    Write-Warning "Could not find category $($category) when formatting document!"
                }
            }
        } | Export-Document -Format $OutputFormat -Path $OutputPath
    }
}