Project Work Hours
Sat & Sun 6am - 9am
Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Create and Deploy from Image -Azure

This article cover creating an Azure virtual machine image from an existing Azure virtual machine (VM) and deploying that image using PowerShell.  As of time of this article, imaging and deploying from image in the resource model can only be done using PowerShell. Video and script used in video below.

dv1-series-azure-02-create-and-deploy-from-image

Video:

 

Steps in the above video covers;

  • Sysprep Operating System
  • Generalize Virtual Machine
  • Save as Azure image
  • Deploy VM using image and copying specs from existing virtual machine

 

Scripts:

Create-V2VMImage.ps1

 Param(
 [Parameter(Mandatory=$true)]
 $ResourceGroupName,
 [Parameter(Mandatory=$true)]
 $SubscriptionId,
 [Parameter(Mandatory=$true)]
 $storageAccName,
 [Parameter(Mandatory=$true)]
 $VMName,
 [Parameter(Mandatory=$true)]
 $VHDNamePrefix
 )


#Login to Azure and select subscription
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId $SubscriptionId

#Stop VM 
Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName -Force

#Generalize VM and confirm status
Set-AzureRmVm -ResourceGroupName $ResourceGroupName -Name $VMName -Generalized
$vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName -status
$vm.Statuses

#Create Azure VM Image 
Save-AzureRmVMImage -ResourceGroupName $ResourceGroupName -VMName $VMName -DestinationContainerName $ResourceGroupName.ToLower() -VHDNamePrefix $VHDNamePrefix

#Getting Image VHD URI
$SA = Get-AzureRmStorageAccount -Name $storageAccName -ResourceGroupName $ResourceGroupName
$image = ($SA | Get-AzureStorageBlob -Container "system").Name | ?{$_ -like "*$VHDNamePrefix-osDisk*.vhd"}
$imageURI = "https://$storageAccName.blob.core.windows.net/system/$Image"

$imageURI 

#https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-capture-image/

 

Create-V2VMFromImageAuto.ps1

 Param(
 [Parameter(Mandatory=$true)]
 $ResourceGroupName,
 [Parameter(Mandatory=$true)]
 $SubscriptionId,
 [Parameter(Mandatory=$true)]
 $OldVMName,
 #[Parameter(Mandatory=$true)]
 #$storageAccName,
 #[Parameter(Mandatory=$true)]
 #$NewvmNames,
 #[Parameter(Mandatory=$true)]
 #$imageURI,
 [Parameter(Mandatory=$true)]
 $VHDNamePrefix
 )


#Creating name and amount of VM needed
$sprefix = "DV1-DPBDC1-00" #New VM name prefix 
$a = 1 #The array of server needed e.g. 2..6
$Newvmnames = $a | % {$sprefix +$_}


#Login to Azure and select subscription
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId $SubscriptionId

# Enter a new user name and password to use as the local administrator account for the remotely accessing the VM
$cred = Get-Credential -Message "Username and Password for you new Virtual Machine"

#Get old vm setting to use as variables
$EVM = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $OldVMName

# Name of the storage account 
$storageAccName = $EVM.DiagnosticsProfile.BootDiagnostics.StorageUri -replace "https://","" -replace ".blob.core.windows.net/",""

#Get the storage account where the uploaded image is stored
$storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $EVM.ResourceGroupName -AccountName $storageAccName


Foreach ($NewvmName in $Newvmnames)
{
#Set the VM name and size
#Use "Get-Help New-AzureRmVMConfig" to know the available options for -VMsize
$vmConfig = New-AzureRmVMConfig -VMName $NewvmName -VMSize $EVM.HardwareProfile.VmSize

#Set the Windows operating system configuration and add the NIC
$vm = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $NewvmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

#Creating Public IP
$rnum = Get-Random -Minimum 100 -Maximum 999
$ipName = $NewvmName.ToLower() + $rnum
$pip = New-AzureRmPublicIpAddress -Name $ipName -Location $EVM.Location -ResourceGroupName $EVM.ResourceGroupName -AllocationMethod Dynamic -DomainNameLabel $NewvmName.ToLower()

#Creating NIC on first VNet and Subnet 
$vnet = (Get-AzureRmVirtualNetwork -ResourceGroupName $EVM.ResourceGroupName)[0]
$rnum = Get-Random -Minimum 100 -Maximum 999
$nicName = $NewvmName.ToLower() + $rnum
$nsg = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Name WindowsServer-Default
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $EVM.ResourceGroupName -Location $EVM.Location -PublicIpAddress $pip -Subnet $vnet.Subnets[0] -NetworkSecurityGroup $nsg

#Adding NIC to VM variable 
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

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

#Getting Image from storage account using image VHDNamePrefix
$image = ($storageAcc | Get-AzureStorageBlob -Container "system").Name | ?{$_ -like "*$VHDNamePrefix-osDisk*.vhd"}
$imageURI = "https://$storageAccName.blob.core.windows.net/system/$Image"

#Configure the OS disk to be created from the image (-CreateOption fromImage), and give the URL of the uploaded image VHD for the -SourceImageUri parameter
#You set this variable when you uploaded the VHD
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $NewvmName -VhdUri $osDiskUri -CreateOption fromImage -SourceImageUri $imageURI -Windows

#Create the new VM
New-AzureRmVM -ResourceGroupName $EVM.ResourceGroupName -Location $EVM.Location -VM $vm

#https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-capture-image/
}

 

Microsoft Azure article covering some of the steps in video

https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-capture-image/

DV1 Series

http://hazelnest.com/blog/blog/tag/dv1-series/