New Delhi , Moti Nagar
+91-9007641046
subhendu@subhendumct.com

Powershell Script to create Snapshot from VM and move to storage account

Live in Future - Live in Cloud

Powershell Script to create Snapshot from VM and move to storage account

I believe you have enjoyed my last script related to VM snapshot creation . Sometime we work futher then that . it may be that you have to move those snapshot to a specific storage account . This script  will help you to do that . Again I have tried my level best to make a super simple script that make your life easier . This script can work with multiple VM also . Before run this script you need collect few information . 

Information  that you need to run this script :

  1. List of VM name .
  2. Storage account name .
  3. Container name where you want to move snapshot’s .

Have a look of entire script and update this details . Then run the script .

#Please type you all VM names over here .
$VMlists = "VM1","VM02","VM03","VM04"
clear
Write-Host "Welcome @ Subhendu's Automation System ." -ForegroundColor Cyan 
Write-Host "Hey $env:UserName Greetings !! Sit Back and relax . " -ForegroundColor Cyan 
Start-Sleep -s 1
$i = 1

ForEach($VMlist in $VMlists)
{
Write-Host "I have started my work with $VMlist VM . And this is No:$i VM in List ." -ForegroundColor Cyan 
Start-Sleep -s 1
$VMName = $VMlist
$VMRGname = (Get-AzureRmResource -Name $VMName -ResourceType "Microsoft.Compute/virtualMachines").ResourceGroupName
$OSDisks = (Get-AzureRMVM –Name $VMName –ResourceGroupName $VMRGname).StorageProfile.OsDisk.Name
$DataDisks = (Get-AzureRMVM –Name $VMName –ResourceGroupName $VMRGname).StorageProfile.DataDisks.name
$i = 1
$king
ForEach($OSDisk in $OSDisks)
{
Write-Host "OS-Disk No $i : $OSDisk"
Write-Host "Now I am creating snapshot of Disk $i " -ForegroundColor White
$Disk = Get-AzureRmDisk -ResourceGroupName $VMRGname -Name $OSDisk
$location = (Get-AzureRmResource -Name $OSDisk).Location
$day = (Get-Date).Day
$month = Get-Date -Format MMM
$year = (Get-Date).Year
$OSSnapshotName = ($VMName + '_' + $day + '_' + $month + '_' + $year + '_Snapshot_OS_Disk_' + $i)
$Snapshot = New-AzureRmSnapshotConfig -SourceUri $Disk.Id -CreateOption Copy -Location $location
New-AzureRmSnapshot -Snapshot $Snapshot -SnapshotName $OSSnapshotName -ResourceGroupName $VMRGname | Out-Null
Write-Host "Snapshot Name : $OSSnapshotName" -ForegroundColor Green
Start-Sleep -s 4


#Move Snap to storage account 
$blobName = $OSSnapshotName
#Mention storage account Name
$storageAccountName = "storagename"
$storageAccountRGName = (Get-AzureRmResource -Name $storageAccountName -ResourceType "Microsoft.Storage/storageAccounts").ResourceGroupName
$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccountRGName -Name $storageAccountName).Value | Select-Object -First 1
$SnapshotAccessURL = (Grant-AzureRMSnapshotAccess -ResourceGroupName $VMRGname -SnapshotName $blobName -Access 'Read' -DurationInSecond 172800).AccessSAS
$absoluteUri = $SnapshotAccessURL
#Mention storage account container Name
$destContainer = “snapshotsblob”
$destContext = New-AzureStorageContext –StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
Start-AzureStorageBlobCopy -AbsoluteUri $absoluteUri -DestContainer $destContainer -DestContext $destContext -DestBlob $blobName -Force
Write-Host "I have moved This $OSSnapshotName to storage account ." -ForegroundColor Green
$i =$i+1
#$Disk.Id
}
$king
$i = 1
ForEach($DataDisk in $DataDisks)
{
Write-Host "Data-Disk No $i : $DataDisk"
Write-Host "Now I am creating snapshot of Disk $i " -ForegroundColor White
$Disk = Get-AzureRmDisk -ResourceGroupName $VMRGname -Name $DataDisk
$location = (Get-AzureRmResource -Name $DataDisk).Location
$day = (Get-Date).Day
$month = Get-Date -Format MMM
$year = (Get-Date).Year
$DataSnapshotName = ($VMName + '_' + $day + '_' + $month + '_' + $year + '_Snapshot_Data_Disk_' + $i)
$Snapshot = New-AzureRmSnapshotConfig -SourceUri $Disk.Id -CreateOption Copy -Location $location
New-AzureRmSnapshot -Snapshot $Snapshot -SnapshotName $DataSnapshotName -ResourceGroupName $VMRGname | Out-Null
Write-Host "Snapshot Name : $DataSnapshotName" -ForegroundColor Green
Start-Sleep -s 4


#Move Snap to storage account 
$blobName = $DataSnapshotName
#Mention storage account Name
$storageAccountName = "storagename"
$storageAccountRGName = (Get-AzureRmResource -Name $storageAccountName -ResourceType "Microsoft.Storage/storageAccounts").ResourceGroupName
$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccountRGName -Name $storageAccountName).Value | Select-Object -First 1
$SnapshotAccessURL = (Grant-AzureRMSnapshotAccess -ResourceGroupName $VMRGname -SnapshotName $blobName -Access 'Read' -DurationInSecond 172800).AccessSAS
$absoluteUri = $SnapshotAccessURL
#Mention storage account container Name
$destContainer = “snapshotsblob”
$destContext = New-AzureStorageContext –StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
Start-AzureStorageBlobCopy -AbsoluteUri $absoluteUri -DestContainer $destContainer -DestContext $destContext -DestBlob $blobName -Force
Write-Host "I have moved This $DataSnapshotName to storage account ." -ForegroundColor Green
$i ++
#$disk.id
$king
}
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("Congrats ! This task is successfully completed! Don't forget to delete snapshots !! ",0, "Build By : Subhendu , Version : V1")
}

Leave a Reply

Your email address will not be published. Required fields are marked *