-
Notifications
You must be signed in to change notification settings - Fork 7
/
ARM - Virtual Network (VNet) - Add Subnet.ps1
88 lines (46 loc) · 2.08 KB
/
ARM - Virtual Network (VNet) - Add Subnet.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
<# Add Subnet - to Virtual Network (VNet) #>
<#
Virtual Network (VNet)
#>
# Variables - Subnet
$subnetShortName = "Test"
$subnetSuffix = "Subnet"
$subnetName = "${subnetShortName}${subnetSuffix}"
$addressPrefix = "10.0.6.0/24"
<# Create Subnet (in VNet), if it does not exist #>
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet -ErrorVariable isSubnetExist -ErrorAction SilentlyContinue `
If ($isSubnetExist)
{
Write-Output "Subnet does not exist"
Write-Verbose "Fetching Virtual Network: {$vnetName}"
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
# add a subnet to the in-memory representation of the virtual network.
Write-Verbose "Creating Subnet: {$subnetName}"
Add-AzureRmVirtualNetworkSubnetConfig `
-Name $subnetName `
-VirtualNetwork $vnet `
-AddressPrefix $addressPrefix
# updates the existing virtual network with the new subnet.
$vnet | Set-AzureRmVirtualNetwork
}
Else
{
Write-Output "Subnet exist"
Write-Verbose "Fetching Virtual Network: {$vnetName}"
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
Write-Verbose "Fetching Subnet: {$subnetName}"
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
}
Write-Verbose "Get list of all subnets"
Write-Output "Subnets"
Write-Verbose "Fetching Virtual Network: {$vnetName}"
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet `
| Select-Object Name, AddressPrefix `
| Format-Table -AutoSize -Wrap
<#
## References
https://docs.microsoft.com/en-us/powershell/module/azurerm.network/add-azurermvirtualnetworksubnetconfig?view=azurermps-6.13.0
https://docs.microsoft.com/en-us/powershell/module/azurerm.network/get-azurermvirtualnetworksubnetconfig?view=azurermps-6.13.0
#>