This is second part of the earlier written article Create a Azure Virtual Network with Subnet using PowerShell. Here in this blog I will create subnet in already created Azure vNET (Virtual Network). I have defined Azure vNET and subnets in Powershell variables. I am creating a Azure vNET without any subnet as shown in below example.
Download this script New-AzSubnetInvNet.ps1 here or it is also available github.com/janviudapi.
$vNetName = 'global_vnet_eastus' $resourceGroupName = 'vcloud-lab.com' $location = 'eastus' $AddressPrefix = @('10.10.0.0/16') $subnet01Name = 'prod02-10.10.1.x' $subnet01AddressPrefix = '10.10.1.0/24' pwsh | 20:33:02 #Create new Azure Virtual Network without subnet New-AzVirtualNetwork -Name $vNetName -ResourceGroupName $resourceGroupName -Location $location -AddressPrefix $AddressPrefix Name : global_vnet_eastus ResourceGroupName : vcloud-lab.com Location : eastus Id : /subscriptions/9e2xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/global_vnet_eastus Etag : W/"771556f5-a9ba-4ff4-8fcc-025e2685c813" ResourceGuid : 65c12e07-56bb-4b6d-9316-fcf5d6c0604b ProvisioningState : Succeeded Tags : AddressSpace : { "AddressPrefixes": [ "10.10.0.0/16" ] } DhcpOptions : {} Subnets : [] VirtualNetworkPeerings : [] EnableDdosProtection : false DdosProtectionPlan : null
Check this article: Remove Azure Virtual Network Subnet using PowerShell
I have vNET created now without any subnet, I will get the existing vNET information and while creating/adding a new subnet use the vNET information in the parameter. There are few warning while setting subnet configuration, it can be ignored. By using subnet config command it creates subnet config in memory, Changes are not pushed into vNet yet.
#Get existing Azure Virtual Network information $azvNet = Get-AzVirtualNetwork -Name $vNetName -ResourceGroupName $resourceGroupName Add-AzVirtualNetworkSubnetConfig -Name $subnet01Name -AddressPrefix $subnet01AddressPrefix -VirtualNetwork $azvNet WARNING: Upcoming breaking changes in the cmdlet 'Add-AzVirtualNetworkSubnetConfig' : Update Property Name Cmdlet invocation changes : Old Way : -ResourceId New Way : -NatGatewayId Update Property Name Cmdlet invocation changes : Old Way : -InputObject New Way : -NatGateway Note : Go to https://aka.ms/azps-changewarnings for steps to suppress this breaking change warning, and other information on breaking changes in Azure PowerShell. Name : global_vnet_eastus ResourceGroupName : vcloud-lab.com Location : eastus Id : /subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/global_vnet_eastus Etag : W/"771556f5-a9ba-4ff4-8fcc-025e2685c813" ResourceGuid : 65c12e07-56bb-4b6d-9316-fcf5d6c0604b ProvisioningState : Succeeded Tags : AddressSpace : { "AddressPrefixes": [ "10.10.0.0/16" ] } DhcpOptions : {} Subnets : [ { "Name": "prod02-10.10.1.x", "AddressPrefix": [ "10.10.1.0/24" ], "PrivateEndpointNetworkPolicies": "Enabled", "PrivateLinkServiceNetworkPolicies": "Enabled" } ] VirtualNetworkPeerings : [] EnableDdosProtection : false DdosProtectionPlan : null
This is final command to set subnet configuration to Virtual Network.
$azvNet | Set-AzVirtualNetwork Name : global_vnet_eastus ResourceGroupName : vcloud-lab.com Location : eastus Id : /subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/global_vnet_eastus Etag : W/"e3a07d9f-a6aa-44f8-a227-0e82024bdd4b" ResourceGuid : 65c12e07-56bb-4b6d-9316-fcf5d6c0604b ProvisioningState : Succeeded Tags : AddressSpace : { "AddressPrefixes": [ "10.10.0.0/16" ] } DhcpOptions : { "DnsServers": [] } Subnets : [ { "Delegations": [], "Name": "prod02-10.10.1.x", "Etag": "W/\"e3a07d9f-a6aa-44f8-a227-0e82024bdd4b\"", "Id": "/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/global_vnet_e astus/subnets/prod02-10.10.1.x", "AddressPrefix": [ "10.10.1.0/24" ], "IpConfigurations": [], "ServiceAssociationLinks": [], "ResourceNavigationLinks": [], "ServiceEndpoints": [], "ServiceEndpointPolicies": [], "PrivateEndpoints": [], "ProvisioningState": "Succeeded", "PrivateEndpointNetworkPolicies": "Enabled", "PrivateLinkServiceNetworkPolicies": "Enabled", "IpAllocations": [] } ] VirtualNetworkPeerings : [] EnableDdosProtection : false DdosProtectionPlan : null
Useful Articles
Deploy create Virtual Network vNET in Azure cloud using Ansible
Azure PowerShell Get-AzStorageShare Could not get the storage context
Azure automation account DSC for On-Premise Virtual Machine on boarding
Azure Powershell : Operation returned an invalid status code 'BadRequest'
Create an Azure Automation account on using Portal PowerShell and AzureCLI
Gather Azure Virtual Network inventory information using PowerShell
Get Azure virtual machine backup reports using Powershell
Why is my Azure recovery services vault not getting deleted?
Microsoft Azure PowerShell cmdlet 'this.Client.SubscriptionId' cannot be null
Azure PowerShell Get-AzStorageShare Could not get the storage context