Private/SSM/Get-SSMEnabledInstances.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
function Get-SSMEnabledInstances
{
    <#
    .SYNOPSIS
        Given a list of instance IDs, find those that are SSM enabled
        and sort into Windows and non-Windows

    .PARAMETER InstanceId
        List of instance IDs to check

    .OUTPUTS
        [PSObject] with the following fields
        - Windows List of SSM enabled Windows instances
        - NonWindows List of SSM enabled non-Windows instances
        - NonSSM List of instances that aren't SSM enabled, or SSM not ready
        - NotReady List of instances that are not running and passed status checks
#>

    param
    (
        [Parameter(Mandatory = $true)]
        [string[]]$InstanceId
    )

    $retval = New-Object PSObject -Property @{
        Windows    = $null
        NonWindows = $null
        NonSSM     = $null
        NotReady   = $null
    }

    $instances = $InstanceId
    $notReadyInstances = $null
    $nonSsmInstances = $null
    $windowsInstances = $null
    $nonWindowsInstances = $null

    # Filter down to running instances that have passed status checks
    $readyInstances = Get-EC2InstanceStatus -InstanceId $instances |
        Where-Object {

        $_.InstanceState.Code -eq 16 -and $_.Status.Status.Value -ieq 'ok' -and $_.SystemStatus.Status.Value -ieq 'ok'
    } |
        Select-Object -ExpandProperty InstanceId

    if ($readyInstances)
    {
        $notReadyInstances = Compare-Object -ReferenceObject $instances -DifferenceObject $readyInstances -PassThru
    }

    if ($notReadyInstances)
    {
        $retval.NotReady = $notReadyInstances
    }

    $instances = $readyInstances

    if (-not $instances)
    {
        # No instances ready - stop now
        return $retval
    }

    # Now filter to ones that respond to SSM
    $ssmInstances = Get-SSMInstanceInformation -Filter @{
        Key    = 'InstanceIds'
        Values = $instances
    } |
        Select-Object -ExpandProperty InstanceId

    if ($ssmInstances)
    {
        $nonSsmInstances = Compare-Object -ReferenceObject $instances -DifferenceObject $ssmInstances -PassThru
    }

    if ($nonSsmInstances)
    {
        $retval.NonSSM = $nonSsmInstances
    }

    $instances = $ssmInstances

    if (-not $instances)
    {
        # No instances SSM capable - stop now
        return $retval
    }

    # Now sort Windows from non-Windows
    $windowsInstances = (Get-EC2Instance -InstanceId $instances) |
        Select-Object -ExpandProperty Instances |
        Where-Object {
        $_.Platform -ieq 'Windows'
    } |
        Select-Object -ExpandProperty InstanceId

    if ($windowsInstances)
    {
        $nonWindowsInstances = Compare-Object -ReferenceObject $instances -DifferenceObject $windowsInstances -PassThru
    }
    else
    {
        $nonWindowsInstances = $instances
    }

    if ($windowsInstances)
    {
        $retval.Windows = $windowsInstances
    }

    if ($nonWindowsInstances)
    {
        $retval.NonWindows = $nonWindowsInstances
    }

    $retval
}