Author Posts

November 3, 2017 at 11:01 pm

I'm referring a below link, using this link i have downloaded the .ps1 file. This .ps1 file explains how to take Azure RM VM's Snapshot.

I want to take snapshot for Single Azure ARM VM. what modification i have to do in this script?

https://gallery.technet.microsoft.com/Azure-ARM-VM-Snapshot-3292a891

Login-AzureRmAccount -Credential $psCred –SubscriptionId $SubscriptionId -ErrorAction Stop | out-null
Get-AzureRmSubscription -SubscriptionId $SubscriptionId | Select-AzureRmSubscription

$tagResList = Find-AzureRmResource -TagName Environment -TagValue Production

#$tagRsList[0].ResourceId.Split("//")
#subscriptions
#
#resourceGroups
#
#providers
#Microsoft.Compute
#virtualMachines
#

foreach($tagRes in $tagResList) {
if($tagRes.ResourceId -like "Microsoft.Compute")
{
$vmInfo = Get-AzureRmVM -ResourceGroupName $tagRes.ResourceId.Split("//")[4] -Name $tagRes.ResourceId.Split("//")[8]

#Condition with no data disks only data disk
$strAccount = ($vmInfo.StorageProfile.OsDisk.Vhd.Uri).Split('//')[2].Split('/.')[0]
#Finding the OS Disk resource Group
$storageDetails = Find-AzureRmResource -ResourceNameContains $strAccount

$AzStrAct = Get-AzureRmStorageAccount -Name $strAccount -ResourceGroupName $storageDetails.ResourceGroupName
$AzStrKey = Get-AzureRmStorageAccountKey -Name $strAccount -ResourceGroupName $storageDetails.ResourceGroupName
$AzStrCtx = New-AzureStorageContext $strAccount -StorageAccountKey $AzStrKey[0].Value
$Container = ($vmInfo.StorageProfile.OsDisk.Vhd.Uri).Split('//')[3]
$VHDName = ($vmInfo.StorageProfile.OsDisk.Vhd.Uri).Split('//')[4]
$VHDNameShort = ($vmInfo.StorageProfile.OsDisk.Vhd.Uri).Split('//')[4].Split('/.')[0]
#$VMName = $vmInfo.Name
#Finds the OS Disk with VHD Name and Container
$VMblob = Get-AzureRmStorageAccount -Name $strAccount -ResourceGroupName $storageDetails.ResourceGroupName | Get-AzureStorageContainer | where {$_.Name -eq $Container} | Get-AzureStorageBlob | where {$_.Name -eq $VHDName -and $_.ICloudBlob.IsSnapshot -ne $true}
#Blob Snapshot
$VMsnap = $VMblob.ICloudBlob.CreateSnapshot()

$blob = Get-AzureStorageContainer -Context $AzStrCtx -Name $Container
$ListOfBlobs = $blob.CloudBlobContainer.ListBlobs($VHDName, $true, "Snapshots")

if($vmInfo.DataDiskNames.Count -ge 1){
#Condition with more than one data disks
for($i=1; $i -le $vmInfo.DataDiskNames.Count; $i++){
$StrdataDisk = ($vmInfo.StorageProfile.DataDisks[$i].Vhd.Uri).Split('//')[2].Split('/.')[0]
$storageDetailsdd = Find-AzureRmResource -ResourceNameContains $StrdataDisk

$AzStrActdd = Get-AzureRmStorageAccount -Name $StrdataDisk -ResourceGroupName $storageDetailsdd.ResourceGroupName
$AzStrKeydd = Get-AzureRmStorageAccountKey -Name $StrdataDisk -ResourceGroupName $storageDetailsdd.ResourceGroupName
$AzStrCtxdd = New-AzureStorageContext $StrdataDisk -StorageAccountKey $AzStrKeydd[0].Value
$ddContainer = ($vmInfo.StorageProfile.DataDisks[$i].Vhd.Uri).Split('//')[3]
$ddVHDName = ($vmInfo.StorageProfile.DataDisks[$i].Vhd.Uri).Split('//')[4]
$ddVHDNameShort = ($vmInfo.StorageProfile.DataDisks[$i].Vhd.Uri).Split('//')[4].Split('/.')[0]
#$VMName = $vmInfo.Name
#Finds the OS Disk with VHD Name and Container
$ddVMblob = Get-AzureRmStorageAccount -Name $StrdataDisk -ResourceGroupName $storageDetailsdd.ResourceGroupName | Get-AzureStorageContainer | where {$_.Name -eq $ddContainer} | Get-AzureStorageBlob | where {$_.Name -eq $ddVHDName -and $_.ICloudBlob.IsSnapshot -ne $true}
#Blob Snapshot
$ddVMsnap = $ddVMblob.ICloudBlob.CreateSnapshot()

$ddblob = Get-AzureStorageContainer -Context $AzStrCtxdd -Name $ddContainer
$ddListOfBlobs = $ddblob.CloudBlobContainer.ListBlobs($ddVHDName, $true, "Snapshots")

}
}
else{
Write-Host $vmInfo.Name + " doesn't have any additional data disk."
}
}
else{
$tagRes.ResourceId + "is not a compute instance"
}
}

#$tagRgList = Find-AzureRmResourceGroup -Tag @{ Environment = "Production" }

November 4, 2017 at 8:45 am

Hi Sunilkatke
This will enable you to snapshot a single AzureRM Virtual Machine using unmanaged disks.

$RSG = Get-AzureRmResourceGroup | Out-GridView -PassThru -Title "Select AzureRM ResourceGroup"
$SnapShotName = Read-Host "Please enter name of the snapshot"
$StorageAccount = Get-AzureRmStorageAccount -ResourceGroupName $RSG.ResourceGroupName | Out-GridView -PassThru -Title "Select the storage account you want to use?"
$Location = "UKSouth" # Enter your location
$VM = Get-AzureRMVM -ResourceGroupName $RSG.ResourceGroupName | Out-GridView -PassThru -Title "Select VM to SnapShot"
$BlobPath = "vhds/NameOfVHD.vhd" # Enter VHD name
$OsDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + $BlobPath


$SSConfig = New-AzureRmSnapshotConfig -AccountType StandardLRS `
                                      -Location $Location `
                                      -CreateOption Import `
                                      -StorageAccountId $StorageAccount.ID `
                                      -SourceUri $OsDiskUri


New-AzureRmSnapshot -Snapshot $SSConfig `
                                      -ResourceGroupName $RSG.ResourceGroupName `
                                      -SnapshotName $SnapShotName

Any questions, let me know

Cheers

Tommy