Private/CFN/Get-StackDrift.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
function Get-StackDrift
{
    param
    (
        [Amazon.CloudFormation.Model.Stack]$Stack
    )

    if ($null -eq (Get-Command -Name Start-CFNStackDriftDetection -ErrorAction SilentlyContinue))
    {
        Write-Warning 'Upgrade your version of AWSPowerShell to see drift information'
        return $null
    }

    # Initiate drift detection
    Write-Host "Initiating drift detection"
    $detectionId = Start-CFNStackDriftDetection -StackName $stack.StackId

    # Wait for it to complete
    $status = New-Object Amazon.CloudFormation.Model.DescribeStackDriftDetectionStatusResponse
    $status.DetectionStatus = 'DETECTION_IN_PROGRESS'

    while ($status.DetectionStatus -eq 'DETECTION_IN_PROGRESS')
    {
        Start-Sleep -Seconds 1
        $status = Get-CFNStackDriftDetectionStatus -StackDriftDetectionId $detectionId
    }

    $status.StackDriftStatus
}