Creating Azure Virtual Network with ARM PowerShell cmdlets

Azure Resource Manager

The AzureResourceManager module, introduced in Azure PowerShell version 0.8.0, lets you manage your resources in an entirely new way. Instead of creating individual resources and trying to use them together, begin by imagining the service you want to create, such as a web portal, a blog, a photo gallery, a commerce site, or a wiki.

Select a resource group template for the service, including one of dozens in the Azure template gallery, or create your own. Each template provides a model of a complex service, complete with the resources that you need to support the service. Then use the template to create a resource group and its resources, and deploy and manage the related resources as a unit.

 

When you use the Azure PowerShell cmdlets, the Azure module is imported into the session by default. To remove the Azure module from the session and import the AzureResourceManager and AzureProfile modules, use the Switch-AzureMode cmdlet.

Switch-AzureMode -Name AzureResourceManager

And, to switch back to the Azure module, just use Switch-AzureMode again

Switch-AzureMode -Name AzureServiceManagement

 

Below Script will help in creating a Resource Group, Virtual Network based on Azure Resource Manager and eventually associate a Subnet to it.

Note:

New-AzureResourceGroup = This command let is used for creation of a new Resource Group.

New-AzureVirtualNetwork =  This command let is used for creation of a Virtual Network.

Add-AzureVirtualNetworkSubnetConfig = This command let is used for adding a Subnet with conjunction with Set-AzureVirtualNetwork to update the Virtual Network’s Configuration.

Switch-AzureMode -Name AzureResourceManager

$VerbosePreference = "continue"

Write-verbose "Setting Environment Variables"

$ResourceGroup = "sushraolab"

$Location = "East US"

$VNETName = "sushraolab_VNET"

$AddressPrefix = "10.0.0.0/16"

$SubnetName = "Subnet-1"

$SubnetPrefix = "10.0.0.0/24"

 

Write-Verbose "Creating a New Azure Resource Group $ResourceGroup"

New-AzureResourceGroup -Name $resourcegroup -Location $Location

Write-Verbose "Creating a ARM based Virtual Network $VNETNAME within the Resource Group $ResourceGroup"

$VNET = New-AzureVirtualNetwork -ResourceGroupName $ResourceGroup -Location $Location -Name $VNETName -AddressPrefix $AddressPrefix

Write-Verbose "Adding a $SubnetName to the ARM based Virtual Network $VNETNAME"

$VNET | Add-AzureVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix $subnetPrefix | Set-AzureVirtualNetwork

Write-verbose "Output of the Subnet's Associated with the ARM based Virtual Network $VNETName"

$VNET1 = Get-AzureVirtualNetwork -ResourceGroupName $ResourceGroup -Name $VNETName

$Subnet = Get-AzureVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $VNET1

$subnet

Write-Verbose "Successfully Executed the Script"

Note: You can copy the above Script to notepad and after updating the Environment Variables save it as AzureVnetCreation.ps1 and execute it via Azure PowerShell

 

Below Script will help in adding a Subnet to an existing Virtual Network based on Azure Resource Manager 

$VerbosePreference = "continue"

Write-verbose "Setting Environment Variables"

$ResourceGroup = "sushraolab"

$VNETName = "sushraolab_VNET"

$SubnetName = "Subnet-2"

$SubnetPrefix = "10.0.1.0/24"

Write-verbose "Adding a New Subnet to an Existing ARM based Virtual Network"

$vnet   = Get-AzureVirtualNetwork -ResourceGroupName $ResourceGroup -Name $VNETName

$vnet | Add-AzureVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetPrefix | Set-AzureVirtualNetwork

Write-verbose "Output of the Subnet's Associated with the ARM based Virtual Network $VNETName"

$VNET1 = Get-AzureVirtualNetwork -ResourceGroupName $ResourceGroup -Name $VNETName

$VNET1

Write-Verbose "Successfully Executed the Script"

Note: You can copy the above Script to notepad and after updating the Environment Variables save it as AzureSubnetCreation.ps1 and execute it via Azure PowerShell

Comments

  • Anonymous
    October 05, 2015
    Brilliant thanks !