Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Create a Azure Virtual Network with Subnet using PowerShell

Azure VNet (Virtual Network) is the essential element for your private network in the Azure Cloud. Virtual Network allows various kinds of Azure resources, such as Azure Virtual Machines (VM), to securely connect with each other, the internet, and on-premises networks. In this article I will be creating Azure Virtual Network using PowerShell. The very first command is to connect azure cloud. (Make sure you have installed Azure Powershell module using command Install-Module Az -Scope CurrentUser).

Connect-AzAccount

Microsoft azure az powershell connect-azaccount subscription free tenant id virtual network vnet azure multi-factor authentication parameter azurecloud.png

Once you are successfully connected to Azure cloud, define vNET and subnet, address suffixes information in the PowerShell variables. These variables will be used in next PowerShell Az commands.

$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'

Download script New-AzvNetWithSubnet.ps1 or it is also available on github.com/janviudapi

Microsoft Azure vnet virtual network resource group location address prefix subnet powershell az module connect-azaccount free subscription automation.png

Firstly create virtual network subnet configuration, providing subnet name and address prefix (IP address block within the virtual network). This will not do anything for now, store the config information in a variable, so it can be used while create vNET. By using subnet config command it creates subnet config in memory, Changes are not pushed into vNet yet.

#Create new Azure Virtual Network Subnet configuration
$subnet01 = New-AzVirtualNetworkSubnetConfig -Name $subnet01Name -AddressPrefix $subnet01AddressPrefix
WARNING: Upcoming breaking changes in the cmdlet 'New-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.

Microsoft azure cloud powershell az module new-azvirtualnetworksubnetconfig addressprefix resourceid natgatewayid inputobject azps virtual network vnet.png

This is a the actual command to create vNET with subnets. Feed all the information in parameters (variables information used earlier). Configuration of vNET information can be viewed on PowerShell console

Check Out: Azure add create a Subnet to existing Virtual Network using PowerShell 
Check Out: Remove Azure Virtual Network Subnet using PowerShell

#Create new Azure Virtual Network with above subnet configuration
New-AzVirtualNetwork -Name $vNetName -ResourceGroupName $resourceGroupName -Location $location -AddressPrefix $AddressPrefix -Subnet $subnet01

Name                   : global_vnet_eastus
Etag                   : W/"c097f527-3102-4edb-8952-7c04ba439d24"
ResourceGuid           : 9e8eb181-140a-4b8b-ba6d-4b5e1710715c
ProvisioningState      : Succeeded
Tags                   :
AddressSpace           : {
                           "AddressPrefixes": [
                             "10.10.0.0/16"
                           ]
                         }
DhcpOptions            : {}
Subnets                : [
                           {
                             "Delegations": [],
                             "Name": "prod02-10.10.1.x",
                             "Etag": "W/\"c097f527-3102-4edb-8952-7c04ba439d24\"",
                             "Id": "/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/global_vnet_eastus/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

Microsoft powershell azure new-azvirtualnetwork resourcegroup location addressprefix subnet dhcp peering ddos az powershell.png

In the last below command deletes/removes Azure Virtual Network. Do your due diligence before deleting Azure Resources. 

Remove-AzVirtualNetwork -Name <vNET Name> -ResourceGroupName <Resource Group Name>

Azure powershell remove-azvirtualnetwork vnet virtual network resource group az powershell global resources.png

Useful Articles
Deploy create Virtual Network vNET in Azure cloud using Ansible
Azure add create a Subnet to existing Virtual Network using PowerShell
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

Go Back

Comment

Blog Search

Page Views

11240960

Follow me on Blogarama