public/Get-AzWvdSessionHostRDBrokerHealth.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
Function Get-AzWvdSessionHostRDBrokerHealth {

    [CmdletBinding(
        SupportsShouldProcess = $True,
        ConfirmImpact = "High")]

    Param (

        [ValidateSet(
            "BrokerURI"
            , "BrokerURIGlobal"
            , "DiagnosticsURI"
        )]
        [ArgumentCompleter({ "BrokerURI" , "BrokerURIGlobal" , "DiagnosticsURI" })]
        [Parameter(
            Mandatory = $False
            , ValueFromPipeline = $True
            , ValueFromPipelineByPropertyName = $True
            , ParameterSetName = "HealthCheck"
        )]
        [Alias("C")]
        [string]
        $Check,

        [Parameter(
            Mandatory = $False
            , ValueFromPipeline = $True
            , ValueFromPipelineByPropertyName = $True
            , ParameterSetName = "HealthCheck"
        )]
        [Alias("R", "Report")]
        [switch]
        $HealthReport,

        [Parameter(
            Mandatory = $False
            , ValueFromPipeline = $True
            , ValueFromPipelineByPropertyName = $True
        )]
        [switch]
        $Raw
    )

    Begin {}

    Process {

        if ($PSBoundParameters.ContainsKey("Check")) {

            if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\RDInfraAgent" -PathType "Container") {

                $Response = Invoke-WebRequest `
                    -Uri "$(Get-ItemPropertyValue `
                        -Path "HKLM:\SOFTWARE\Microsoft\RDInfraAgent" `
                        -Name $Check)/api/health"
 `
                    -UseBasicParsing | Select-Object `
                    -ExpandProperty "Content"
            }
        }
        else {

            if (!(Test-Path -Path "HKLM:\SOFTWARE\Microsoft\RDInfraAgent" -PathType "Container")) {

                $Uri = "https://rdbroker.wvdselfhost.microsoft.com"

                $Response = Invoke-WebRequest `
                    -Uri "$Uri/api/health" `
                    -UseBasicParsing | Select-Object `
                    -ExpandProperty "Content"

            }
            else {

                $Response = @("BrokerURI", "BrokerURIGlobal", "DiagnosticsURI") | ForEach-Object -Process {

                    Invoke-WebRequest `
                        -Uri "$(Get-ItemPropertyValue `
                        -Path "HKLM:\SOFTWARE\Microsoft\RDInfraAgent" `
                        -Name $_)/api/health"
 `
                        -UseBasicParsing | Select-Object `
                        -ExpandProperty "Content"
                }
                
            }
        }

        if ($PSBoundParameters.ContainsKey("HealthReport")) {

            $HealthReportsObject = @()
            
            if ([string]::IsNullOrEmpty($Check)) {

                $HealthCheck = @("BrokerURI", "BrokerURIGlobal", "DiagnosticsURI")
            }
            else {

                $HealthCheck = @($Check)
            }

            $ArrayIndex = 0

            $Response | ConvertFrom-Json | ForEach-Object -Process {

                ($_.HealthCheckStatus.HealthReport.Entries).PSObject.Properties | ForEach-Object -Process {

                    $HealthReportObject = [PSCustomObject]@{
                        HealthReport =  "$($HealthCheck[$ArrayIndex]) - $($_.Name)"
                    }
            
                    ($_.Value).PSObject.Properties | ForEach-Object -Process {
            
                        $HealthReportObject | Add-Member `
                            -MemberType NoteProperty `
                            -Name $_.Name `
                            -Value $_.Value
                    }

                    $HealthReportsObject += $HealthReportObject
                }
                    
                $ArrayIndex++
            }

            $Response = $HealthReportsObject | ConvertTo-Json
        }

    }

    End {

        if ($PSBoundParameters.ContainsKey("Raw")) {
            $Response | Write-Output
        }
        else {
            $Response | ConvertFrom-Json | Write-Output
        }
    }

}