forked from Azure/azure-vm-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-AzureRmVMHealth.ps1
97 lines (92 loc) · 3.1 KB
/
Get-AzureRmVMHealth.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
param(
[string]$resourceGroupName,
[string]$name,
[string]$path,
[switch]$openJsonFile = $true
)
function Get-JsonFromSerialLog ($serialLogFilePath)
{
$serialLogFileName = split-path -Path $serialLogFilePath -Leaf
$jsonFileName = "$($serialLogFileName.SubString(0,$serialLogFileName.Length-4)).json"
$jsonFilePath = "$env:TEMP\$jsonFileName"
$serialLog = get-content -Path $serialLogFilePath
for ($i = ($serialLog.count); $i -ne 0; $i--) {
if ($serialLog[$i] -match 'Microsoft Azure VM Health Report - End')
{
$jsonString = $serialLog[$i-1]
try
{
$json = $jsonString | ConvertFrom-Json -ErrorAction SilentlyContinue
}
catch
{
write-verbose "ConvertFrom-Json failed, will try next entry"
$i--
}
if ($json) {break}
}
}
if ($json){$json | ConvertTo-Json -Depth 99 | out-file $jsonFilePath}
if (test-path $jsonFilePath)
{
get-content $jsonFilePath
"VM Health JSON: $jsonFilePath"
if ($openJsonFile)
{
invoke-item $jsonFilePath
}
}
else
{
write-host "No VM Health Report entries found."
}
}
if ($resourceGroupName -and $name)
{
$vm = get-azurermvm -ResourceGroupName $resourceGroupName -Name $name -ErrorAction Stop
$vmstatus = $vm | get-azurermvm -status -ErrorAction Stop
if ($vm.DiagnosticsProfile.Bootdiagnostics.Enabled)
{
if ($vmstatus.bootdiagnostics.ConsoleScreenshotBlobUri)
{
$consoleScreenshotBlobUri = $vmstatus.bootdiagnostics.ConsoleScreenshotBlobUri
}
else
{
"ConsoleScreenshotBlobUri property not populated"
exit
}
}
else
{
"Bootdiagnostics: $($vm.DiagnosticsProfile.Bootdiagnostics.Enabled)"
exit
}
$storageAccountName = $consoleScreenshotBlobUri.split('/')[2].split('.')[0]
$storageContainer = $consoleScreenshotBlobUri.split('/')[3]
#TODO If boot diag storage account can reside in a different RG than the VM's RG, need a different way to get the RG of the boot diag storage account
$storageContext = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName)[0].Value
$blobs = get-azurestorageblob -Container $storageContainer -Context $storageContext
$log = $blobs | where {$_.Name.EndsWith('.serialconsole.log')} | select -first 1
$log | Get-AzureStorageBlobContent -Destination $env:TEMP -Force | Out-Null
$logFilePath = "$env:TEMP\$($log.Name)"
Get-JsonFromSerialLog $logFilePath
"serial log: $logFilePath"
}
elseif ($path)
{
if (test-path $path)
{
Get-JsonFromSerialLog $path
}
else
{
Write-Error "File not found: $path"
exit
}
}
else
{
write-error "Use -resourceGroupName and -name to download the log, or -path to parse output from log already downloaded."
exit
}