Users: 30k; Pages Per Session: 2.5p; Avg. Session Duration: 1:35mi
TechDevOps.com
Explore Tools
{Featuring Microsoft Technologies + Enterprise Data + Cloud Computing}



Azure Powershell Create a new Virtual Machine with 1 VNET and 2 SUBNETS
by BF (Senior MSSQL Operations - Enterprise, Cloud, Strategy)
2016-09-17








This Powershell example is complete code required for creating a new Windows-based Virtual Machine in the Microsoft Azure Cloud Platform.
You can use this to quickly deploy Virtual Machines to your Microsoft Azure Cloud Environment(s) Subscription(s). It has components of
Storage, Networking and Compute configured. It will provision 2 Subnets solely for the purpose of a future Windows Failover Clustering setup.
1 Subnet is will be used for Data and 1 Subnet will be used for Clustering Heartbeat.




Powershell Code:


Authenticate to Azure Portal:

Login-AzureRmAccount


Select your Azure Subscription:

Select-AzureRmSubscription -SubscriptionName "TST"


Define Variables

$Location = "East US 2"

$ResourceGroupName = "RGSQLTST"
$ResourceGroupNameVNET = "TSTDC"
$ResourceGroupStorage = "RGSQLTST"

$StorageName = "azrstrg12345"
$StorageType = "Standard_GRS"

$Interface1Name = "node1000"
$Interface2Name = "node1000ha"
$VNetName = "TSTDC"
$Subnet1Name = "TSTSubnet"
$Subnet2Name = "TSTHASubnet"
$VNetAddressPrefix = "x.x.x.x/16"
$VNetSubnetAddressPrefix = "x.x.x.x/16"
$VMName = "NODE1000"

$ComputerName = "NODE1000"
$VMSize = "Standard_DS4_V2"
$RandomNum = Get-Random
$OSDiskName = $VMName + "_" + $RandomNum + "_VMOSDISK"


Storage:

$StorageAccount = Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroupStorage -Name $StorageName


Networking (1 VNET, 2 SUBNETS, 2 NIC's)

$VNet = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupNameVNET

$VNetSubnet1 = $VNet.Subnets[1] #Subnet 1
$VNetSubnet1ID = $VNetSubnet1.Id
$VNetSubnet2 = $VNet.Subnets[2] #Subnet 2
$VNetSubnet2ID = $VNetSubnet2.Id

$Subnet1Config = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $VNet -Name $Subnet1Name
$Interface1 = New-AzureRmNetworkInterface -Name $Interface1Name -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VNetSubnet1ID

$Subnet2Config = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $VNet -Name $Subnet2Name
$Interface2 = New-AzureRmNetworkInterface -Name $Interface2Name -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VNetSubnet2ID


Build Local VM Object

$Credential = Get-Credential

$VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize

$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $ComputerName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate

$VirtualMachine = Set-AzureRmVMSourceImage -VM $VirtualMachine -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"

$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $Interface1.Id

$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $Interface2.Id

#Set Interface1 as Primary:
$VirtualMachine.NetworkProfile.NetworkInterfaces.Item(0).Primary = $true

$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"

$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption FromImage


Create VM

New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine



Resources:

Microsoft Azure