Functions/Public/Get-AMHealthCheckConfiguration.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
function Get-AMHealthCheckConfiguration {
    <#
        .SYNOPSIS
            Gets the configuration for the health check
 
        .DESCRIPTION
            Get-AMHealthCheckConfiguration pulls the health check configuration from config.json
 
        .PARAMETER CustomConfigPath
            The path to a JSON file containing health check configuration settings. This file can include new, custom health checks, or modifications to built in health checks.
    #>

    [CmdletBinding()]
    param (
        [ValidateScript({Test-Path -Path $_})]
        $CustomConfigPath
    )

    $moduleRoot = (Get-Item -Path $PSCommandPath).Directory.Parent.Parent
    $config = Get-Content -Path "$($moduleRoot.FullName)\Config\config.json" | ConvertFrom-Json
    if ($CustomConfigPath) {
        $customConfig = Get-Content -Path $CustomConfigPath | ConvertFrom-Json
        foreach ($category in $customConfig.Categories) {
            # Add new categories
            if ($config.Categories.Name -notcontains $category.Name) {
                $config.Categories += $category
            } else {
                $defaultCategory = $config.Categories | Where-Object {$_.Name -eq $category.Name}
                # Process Enabled property
                if ($null -ne $category.Enabled) {
                    if ($category.Enabled -is [Boolean]) {
                        $defaultCategory.Enabled = $category.Enabled
                    } else {
                        Write-Warning "Boolean value not specified for Enabled property in category $($category.Name)!"
                    }
                }

                # Process SortOrder property
                if ($null -ne $category.SortOrder) {
                    if ($category.SortOrder -is [int]) {
                        $defaultCategory.SortOrder = $category.SortOrder
                    } else {
                        Write-Warning "Integer value not specified for SortOrder property in category $($category.Name)!"
                    }
                }

                # Process HealthChecks
                foreach ($healthCheck in $category.HealthChecks) {
                    if ($defaultCategory.HealthChecks.Name -notcontains $healthCheck.Name) {
                        $defaultCategory.HealthChecks += $healthCheck
                    } else {
                        $defaultHealthCheck = $defaultCategory.HealthChecks | Where-Object {$_.Name -eq $healthCheck.Name}
                        # Process Description property
                        if ($null -ne $healthCheck.Description) {
                            $defaultHealthCheck.Description = $healthCheck.Description
                        }

                        # Process Function property
                        if ($null -ne $healthCheck.Function) {
                            if (Get-Command -Name $healthCheck.Function -ErrorAction SilentlyContinue) {
                                $defaultHealthCheck.Function = $healthCheck.Function
                            } else {
                                Write-Warning "Unrecognized Function '$($healthCheck.Function)' specified for health check $($healthCheck.Name) in category $($category.Name)!"
                            }
                        }

                        # Process Importance property
                        if ($null -ne $healthCheck.Importance) {
                            if ($healthCheck.Importance -in [AMHealthCheckImportance].GetEnumValues()) {
                                $defaultHealthCheck.Importance = $healthCheck.Importance
                            } else {
                                Write-Warning "Unrecognized Importance '$($healthCheck.Importance)' specified for health check $($healthCheck.Name) in category $($category.Name)!"
                            }
                        }

                        # Process ShowCount property
                        if ($null -ne $healthCheck.ShowCount) {
                            if ($healthCheck.ShowCount -is [Boolean]) {
                                $defaultHealthCheck.ShowCount = $healthCheck.ShowCount
                            } else {
                                Write-Warning "Boolean value not specified for ShowCount property in health check $($healthCheck.Name) in category $($category.Name)!"
                            }
                        }
        
                        # Process Enabled property
                        if ($null -ne $healthCheck.Enabled) {
                            if ($healthCheck.Enabled -is [Boolean]) {
                                $defaultHealthCheck.Enabled = $healthCheck.Enabled
                            } else {
                                Write-Warning "Boolean value not specified for Enabled property in health check $($healthCheck.Name) in category $($category.Name)!"
                            }
                        }
        
                        # Process SortOrder property
                        if ($null -ne $healthCheck.SortOrder) {
                            if (($healthCheck.SortOrder -is [int16]) -or `
                                ($healthCheck.SortOrder -is [int32]) -or `
                                ($healthCheck.SortOrder -is [int64])) {
                                $defaultHealthCheck.SortOrder = $healthCheck.SortOrder
                            } else {
                                Write-Warning "Integer value not specified for SortOrder property in health check $($healthCheck.Name) in category $($category.Name)!"
                            }
                        }
                    }
                }
            }
        }
    }
    return $config
}