-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathARM - Load Balancer - Remove NAT Rule.ps1
168 lines (81 loc) · 3.21 KB
/
ARM - Load Balancer - Remove NAT Rule.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
## To Set Verbose output
$PSDefaultParameterValues['*:Verbose'] = $true
<# Load Balancer (LB) - Remove NAT Rule #>
<#
Network Interface (NIC)
Load Balancer (LB)
#>
# Variables - common
$rgShortName = "qweasdzxc"
$rgSuffix = "-rg"
$rgName = "${rgShortName}${rgSuffix}"
$nicShortName = "qweasdzxc"
$nicSuffix = "-nic"
$nicName = "${nicShortName}${nicSuffix}"
$lbShortName = "qweasdzxc"
$lbSuffix = "-lb"
$lbName = "${lbShortName}${lbSuffix}"
$rgName="qweasdzxc-rg"
$lbName="qweasdzxc-lb"
$frontendName = "qweasdzxcFrontEndPool"
<# NAT Rule #>
<#
Load Balancer (LB)
#>
# Variables - NAT Rule
$lbRuleName = "TestNatRule"
$natRuleFrontEndPort = 3350
$natRuleBackEndPort = 3350
Get-AzureRmLoadBalancer -Name $lbName -ResourceGroupName $rgName -ErrorVariable isLBExist -ErrorAction SilentlyContinue `
if (!$isLBExist)
{
Write-Output "Load Balancer exist"
Write-Verbose "Fetching Load Balancer (LB): {$lbName}"
$lb = Get-AzureRmLoadBalancer -Name $lbName -ResourceGroupName $rgName
#Write-Verbose "Fetching Backend Pool: {$backendPoolName}"
#$backend = Get-AzureRmLoadBalancerBackendAddressPoolConfig -Name $backendPoolName -LoadBalancer $lb
Write-Verbose "Fetching Frontend IP config: {$frontendName}"
$frontendIP = Get-AzureRmLoadBalancerFrontendIpConfig -Name $frontendName -LoadBalancer $lb
#$frontendIP = $lb.FrontendIpConfigurations[0]
Write-Verbose "Creating NAT rules: {$lbRuleName}"
Add-AzureRmLoadBalancerInboundNatRuleConfig `
-LoadBalancer $lb `
-Name $lbRuleName `
-FrontendIPConfiguration $frontendIP `
-FrontendPort $natRuleFrontEndPort `
-BackendPort $natRuleBackEndPort `
-Protocol "Tcp" `
-EnableFloatingIP
Set-AzureRmLoadBalancer -LoadBalancer $lb
}
else
{
Write-Output "Load Balancer does not exist"
}
<#
# Remove NAT rule
Write-Verbose "Fetching Load Balancer (LB): {$lbName}"
$lb = Get-AzureRmLoadBalancer -Name $lbName -ResourceGroupName $rgName
Write-Verbose "Removing NAT rule: {$lbRuleName} from Load Balancer (LB): {$lbName}"
Remove-AzureRmLoadBalancerInboundNatRuleConfig -Name $lbRuleName -LoadBalancer $lb
Set-AzureRmLoadBalancer -LoadBalancer $lb
#>
<#
##########################################
# Network Interface (NIC)
Write-Verbose "Featching Network Interface (NIC): {$nicName}"
$nic = Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName
# Add VM's Network Interface (NIC) to the load balancer
Write-Verbose "Featching Load Balancer (LB): {$lbName}"
$lb = Get-AzureRMLoadBalancer -Name $lbName -ResourceGroupName $rgName
Write-Verbose "Attaching/Adding VM's Network Interface (NIC) {$nicName} to the load balancer: {$lbName}"
#$nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $lb.BackendAddressPools[0]
#Set-AzureRmNetworkInterface -NetworkInterface $nic
$nic.LoadBalancerInboundNatRule = $lb.InboundNatRules[0]
Set-AzureRmNetworkInterface -NetworkInterface $nic
#>
<#
# References
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/tutorial-load-balancer
#>