The complete Script: Create a VM in Azure

Hi Everyone and Happy Friday.

Before starting a new topic next week, you can find below a complete script to create VM’s in Azure Resource Manager:

 

# Authenticate to Azure Account

Login-AzureRmAccount

# Select an Azure subscription

 

$subscriptionId =

    (Get-AzureRmSubscription |

     Out-GridView `

        -Title “Select an Azure Subscription …” `

        -PassThru).SubscriptionId

 

Select-AzureRmSubscription `

    -SubscriptionId $subscriptionId

# Select existing Resource Group in which VM will be installed

$location =

    (Get-AzureRmLocation |

    Out-GridView `

        -Title “Select the Location …..” `

        -PassThru).DisplayName

# Select existing Resource Group in which VM will be installed

$ResourceGroupName =

    (Get-AzureRmResourceGroup |

    Out-GridView `

        -Title “Select Resource Group ……….” `

        -PassThru).ResourceGroupName
## Set network configuration

 

$vnetName =

    (Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroupName).Name |

    Out-GridView `

    -Title “Select a Virtual Network” `

    -PassThru

 

$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $vnetName

 

$subnetNameSelect =

    (Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet).name |

    Out-GridView `

    -Title “Select a subnet” `

    -PassThru

$SubnetName = $subnetNameSelect.ToString()

 

# Select VM Size

$VMSize =

    (Get-AzureRmVMSize -location $location |

    Out-GridView `

        -Title “Select VM Size…….” `

        -PassThru).name

 

$Skus =

    (Get-AzureRmVMImageSku -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Location $location |

    Out-GridView `

        -Title “Select O/S …..” `

        -PassThru).Skus
#Load assembly for user input

 

[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null

 

$VMName = [Microsoft.VisualBasic.Interaction]::InputBox(“Pleae Enter VM Name”,”VM Name”, “”)

 

$nicname = $VMName + “-NIC”

$Subnet = Get-AzureRMVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $VNet

$Interface = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $ResourceGroupName -Location $location -Subnet $Subnet 

$ComputerName = $VMName

$timeZone = “AUS Eastern Standard Time”

$OSDiskName = $VMName + “-OSdisk”

$InterfaceName = $VMName + “-NIC”

$Credential = Get-Credential

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

$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Window -ComputerName $ComputerName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate -TimeZone $timeZone

$VirtualMachine = Set-AzureRmVMSourceImage -VM $VirtualMachine -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus $Skus -Version “latest”

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

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

 

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

 

The topic for the following few weeks will be migrating existing workload to Azure IaaS using backup and site recovery.

 

Have a good weekend,

Ofir.