forked from JamesDLD/AzureRm-PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove-AzAppOnApg.ps1
96 lines (79 loc) · 4.24 KB
/
Remove-AzAppOnApg.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
<#
.SYNOPSIS
Remove Application Gateway publishing rules if it has been created with the naming convention used here : https://github.com/JamesDLD/AzureRm-PowerShell/blob/master/Publish-AzAppOnApg.ps1/?WT.mc_id=DOP-MVP-5003548
.NOTES
AUTHOR: James Dumont le Douarec
.EXAMPLE
./Remove-AzAppOnApg.ps1
#>
## Global variables
$AzureRmSubscriptionName = "mvp-sub1"
$RgName = "infr-hub-prd-rg1"
$AppGwName = "mvp-hub-agw1"
$Rules = @(
# @{
# ApplicationName = "myapp1apiprd" #only letters!
# };
@{
ApplicationName = "myapp1apidev" #only letters!
};
# @{
# ApplicationName = "myapp1apistg" #only letters!
# };
)
## Connectivity
# Login first with Connect-AzAccount if not using Cloud Shell
$AzureRmContext = Get-AzSubscription -SubscriptionName $AzureRmSubscriptionName | Set-AzContext -ErrorAction Stop
Select-AzSubscription -Name $AzureRmSubscriptionName -Context $AzureRmContext -Force -ErrorAction Stop | Out-Null
Foreach ($Rule in $Rules) {
$AppGw = Get-AzApplicationGateway -Name $AppGwName -ResourceGroupName $RgName
#Health Probe
$ProbeConfig = Get-AzApplicationGatewayProbeConfig -ApplicationGateway $appgw | Where-Object { $_.Name -eq "$($Rule.ApplicationName)-hpb" }
if ($ProbeConfig) {
Write-Host "Delete the Health Probe configuration" -ForegroundColor Cyan
Remove-AzApplicationGatewayProbeConfig -ApplicationGateway $AppGw -Name $ProbeConfig.Name | Out-Null
}
#Routing rule
$rule01 = Get-AzApplicationGatewayRequestRoutingRule -ApplicationGateway $appgw | Where-Object { $_.Name -eq "$($Rule.ApplicationName)-rqrt-https" }
if ($rule01) {
Write-Host "Delete Request Routing Rule $($Rule.ApplicationName)-rqrt-https" -ForegroundColor Cyan
Remove-AzApplicationGatewayRequestRoutingRule -ApplicationGateway $appgw -Name $rule01.Name | Out-Null
}
$rule02 = Get-AzApplicationGatewayRequestRoutingRule -ApplicationGateway $appgw | Where-Object { $_.Name -eq "$($Rule.ApplicationName)-rqrt-http" }
if ($rule02) {
Write-Host "Delete Request Routing Rule $($Rule.ApplicationName)-rqrt-http" -ForegroundColor Cyan
Remove-AzApplicationGatewayRequestRoutingRule -ApplicationGateway $appgw -Name $rule02.Name | Out-Null
}
$listener01 = Get-AzApplicationGatewayHttpListener -ApplicationGateway $appgw | Where-Object { $_.Name -eq "$($Rule.ApplicationName)-ln-443" }
if ($listener01) {
Write-Host "Delete the listener $($Rule.ApplicationName)-ln-443" -ForegroundColor Cyan
Remove-AzApplicationGatewayHttpListener -ApplicationGateway $appgw -Name $listener01.Name | Out-Null
}
$listener02 = Get-AzApplicationGatewayHttpListener -ApplicationGateway $appgw | Where-Object { $_.Name -eq "$($Rule.ApplicationName)-ln-80" }
if ($listener02) {
Write-Host "Delete the listener $($Rule.ApplicationName)-ln-80" -ForegroundColor Cyan
Remove-AzApplicationGatewayHttpListener -ApplicationGateway $appgw -Name $listener02.Name | Out-Null
}
#Backend Http
$poolSetting01 = Get-AzApplicationGatewayBackendHttpSetting -ApplicationGateway $appgw | Where-Object { $_.Name -eq "$($Rule.ApplicationName)-be-htst1" }
if ($poolSetting01) {
Write-Host "Delete Backend setting $($Rule.ApplicationName)-be-htst1" -ForegroundColor Cyan
Remove-AzApplicationGatewayBackendHttpSetting -ApplicationGateway $appgw -Name $poolSetting01.Name | Out-Null
}
#Backend Address Pool
$pool = Get-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw | where-object { $_.Name -like "$($Rule.ApplicationName)-pool1" }
if ($pool) {
Write-Host "Delete the Pool $($Rule.ApplicationName)-pool1"
Remove-AzApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name $pool.Name | Out-Null
}
#Redirection configuration
$redirectConfig = Get-AzApplicationGatewayRedirectConfiguration -ApplicationGateway $appgw | Where-Object { $_.Name -eq "$($Rule.ApplicationName)-rqrt-https" }
if ($redirectConfig) {
Write-Host "Delete the redirection configuration" -ForegroundColor Cyan
Remove-AzApplicationGatewayRedirectConfiguration -ApplicationGateway $appgw -Name $redirectConfig.Name | Out-Null
}
#Pushing the configuration on the Application Gateway
Write-Host "Updating the Application Gateway : $AppGwName" -ForegroundColor Cyan
Set-AzApplicationGateway -ApplicationGateway $appgw | Out-Null
#endregion
}