Public/SDK Wrappers/CloudFormation/Watch-Stack.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
function Watch-Stack {
    <#
    .SYNOPSIS
    Watches Stack Events as they happen. Colorizes events based on event type.
    
    .PARAMETER StackId
    The Stack ID or name of the stack that you'd like to watch events for.
    
    .PARAMETER InNewWindow
    WINDOWS ONLY (For now). Watch events in a new PowerShell window. So you can continue working in your current console.
    
    .PARAMETER RefreshRate
    The rate in seconds that you'd like the event list to poll for new events. Defaults to 2.

    .PARAMETER ProfileName
    The name of the configuration profile to deploy the stack with. Defaults to $env:AWS_PROFILE, if set.
    
    .FUNCTIONALITY
    Vaporshell
    #>

    Param
    (
        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("StackId")]
        [String]
        $StackName,
        [parameter(Mandatory = $false)]
        [Switch]
        $InNewWindow,
        [parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [Int]
        $RefreshRate = 2,
        [parameter(Mandatory = $false)]
        [String]
        $ProfileName = $env:AWS_PROFILE
    )
    if ($InNewWindow) {
        if ($Env:OS -and ($Env:OS -notlike "*Windows*")) {
            Write-Warning "You must be running Windows to use the '-InNewWindow' parameter -- returning to prompt"
            return
        }
        else {
            $command = "Import-Module `"$($Script:VaporshellPath)\Vaporshell.psd1`" -ArgumentList `$false,`$true -Verbose:`$false;Clear-Host;. '$($Script:VaporshellPath)\bin\SetWatchWindow.ps1' -StackId '$StackName';Watch-Stack -StackName '$StackName' -RefreshRate $RefreshRate"
            if ($ProfileName) {
                $command += " -ProfileName '$ProfileName'"
            }
            Start-Process powershell -ArgumentList "-noprofile -noexit -command & {$command}"
        }
    }
    else {
        $i = 0
        $cont = $true
        $prof = @{}
        if ($ProfileName) {
            $prof["ProfileName"] = $ProfileName
        }
        $strings = @()
        $head = "`nSTACK NAME : $StackName`nREFRESH : $RefreshRate seconds`n"
        Colorize $head
        do {
            try {
                $results = Get-VSStack -Events -StackId "$StackName" @prof -ErrorAction Stop -Verbose:$false
                $StackName = $results[0].StackId
                $stack = ($results | Sort-Object timestamp | Select-Object Timestamp,ResourceStatus,StackName,ResourceType,ResourceStatusReason | Format-Table -AutoSize | Out-String) -split "`n" | Where-Object {!([String]::IsNullOrWhiteSpace($_)) -and $strings -notcontains $_.Trim()}
                $strings += $stack | ForEach-Object {$_.Trim()}
                Colorize $stack
                if ($i -ge 1 -and $stack -clike '*_COMPLETE*AWS::CloudFormation::Stack*') {
                    $cont = $false
                }
                else {
                    Start-Sleep $RefreshRate
                    $i++
                }
            }
            catch {
                $PSCmdlet.ThrowTerminatingError($_)
                $cont = $false
            }
        }
        until ($cont -eq $false)
    }
}