Create VNet with 2 Subnets and place VM with 2 NICs using ARM PowerShell

In this article I will demonstrate how to create Azure Virtual Machines (VM) with 2 Network Interface Cards (NICs) using Azure Resource Manager (ARM). For this exercise we’re going to create our VM using plain PowerShell in ARM mode, and in the next article we’re going to use ARM templates featured as a showcase of how much easier it is.

The following links are recommended before starting:

Create and configure a Windows Virtual Machine with Resource Manager and Azure PowerShell

https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-ps-create-preconfigure-windows-resource-manager-vms/

Create a VM with multiple NICs (note that this is using Classic Deployment model)

https://azure.microsoft.com/en-us/documentation/articles/virtual-networks-multiple-nics/

Deploy multi NIC VMs using PowerShell

https://azure.microsoft.com/en-us/documentation/articles/virtual-network-deploy-multinic-arm-ps/

Worth noting I usually keep one VM A0 machine instance size on a subscription as a Domain Controller (DC) and it has PowerShell installed and configured. I’m going to reuse one of those machines, and as it had the previous version of Azure PowerShell (PS) installed, we will need to manually uninstall it.

The easiest way is to spin up another VM and install PS there, but for this exercise I do not want to reinstall and reconfigure existing DC.

Uninstall Previous PowerShell:

clip_image002

Install Latest PowerShell

Run Web Platform Installer from here:

https://www.microsoft.com/web/downloads/platform.aspx

clip_image004

PowerShell and ISE

Note that the ARM model no longer supports using a subscription certificate for logging in, so we will need to manually login and select a subscription each time we’re working on it. Personally I like using PowerShell ISE editor, but it’s really up to you.

Following set of commands will load necessary Azure modules and log in to your account.

Import-Module Azure

Then we can login into our Azure subscription:

Login-AzureRMAccount

clip_image006

I would disable IE Enhanced security configuration before doing that, because it interferes with the login process. The following list of commands will login into your Azure Account, then list all available subscriptions (that can be managed by that account) and then we’re going to select “Active” Azure Subscription.

clip_image008

We will need to:

1. Create Resource Group (RG) (this is very useful, because we can manage all infrastructure within the RG)

2. Create Storage Account

3. Create Virtual network with 2 subnets

4. Create Public IP Address.

5. Create 2 NICs, associate one NIC with Public IP Address.

6. Create VM

Following is the PowerShell script:

# before we start:

# import Azure related modules

# Import-Module Azure

# login into your azure account

# Login-AzureRMAccount

# select active azure subscription

# Get-AzureRmSubscription -SubscriptionName “PP BizSpark” | Select-AzureRmSubscription

$location = “East US”

$rgroup = “pp-rsMultiNicTest02”

$storage = “ppsatest10044”

$vmname = “pp-test12345”

# 1. Create Resource Group

New-AzureRmResourceGroup -Name $rgroup -Location $location

# 2. Create STorage Account

$storageAcc = New-AzureRmStorageAccount -ResourceGroupName $rgroup -AccountName $storage -Location $location -Type “Standard_LRS”

# 3. Create Virtual Network with 2 Subnets

$subnet1 = New-AzureRmVirtualNetworkSubnetConfig -Name “Subnet1” -AddressPrefix “10.0.0.0/24”

$subnet2 = New-AzureRmVirtualNetworkSubnetConfig -Name “Subnet2” -AddressPrefix “10.0.1.0/24”

$vnet = New-AzureRmVirtualNetwork -Name testvnet -ResourceGroupName $rgroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet1, $subnet2

$cred = Get-Credential -Message “Type the name and password of the local administrator account.”

$vm = New-AzureRmVMConfig -VMName $vmname -VMSize “Standard_A3”

$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName MyWindowsVM -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

# another way of picking the VM image from the gallery

# $image = (Get-AzureRmVMImage -Location $location -PublisherName “MicrosoftWindowsServer” -Offer “WindowsServer” -Skus “2012-R2-Datacenter” | Sort-Object –Descending Version)[0]

$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version “latest”

# 4. Create Public IP Address

$pip = New-AzureRmPublicIpAddress -Name TestPIP -ResourceGroupName $rgroup -Location $location -AllocationMethod Dynamic

$nic1 = New-AzureRmNetworkInterface -Name TestNIC1 -ResourceGroupName $rgroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id

$nic2 = New-AzureRmNetworkInterface -Name TestNIC2 -ResourceGroupName $rgroup -Location $location -SubnetId $vnet.Subnets[1].Id

$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic1.Id

$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic2.Id

$vm.NetworkProfile.NetworkInterfaces.Item(0).Primary = $true

$storageAcc = Get-AzureRMStorageAccount -Name $storage

$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + “vhds/WindowsVMosDisk.vhd”

$vm = Set-AzureRmVMOSDisk -VM $vm -Name “windowsvmosdisk” -VhdUri $osDiskUri -CreateOption fromImage

# 6. create the VM

New-AzureRmVM -ResourceGroupName $rgroup -Location $location -VM $vm

After running the script, we’ll see success report.

clip_image010

Let’s take a look at infrastructure on the Portal:

clip_image012

We can see that following items were created:

1. VM

2. 2 x NICs

3. Public IP Address (for simplicity I attached PIP directly to the VMs NIC)

4. VNet with 2 Subnets

5. Storage Account.

Lastly this is how I can delete the whole resource group:

Remove-AzureRmResourceGroup -ResourceGroupName $rgroup

9 Comments

  1. Kevin Remde February 11, 2016 Reply
    • Peter Perov February 15, 2016 Reply
  2. Mike Barton February 19, 2016 Reply
  3. Mukul Srivastava June 16, 2016 Reply
  4. Delle June 21, 2016 Reply
    • Delle June 22, 2016 Reply
      • Sai March 29, 2017 Reply
  5. Matt September 29, 2016 Reply

Leave a Reply