-
Notifications
You must be signed in to change notification settings - Fork 7
/
ARM - Network Interface (NIC) - Delete.ps1
276 lines (164 loc) · 6.61 KB
/
ARM - Network Interface (NIC) - Delete.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
## To Set Verbose output
$PSDefaultParameterValues['*:Verbose'] = $true
# Variables - Common
$location = "eastus2"
$tags = New-Object 'System.Collections.Generic.Dictionary[String,object]'
$tags.Add("environment", "Production") # Production, Staging, QA
$tags.Add("projectName", "Demo Project")
$tags.Add("projectVersion", "1.0.0")
$tags.Add("managedBy", "developer.aashishpatel@gmail.com")
$tags.Add("billTo", "Ashish Patel")
$tags.Add("tier", "Front End") # Front End, Back End, Data
$tags.Add("dataProfile", "Public") # Public, Confidential, Restricted, Internal
<# Resource Group #>
# Variables - Resource Group
$rgShortName = "qweasdzxc"
$rgSuffix = "-rg"
$rgName = "${rgShortName}${rgSuffix}"
<# Virtual Network (VNet) #>
<#
#>
# Variables - Virtual Network
$vnetShortName = "qweasdzxc"
$vnetSuffix = "-vnet"
$vnetName = "${vnetShortName}${vnetSuffix}"
# subnets
$webSubnetName = "WebSubnet"
$frontendSubnetName = "FrontEndSubnet"
$backendSubnetName = "BackEndSubnet"
$gatewaySubnetName = "GatewaySubnet"
# Variables - Public IP
$publicIpShortName = "qweasdzxc3"
$publicIpSuffix = "-ip"
$publicIpName = "${publicIpShortName}${publicIpSuffix}"
$dnsPrefix = "qweasdzxc"
#$dnsPrefix = "qweasdzxc$(Get-Random)"
# Variables - Network Security Group
$nsgShortName = "qweasdzxc"
$nsgSuffix = "-nsg"
$nsgName = "${nsgShortName}${nsgSuffix}"
$subnetName = "BackEndSubnet"
<# Network Interface (NIC) #>
<#
Virtual Network (VNet)
Subnet
Network Security Group (NSG) (optional)
Public IP address (optional)
#>
# Variables - Network Interface (NIC)
$nicShortName = "qweasdzxc2"
$nicSuffix = "-nic"
$nicName = "${nicShortName}${nicSuffix}"
# NIC has public IP or not
$hasPublicIp = $true
# Static: Private Ip Address (within the subnet ip range)
#$privateIpAddress = "10.0.2.77"
# Dynamic: Private Ip Address
$privateIpAddress = $null
<# Create Network Interface (NIC), if it does not exist #>
Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -ErrorVariable isNICExist -ErrorAction SilentlyContinue `
if ($isNICExist)
{
Write-Output "Network Interface (NIC) does not exist"
# Virtual Network (VNet)
Write-Verbose "Fetching Virtual Network (VNet): {$vnetName}"
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
# Subnet
Write-Verbose "Fetching Subnet: {$subnetName}"
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
# Network Security Group (NSG)
Write-Verbose "Fetching Network Security Group (NSG): {$nsgName}"
$nsg = Get-AzureRmNetworkSecurityGroup -Name $nsgName -ResourceGroupName $rgName
$IPconfigName = "ipconfig1"
if($hasPublicIp)
{
# Public IP address
Write-Verbose "Fetching Public IP: {$publicIpName}"
$publicIp = Get-AzureRmPublicIpAddress -Name $publicIpName -ResourceGroupName $rgName
# Create a IP config and associate with public IP address
Write-Verbose "Creating IP config (with public IP address): {$IPconfigName}, Subnet: {$subnetName}, Public Ip: {$publicIpName}"
}
else
{
$publicIp = $null
# Create a IP config without public IP address
Write-Verbose "Creating IP config (without public IP address): {$IPconfigName}, Subnet: {$subnetName}"
}
# IP config
$IPconfig = New-AzureRmNetworkInterfaceIpConfig `
-Name $IPconfigName `
-Subnet $subnet `
-PrivateIpAddress $privateIpAddress `
-PrivateIpAddressVersion IPv4 `
-PublicIpAddress $publicIp `
# Create a virtual network card
Write-Verbose "Creating Network Interface (NIC): {$nicName}, Subnet: {$subnetName}, NSG: {$nsgName}"
$nic = New-AzureRmNetworkInterface `
-Name $nicName `
-ResourceGroupName $rgName `
-Location $location `
-NetworkSecurityGroup $nsg `
-IpConfiguration $IPconfig `
-Tag $tags
<#
# Default options without IP Config
if($hasPublicIp)
{
# Public IP address
Write-Verbose "Fetching Public IP: {$publicIpName}"
$publicIp = Get-AzureRmPublicIpAddress -Name $publicIpName -ResourceGroupName $rgName
# Create a virtual network card and associate with public IP address and NSG
Write-Verbose "Creating Network Interface (NIC): {$nicName}, Subnet: {$subnetName}, NSG: {$nsgName}, Public Ip: {$publicIpName}"
}
else
{
$publicIp = $null
# Create a virtual network card without public IP address and NSG
Write-Verbose "Creating Network Interface (NIC): {$nicName}, Subnet: {$subnetName}, NSG: {$nsgName}"
}
$nic = New-AzureRmNetworkInterface `
-Name $nicName `
-ResourceGroupName $rgName `
-Location $location `
-Subnet $subnet `
-NetworkSecurityGroup $nsg `
-PublicIpAddress $publicIp `
-Tag $tags
#>
}
else
{
Write-Output "Network Interface (NIC) exist"
Write-Verbose "Fetching Network Interface (NIC): {$rgName}"
$nic = Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName
}
Write-Verbose "Get list of Network Interface (NIC)s"
Write-Output "Network Interface (NIC)s"
Get-AzureRmNetworkInterface -ResourceGroupName $rgName `
| Select-Object Name, ResourceGroupName, Location `
| Format-Table -AutoSize -Wrap
Get-AzureRmNetworkInterface -ResourceGroupName $rgName `
| Select-Object -ExpandProperty IpConfigurations `
| Select-Object Name, PrivateIpAddress `
| Format-Table -AutoSize -Wrap
<#
Get-AzureRmNetworkInterface `
| Select-Object Name, ResourceGroupName, Location `
| Format-Table -AutoSize -Wrap -GroupBy ResourceGroupName
#>
<#
## Remove Network Interface (NIC)
$nicShortName = "qweasdzxc2"
$nicSuffix = "-nic"
$nicName = "${nicShortName}${nicSuffix}"
Write-Verbose "Delete Network Interface (NIC): {$nicName}"
Remove-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Force
#>
<#
## References
https://docs.microsoft.com/en-us/powershell/module/azurerm.network/new-azurermnetworkinterface?view=azurermps-6.13.0
edit: change ip, subnet, dns etc
https://docs.microsoft.com/en-us/powershell/module/azurerm.network/set-azurermnetworkinterface?view=azurermps-6.13.0
in config, more than one
https://github.com/anthonychu/azure-content/blob/master/articles/virtual-network/virtual-network-multiple-ip-addresses-powershell.md
#>