-
Notifications
You must be signed in to change notification settings - Fork 0
/
Azure_Gateway_Diagnostics.ps1
140 lines (121 loc) · 6.98 KB
/
Azure_Gateway_Diagnostics.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
#------------------------------------------------------------------------------
#
# Copyright © 2014 Microsoft Corporation. All rights reserved.
#
# THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED “AS IS” WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
# RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#
#------------------------------------------------------------------------------
#
# PowerShell Source Code
#
# NAME:
# Azure_Gateway_Diagnostics.ps1
#
# VERSION:
# 2.0
#
#------------------------------------------------------------------------------
"------------------------------------------------------------------------------ " | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
" Copyright © 2014 Microsoft Corporation. All rights reserved. " | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
" THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED `“AS IS`” WITHOUT " | Write-Host -ForegroundColor Yellow
" WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT " | Write-Host -ForegroundColor Yellow
" LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS " | Write-Host -ForegroundColor Yellow
" FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR " | Write-Host -ForegroundColor Yellow
" RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER. " | Write-Host -ForegroundColor Yellow
"------------------------------------------------------------------------------ " | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
" PowerShell Source Code " | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
" NAME: " | Write-Host -ForegroundColor Yellow
" Azure_Gateway_Diagnostics.ps1 " | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
" VERSION: " | Write-Host -ForegroundColor Yellow
" 2.0" | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
"------------------------------------------------------------------------------ " | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
"`n This script SAMPLE is provided and intended only to act as a SAMPLE ONLY," | Write-Host -ForegroundColor Yellow
" and is NOT intended to serve as a solution to any known technical issue." | Write-Host -ForegroundColor Yellow
"`n By executing this SAMPLE AS-IS, you agree to assume all risks and responsibility associated." | Write-Host -ForegroundColor Yellow
$ErrorActionPreference = "SilentlyContinue"
$ContinueAnswer = Read-Host "`n Do you wish to proceed at your own risk? (Y/N)"
If ($ContinueAnswer -ne "Y") { Write-Host "`n Exiting." -ForegroundColor Red;Exit }
#import module
Write-Host "`n Checking Azure PowerShell module" -ForegroundColor Cyan
Import-Module Azure
#check module version
$modver = (Get-Module azure).version
$PSMinor =$modver.Minor
$PSBuild =$modver.Build
If ($PSMinor -ne 8 -or $PSBuild -lt 11){ Write-Host "`n FAILED: Azure PowerShell update required`n`thttp://go.microsoft.com/fwlink/p/?linkid=320376`n" -fore red;Exit } Else { Write-Host "`tConfirmed" -fore Green }
#remove existing subs
If (Get-AzureSubscription) { Get-AzureSubscription | Remove-AzureSubscription -Force }
#auth
Write-Host "`n Authenticating to your Azure account" -ForegroundColor Cyan
Add-AzureAccount | Out-Null
#select sub
Write-Host "`n Listing available Azure subscriptions:" -ForegroundColor Cyan
If (Get-AzureSubscription) { Get-AzureSubscription | Select SubscriptionName,SubscriptionId | FT } Else { Write-Host "`n FAILED: No subscriptions found`n" -fore red;Exit }
$selSub = Read-Host "`n Select SubscriptionID"
If (!(Get-AzureSubscription -SubscriptionId $selSub -ErrorAction SilentlyContinue)){ Write-Host "`n FAILED: Invalid SubscriptionId`n" -fore red;Exit }
Select-AzureSubscription -SubscriptionId $selSub | Out-Null
#select storage
Write-Host "`n Listing available Storage Accounts:" -ForegroundColor Cyan
Get-AzureStorageAccount | Select StorageAccountName | FT
$san=Read-Host "`n Select Storage Account Name"
If (!(Get-AzureStorageAccount -StorageAccountName $san -ErrorAction SilentlyContinue)){ Write-Host "`n FAILED: Invalid Storage Account Name`n" -fore red;Exit }
#get storage context
$key=(Get-AzureStorageKey -StorageAccountName $san).primary
$cxt=New-AzureStorageContext -StorageAccountName $san -StorageAccountKey $key
If (!$cxt){ Write-Host "`n FAILED: Storage Key not obtained`n" -fore red;Exit }
#select container
Write-Host "`n Listing available Storage Account Containers:" -ForegroundColor Cyan
Get-AzureStorageContainer -Context $cxt | Select Name | FT
$container=Read-Host "`n Select Storage Container Name"
If (!(Get-AzureStorageContainer -Name $container -Context $cxt -ErrorAction SilentlyContinue)){ Write-Host "`n FAILED: Invalid Storage Container Name`n" -fore red;Exit }
#select VNET
Write-Host "`n Listing available Virtual Networks (having Gateways):" -ForegroundColor Cyan
Get-AzureVNetSite | Where GatewayProfile -ne $null | Select Name | FT
$vnet=Read-Host "`n Select Virtual Network Name"
If (!(Get-AzureVNetSite -VNetName $vnet -ErrorAction SilentlyContinue)){ Write-Host "`n FAILED: Invalid Virtual Network Name`n" -fore red;Exit }
#select duration
[int]$duration=Read-Host "`n Select Duration (seconds with 300 max)"
If (!($duration -gt 0 -and $duration -le 300)){ Write-Host "`n FAILED: Invalid Duration`n" -fore red;Exit }
#set output file
$outputfile="$Env:TEMP\AzureGatewayDiag.txt"
#start diags
Write-Host "`n Starting diagnostics for VNET $vnet`n" -ForegroundColor Cyan
$StartDiags=Start-AzureVNetGatewayDiagnostics -VNetName $vnet -CaptureDurationInSeconds $duration -ContainerName $container -StorageContext $cxt
If ($StartDiags.Status -eq "Successful") { Write-Host "`tSuccessfully started tracing" -fore green } Else { Write-Host "`tFAILED: $($StartDiags.Error)`n" -fore red;Exit }
#wait
Write-Host "`n Waiting $duration seconds" -ForegroundColor Cyan
Start-Sleep -Seconds $duration
#check status for up to 5 minutes
$State = "NotReady"
$Iterations = 1
While ($State -ne "Ready" -and $Iterations -lt 150)
{
Write-Host "`n Checking status" -ForegroundColor Cyan
$State = (Get-AzureVNetGatewayDiagnostics -VNetName $vnet).State
Write-Host "`t$State"
Start-Sleep -Seconds 2
$Iterations++
}
#get diags URL
$url = (Get-AzureVNetGatewayDiagnostics -VNetName $vnet).DiagnosticsUrl
#download output
Write-Host "`n Downloading data" -ForegroundColor Cyan
$wc = New-Object System.Net.WebClient
Try
{ $wc.DownloadFile($url, $outputFile) }
Catch [Exception]
{ Write-Host "`tFAILED: $_`n`n`tTry manually browsing: $url" -fore red;Exit }
#open output file
If (Test-Path $outputFile) { Invoke-Item $outputFile } Else { Write-Host "`tFAILED: Output file not found`n`n`tTry manually browsing: $url" -fore red }
Write-Host "`n Done`n`n" -ForegroundColor Cyan