Create a Virtual Machine from Image

The following PowerShell script shows how to set up the virtual machine configurations and use the captured Virtual Machine image as the source for the new installation.

The following configuration items need to be updated in the script :

  • $resourceGroupName = "YourResourceGroup"
  • $storageAccountName = "YourStorageAccount";
  • $vnetworkName = "YourNetworkName";
  • $vnetworkSubnetName = "YourNetworkSubnetName";
  • $vmName = "YourVMSecondaryName" - the name of the Virtual Machine, this could be 'sec1'
  • $urlOfCapturedImageVhd = ""
  • $adminUsername = YourAdminUserName";
    • Administrator Username which can be used to RDP to the newly created VM
  • $adminPassword = "YourAdminPassword";
    • Password for the above Administrator account
  • $vmSize = "Standard_A2" (Azure Sizes)

If a machine has been created in Azure and then deleted at any point, the components that make up this machine do not get deleted. (Public IP addresses, Network Interfaces or Virtual hard disks). These must be deleted manually.

Create a Virtual Machine from Image PowerShell

Import-Module AzureRM.Compute

$vmName = "YourVMName"
$resourceGroupName = "YourResourceGroup"
$storageAccountName = "YourStorageAccount";
$vnetworkName = "YourNetworkName";
$vnetworkSubnetName = "YourNetworkSubnetName";
$urlOfCapturedImageVhd = ""
$adminUsername = "YourAdminUserName";
$adminPassword = "YourAdminPassword";
$vmSize = "Basic_A2";

$resourceGroupDetail = Get-AzureRmResourceGroup -ResourceGroupName $resourceGroupName
$location = $resourceGroupDetail.Location;
$publicIPName = $vmName + "-ip"
$networkInterfaceName = $vmName + "-nic";
$computerName = $vmName;
$osDiskName = $vmName + "-disk";
$cred = New-Object PSCredential $adminUsername, ($adminPassword | ConvertTo-SecureString -AsPlainText -Force)

$resourceGroupDetail = Get-AzureRmResourceGroup -ResourceGroupName $resourceGroupName
$location = $resourceGroupDetail.Location;
$publicIPName = $vmName + "-ip"
$networkInterfaceName = $vmName + "-nic";
$computerName = $vmName;
$osDiskName = $vmName + "-disk";
$cred = New-Object PSCredential $adminUsername, ($adminPassword | ConvertTo-SecureString -AsPlainText -Force)

Write-Host "Getting Virtual Network"
$vnet = Get-AzureRmVirtualNetwork -Name ($vnetworkName) -ResourceGroupName $resourceGroupName

Write-Host "Getting Network Subnet Network"
$subnetconfig = Get-AzureRmVirtualNetworkSubnetConfig -Name $vnetworkSubnetName -VirtualNetwork $vnet

    Write-Host "Creating Public IP Address :" $publicIPName
    $pip = New-AzureRmPublicIpAddress -Name $publicIPName -ResourceGroupName $resourceGroupName -Location $location -AllocationMethod Dynamic
    Write-Host "Created Public IP Address :" $publicIPName

    Write-Host "Creating Network Interface :" $networkInterfaceName
    $nic = New-AzureRmNetworkInterface -Name $networkInterfaceName -ResourceGroupName $resourceGroupName -Location $location -SubnetId $subnetconfig.Id -PublicIpAddressId $pip.Id
    Write-Host "Created Network Interface :" $networkInterfaceName

    #Get the storage account
    Write-Host "Getting Storage Account :" $storageAccountName
    $storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -AccountName $storageAccountName

    #Set the VM name and size
    Write-Host "Create VM Config :" $vmName "-" $vmSize
    $vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize

    #Set the Windows operating system configuration and add the NIC
    $vm = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent
    $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

    #Create the OS disk URI
    $osDiskUri = '{0}vhds/{1}{2}.vhd' -f $storageAcc.PrimaryEndpoints.Blob.ToString(), $vmName.ToLower(), $osDiskName

    #Configure the OS disk to be created from image (-CreateOption fromImage) and give the URL of the captured image VHD for the -SourceImageUri parameter.
    $vm = Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption fromImage -SourceImageUri $urlOfCapturedImageVhd -Windows

    #Create the new VM
    Write-Host "Creating VM :" $vmName "-" $location
    New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $vm
    Write-Host "Created VM :" $vmName "-" $location
  Write-Host "VM already exists :" $vmName "-" $location 

You should see the newly created Virtual Machine in either theĀ Azure portal under Browse > Virtual machines, Or by using the following PowerShell commands:

$vmList = Get-AzureRmVM -ResourceGroupName $rgName
Was this article helpful?
0 out of 1 found this helpful