AzureAutomationTools.Logging.psm1

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
Set-StrictMode -Version latest

function Export-AzureRmAutomationRunbookLog {
    param (
        # Target ResourceGroupName of the AutomationAccount
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $ResourceGroupName,

        # Automation account to pull the logs from
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $AutomationAccountName,

        # Storage account name to post the logs to
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $StorageAccountName,
        
        # Storage container to post the logs to - best solution is to create a blank container called 'logs'
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $StorageContainerName,

        # Runbook names to include
        [Parameter()]
        [string[]]
        $IncludeRunbook = '*',

        # Runbook names to exclude
        [Parameter()]
        [string[]]
        $ExcludeRunbook = $null,

        # Collect logs completed after this datetime
        [Parameter()]
        [datetime]
        $CollectLogsFrom = [datetime]::MinValue,

        # Collect logs completed until this datetime
        [Parameter()]
        [datetime]
        $CollectLogsTo = [datetime]::MaxValue
    )

    $ErrorActionPreference = 'Stop'

    #region Helper functions

    function TestRunbookInclusion {
        param (
            [Parameter(Mandatory = $true)]
            [string]
            $RunbookName
        )
        $Result = $false

        foreach ($Inclusion in $IncludeRunbook) {
            if ($RunbookName -like $Inclusion) {
                $Result = $true
                break
            }
        }

        foreach ($Exclusion in $ExcludeRunbook) {
            if ($RunbookName -like $Exclusion) {
                $Result = $false
                break
            }
        }

        return $Result
    }

    #endregion

    #region pre-Azure checks

    if ($CollectLogsTo -lt $CollectLogsFrom) {
        throw "CollectLogsFrom cannot be greater than CollectLogsTo! CollectLogsFrom: $($CollectLogsFrom.ToString('s')); CollectLogsTo: $($CollectLogsTo.ToString('s'))"
    }

    if ($null -eq $IncludeRunbook) {
        $IncludeRunbook = '*'
    }

    #endregion

    $KeySplat = @{
        ResourceGroupName = $ResourceGroupName
        Name = $StorageAccountName
    }
    $StorageAccountKeyObj = (Get-AzureRmStorageAccountKey @KeySplat)
    if ($StorageAccountKeyObj -is [System.Collections.IList]) {
        $StorageAccountKey = $StorageAccountKeyObj |
            Where-Object KeyName -eq 'Key1' |
            Select-Object -ExpandProperty 'Value'
    }
    else {
        $StorageAccountKey = $StorageAccountKeyObj.Key1
    }

    $ContextSplat = @{
        StorageAccountName = $StorageAccountName
        StorageAccountKey = $StorageAccountKey
    }
    $StorageContext = New-AzureStorageContext @ContextSplat
    $Container = Get-AzureStorageContainer -Name $StorageContainerName -Context $StorageContext


    Write-Output "Collecting jobs data..."
    $Jobs = Get-AzureRmAutomationJob -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName |
        Where-Object {
        $_.Status -In @('Completed', 'Failed', 'Stopped') -and 
        $_.EndTime.UtcDateTime -ge $CollectLogsFrom -and 
        $_.EndTime.UtcDateTime -le $CollectLogsTo -and 
        (TestRunbookInclusion -RunbookName $_.RunbookName)
    } | Get-AzureRmAutomationJob | Sort-Object -Property EndTime
            
    Write-Output "Collecting jobs data... Done."

    if ($Jobs.Count -eq 0) {
        Write-Output "Could not find any jobs."
        Write-Warning "Could not find any jobs."
        return
    }
    else {
        Write-Output "Logging $($Jobs.Count) jobs..."
    }

    # Check if job exists
    foreach ($Job in $Jobs) {
        Write-Verbose "Capturing:@{ Name = $($Job.RunbookName); Start = $($Job.CreationTime.UtcDateTime.ToString('s')) }"
        $JobName = $Job.RunbookName
        $CreateDate = $job.CreationTime.UtcDateTime
        $Year = $CreateDate.Year
        $Month = [string]::Format('{0:00}', $CreateDate.Month)
        $Day = [string]::Format('{0:00}', $CreateDate.Day)
        $JobBlobPath = "$ResourceGroupName/$AutomationAccountName/$JobName/$Year/$Month/$Day/$($Job.JobId).json"

        try {
            $CurrentEntry = Get-AzureStorageBlob -Blob $JobBlobPath -Container $StorageContainerName -Context $StorageContext
            throw "Blob '$($JobBlobPath)' already exists in '$($StorageAccountName)/$($StorageContainerName)'."
        }
        catch [Microsoft.WindowsAzure.Commands.Storage.Common.ResourceNotFoundException] {
            Write-Verbose "No entry found for Blob '$($JobBlobPath)'. Continuing with import."
        }
        
        $LogObjParams = [ordered]@{}
        foreach ($Param in ($Job | Get-Member | Where-Object MemberType -eq 'Property').Name) {
            $Value = $Job."$Param"
            $LogObjParams[$Param] = $Value
        }
        
        $Params = @{
            JobId = $Job.JobId
            ResourceGroupName = $ResourceGroupName
            AutomationAccountName = $AutomationAccountName
        }

        $Streams = [ordered]@{}
        foreach ($Stream in @('Output', 'Warning', 'Error')) {
            $StreamData = Get-AzureRmAutomationJobOutput @Params -Stream $Stream
            $StreamOutput = @()
            foreach ($Entry in $StreamData) {
                $RecordParams = @{
                    Id = $Entry.StreamRecordId
                    JobId = $Job.JobId
                    ResourceGroupName = $ResourceGroupName
                    AutomationAccountName = $AutomationAccountName
                }
                $Record = Get-AzureRmAutomationJobOutputRecord @RecordParams
                
                $StreamOutput += @{
                    StreamRecordId = $Record.StreamRecordId
                    Time = $Record.Time
                    Value = $Record.Value
                }

                Write-Verbose "Captured record $($Entry.StreamRecordId)."
            }
            $Streams[$Stream] = $StreamOutput
        }

        $LogObjParams['Streams'] = $Streams
        $LogPath = [System.IO.Path]::GetTempFileName()
        $LogObjParams | ConvertTo-Json -Depth 100 | Set-Content -Path $LogPath -Force

        Set-AzureStorageBlobContent -File $LogPath -Container $StorageContainerName -Blob $JobBlobPath -Context $StorageContext | Out-Null
        Write-Output "Successfully uploaded log: '$JobBlobPath'"
        Remove-Item -Path $LogPath -Force
    }


    Write-Output "Finished harvesting logs."
}