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

Powershell Script to create Snapshot from VM – One VM at a time

Live in Future - Live in Cloud

Powershell Script to create Snapshot from VM – One VM at a time

Greetings to all unbelievable students across globe . Allow me to present one of the the most useful script for production use . We need to create VM disks (OS+Data) snapshot for various reason . We all know how to create that one through portal or through PowerShell . But for that you need maintain multiple details like VM name , RG name , disk name etc . This script is a ultimate solution for snapshot creation . Beauty of this script is you dont have to mention VM name at the beginning . Script will prompt you for the input . Type the VM name , sit back and relax . Script will collect all required information from the VM name and will pass in the script when needed . You can run this script directly in production . It will not intaratpt anytghing unless there is any dependecy with VM snap .  

Only limitation of this script is this script will work with only one VM at a single time . I will post a new script that will work with multiple VM in one script .

Few point to keep in mind :

  • Please run Powershell ISE in admin mode and copy and paste this script in scripting environment .
  • Make sure you have installed and imported AzureRM module.
  • You should have access to fetch VM details in production . I am owner of my EA subscription .

 

clear
Write-Host "Hey Bro Greetings !!" -ForegroundColor Cyan 
Start-Sleep -s 1
Write-Host "My Name is King's Automation System . Designed By Subhendu ." -ForegroundColor Cyan 
Start-Sleep -s 1
Write-Host "So you want to create snapshot of disks of a VM ." -ForegroundColor Cyan 
Start-Sleep -s 1
Write-Host "Don't Worry ! Can you please tell me the VM Name ." -ForegroundColor Cyan 
Start-Sleep -s 1

Add-Type -AssemblyName Microsoft.VisualBasic
$User = [Microsoft.VisualBasic.Interaction]::InputBox('Please type VM name', 'Build By Subhendu V1', "Please Type VM name")
$king = '---------------------------------------------------------------------------------------'
Write-Host "Great !! So I will continue with "$user" vm" -ForegroundColor Yellow

#Just type the VM name over here . Version 1 . Build by Subhendu 
$VMName = $User
$VMRGname = (Get-AzureRmResource -Name $VMName -ResourceType "Microsoft.Compute/virtualMachines").ResourceGroupName
$OSDisk = (Get-AzureRMVM –Name $VMName –ResourceGroupName $VMRGname).StorageProfile.OsDisk.Name
$DataDisk = (Get-AzureRMVM –Name $VMName –ResourceGroupName $VMRGname).StorageProfile.DataDisks.name

$i = 1
$king

ForEach($OSDisk in $OSDisk)
{
Write-Host "OSDisk 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
Write-Host "Snapshot Name : $OSSnapshotName" -ForegroundColor Green
$i =$i+1
}

$king
$i = 1
ForEach($DataDisk in $DataDisk)
{
Write-Host "DataDisk 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
Write-Host "Snapshot Name : $DataSnapshotName" -ForegroundColor Green
$i =$i+1
$king
}

$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("Congrats ! This task is successfully completed!",0, "Build By : Subhendu , Version : V1")

Leave a Reply

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