forked from TravisEz13/DscExtDiags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectDscDiagnostics.ps1
397 lines (351 loc) · 14.2 KB
/
CollectDscDiagnostics.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
param(
[System.Management.Automation.Runspaces.PSSession] $Session
)
$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest
#
# Zips the specified folder
# returns either the path or the contents of the zip files based on the returnvalue parameterer
# When using the contents, Use set-content to create a zip file from it.
# on the specified session, if the session is not specified
# a session to the local machine will be used
#
function Get-FolderAsZip
{
[CmdletBinding()]
param(
[string]$sourceFolder,
[string] $destinationPath,
[System.Management.Automation.Runspaces.PSSession] $Session,
[ValidateSet('Path','Content')]
[string] $ReturnValue = 'Path',
[string] $filename
)
if(!$Session)
{
$Session = New-PSSession
}
$attempts =0
$gotZip = $false
while($attempts -lt 5 -and !$gotZip)
{
$attempts++
$resultTable = invoke-command -ErrorAction:Continue -Session $Session -script {
param($logFolder, $destinationPath, $fileName, $ReturnValue)
$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest
$tempPath = Join-path $env:temp ([system.io.path]::GetRandomFileName())
if(!(Test-Path $tempPath))
{
mkdir $tempPath > $null
}
$sourcePath = Join-path $logFolder '*'
Copy-Item -Recurse $sourcePath $tempPath -ErrorAction SilentlyContinue
$content = $null
$caughtError = $null
try
{
# Copy files using the Shell.
#
# Note, because this uses shell this will not work on core OSs
# But we only use this on older OSs and in test, so core OS use
# is unlikely
function Copy-ToZipFileUsingShell
{
param (
[string]
[ValidateNotNullOrEmpty()]
[ValidateScript({ if($_ -notlike '*.zip'){ throw 'zipFileName must be *.zip'} else {return $true}})]
$zipfilename,
[string]
[ValidateScript({ if(-not (Test-Path $_)){ throw 'itemToAdd must exist'} else {return $true}})]
$itemToAdd,
[switch]
$overWrite
)
Set-StrictMode -Version latest
if(-not (Test-Path $zipfilename) -or $overWrite)
{
set-content $zipfilename ('PK' + [char]5 + [char]6 + ("$([char]0)" * 18))
}
$app = New-Object -com shell.application
$zipFile = ( Get-Item $zipfilename ).fullname
$zipFolder = $app.namespace( $zipFile )
$itemToAdd = (Resolve-Path $itemToAdd).ProviderPath
$zipFolder.copyhere( $itemToAdd )
}
if(!$fileName)
{
$fileName = "$([System.IO.Path]::GetFileName($logFolder))-$((Get-Date).ToString('yyyyMMddhhmmss')).zip"
}
if($destinationPath)
{
$zipFile = Join-Path $destinationPath $fileName
if(!(Test-Path $destinationPath))
{
mkdir $destinationPath > $null
}
}
else
{
$zipFile = Join-Path ([IO.Path]::GetTempPath()) ('{0}.zip' -f $fileName)
}
if ($PSVersionTable.CLRVersion.Major -lt 4)
{
Copy-ToZipFileUsingShell -zipfilename $zipFile -itemToAdd $tempPath
$content = Get-Content $zipFile | Out-String
}
else
{
Add-Type -AssemblyName System.IO.Compression.FileSystem > $null
[IO.Compression.ZipFile]::CreateFromDirectory($tempPath, $zipFile) > $null
$content = Get-Content -Raw $zipFile
}
}
catch [Exception]
{
$caughtError = $_
}
if($ReturnValue -eq 'Path')
{
# Don't return content if we don't need it
return @{
Content = $null
Error = $caughtError
zipFilePath = $zipFile
}
}
else
{
return @{
Content = $content
Error = $caughtError
zipFilePath = $zipFile
}
}
} -argumentlist @($sourceFolder,$destinationPath, $fileName, $ReturnValue) -ErrorVariable zipInvokeError
if($zipInvokeError -or $resultTable.Error)
{
if($attempts -lt 5)
{
Write-Debug "An error occured trying to zip $sourceFolder . Will retry..."
Start-Sleep -Seconds $attempts
}
else {
if($resultTable.Error)
{
$lastError = $resultTable.Error
}
else
{
$lastError = $zipInvokeError[0]
}
Write-Warning "An error occured trying to zip $sourceFolder . Aborting."
Write-ErrorInfo -ErrorObject $lastError -WriteWarning
}
}
else
{
$gotZip = $true
}
}
if($ReturnValue -eq 'Path')
{
$result = $resultTable.zipFilePath
}
else
{
$result = $resultTable.content
}
return $result
}
#
# Tests if a parameter is a container, to be used in a ValidateScript attribute
#
function Test-ContainerParameter
{
[CmdletBinding()]
param(
[string] $Path,
[string] $Name = 'Path'
)
if(!(Test-Path $Path -PathType Container))
{
throw "$Name parameter must be a valid container."
}
return $true
}
#
# Exports an event log to a file in the path specified
# on the specified session, if the session is not specified
# a session to the local machine will be used
#
function Export-EventLog
{
[CmdletBinding()]
param(
[string] $Name,
[string] $path,
[System.Management.Automation.Runspaces.PSSession] $Session
)
Write-Verbose "Exporting eventlog $name"
$local = $false
if(!$Session)
{
Write-Verbose "Session not specified, using a local Session"
$Session = New-PSSession
$local = $true
}
invoke-command -ErrorAction:Continue -Session $Session -script {
param($name, $path)
$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest
Write-Debug "Name: $name"
Write-Debug "Path: $path"
Write-Debug "windir: $Env:windir"
$exePath = Join-Path $Env:windir 'system32\wevtutil.exe'
$exportFileName = "$($Name -replace '/','-').evtx"
$ExportCommand = "$exePath epl '$Name' '$Path\$exportFileName' /ow:True 2>&1"
Invoke-expression -command $ExportCommand
} -argumentlist @($Name, $path)
}
#
# Gathers diagnostics for DSC and the DSC Extension into a zipfile
# if specified, in the specified path
# if specified, in the specified filename
# on the specified session, if the session is not specified
# a session to the local machine will be used
#
function Get-AzureVmDscDiagnostics
{
[CmdletBinding( SupportsShouldProcess=$true, ConfirmImpact="High" )]
param(
[System.Management.Automation.Runspaces.PSSession] $Session,
[string] $destinationPath,
[string] $filename
)
$local = $false
if(!$Session)
{
$Session = New-PSSession
$local = $true
}
Function Write-ProgressMessage
{
[CmdletBinding()]
param([string]$Status, [int]$PercentComplete, [switch]$Completed)
Write-Progress -Activity 'Get-AzureVmDscDiagnostics' @PSBoundParameters
Write-Verbose -message $status
}
$privacyConfirmation = @"
Collecting the following information, which may contain private/sensative details including:
1. Logs from the Azure VM Agent, including all extensions
2. The state of the Azure DSC Extension,
including their configuration, configuration data (but not any decryption keys)
and included or generated files.
3. The DSC and application event logs.
4. The WindowsUpdate, CBS and DISM logs
5. The output of Get-Hotfix
6. The output of Get-DscLocalConfigurationManager
This tool is provided for your convience, to ensure all data is collected as quickly as possible.
Are you sure you want to continue
"@
if ($pscmdlet.ShouldProcess($privacyConfirmation))
{
$tempPath = invoke-command -ErrorAction:Continue -Session $Session -script {
$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest
$tempPath = Join-path $env:temp ([system.io.path]::GetRandomFileName())
if(!(Test-Path $tempPath))
{
mkdir $tempPath > $null
mkdir $tempPath\CBS > $null
mkdir $tempPath\DISM > $null
}
return $tempPath
}
Write-Debug -message "tempPath: $tempPath" -verbose
Write-ProgressMessage -Status 'Finding DSC and copying Extension ...' -PercentComplete 0
invoke-command -ErrorAction:Continue -Session $Session -script {
param($tempPath)
$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest
$dirs = @(Get-ChildItem C:\Packages\Plugins\Microsoft.Powershell.*DSC -ErrorAction SilentlyContinue)
$dir = $null
if($dirs.Count -ge 1)
{
$dir = @(Get-ChildItem C:\Packages\Plugins\Microsoft.Powershell.*DSC -ErrorAction SilentlyContinue)[0].FullName
}
if($dir)
{
Write-Verbose -message "Found DSC extension at: $dir" -verbose
Copy-Item -Recurse $dir $tempPath\DscPackageFolder -ErrorAction SilentlyContinue
Get-ChildItem "$tempPath\DscPackageFolder" -Recurse | %{
if($_.Extension -ieq '.msu')
{
$newFileName = "$($_.FullName).wasHere"
Get-ChildItem $_.FullName | Out-String | Out-File $newFileName -Force
$_.Delete()
}
}
}
else
{
Write-Verbose -message "Did not find DSC extension." -verbose
}
} -argumentlist @($tempPath)
Write-ProgressMessage -Status 'Copying log files..' -PercentComplete 1
invoke-command -ErrorAction:Continue -Session $Session -script {
param($tempPath)
$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest
Copy-Item -Recurse C:\WindowsAzure\Logs $tempPath\WindowsAzureLogs -ErrorAction SilentlyContinue
Copy-Item $env:windir\WindowsUpdate.log $tempPath\WindowsUpdate.log -ErrorAction SilentlyContinue
Copy-Item $env:windir\logs\CBS\*.* $tempPath\CBS -ErrorAction SilentlyContinue
Copy-Item $env:windir\logs\DISM\*.* $tempPath\DISM -ErrorAction SilentlyContinue
Get-HotFix | Out-String | Out-File $tempPath\HotFixIds.txt
Get-DscLocalConfigurationManager | Out-String | Out-File $tempPath\Get-dsclcm.txt
} -argumentlist @($tempPath)
Write-ProgressMessage -Status 'Getting DSC Event log ...' -PercentComplete 25
Export-EventLog -Name Microsoft-Windows-DSC/Operational -Path $tempPath -session $session
Write-ProgressMessage -Status 'Getting Application Event log ...' -PercentComplete 50
Export-EventLog -Name Application -Path $tempPath -session $session
if(!$destinationPath)
{
Write-ProgressMessage -Status 'Getting destinationPath ...' -PercentComplete 74
$destinationPath = invoke-command -ErrorAction:Continue -Session $Session -script {
$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest
Join-path $env:temp ([system.io.path]::GetRandomFileName())
}
}
Write-Debug -message "destinationPath: $destinationPath" -verbose
$zipParams = @{
sourceFolder = $tempPath
destinationPath = $destinationPath
Session = $session
fileName = $fileName
}
Write-ProgressMessage -Status 'Zipping files ...' -PercentComplete 75
if($local)
{
$zip = Get-FolderAsZip @zipParams
$zipPath = $zip
}
else
{
$zip = Get-FolderAsZip @zipParams -ReturnValue 'Content'
if(!(Test-Path $destinationPath))
{
mkdir $destinationPath > $null
}
$zipPath = (Join-path $destinationPath "$($session.ComputerName)-dsc-diags-$((Get-Date).ToString('yyyyMMddhhmmss')).zip")
set-content -path $zipPath -value $zip
}
Start-Process $destinationPath
Write-Verbose -message "Please send this zip file the engineer you have been working with. The engineer should have emailed you instructions on how to do this: $zipPath" -verbose
Write-ProgressMessage -Completed
return $zipPath
}
}
Get-AzureVmDscDiagnostics @PSBoundParameters