Query on Snapsht

Glenn Maxwell 11,496 Reputation points
2024-11-09T22:07:37.7033333+00:00

Hi All,

I am using the PowerShell syntax below to take snapshots of Windows VMs. Will the current syntax use an incremental or full snapshot? I want to use a full snapshot. Additionally, I would like to take snapshots of Linux VMs. If I change -OsType Windows to -OsType Linux in the syntax, will it work? Does specifying -OsType Linux cover all Linux distributions (e.g., Red Hat, Ubuntu, SUSE, etc.)? I am currently using the snapshot naming format -Format MMddyyyy. How can I modify this to achieve a format like Nov102024 instead of 11102024? Please guide me.

$VmName = "WinVM01"
$VmResourceGroup = "myrg01"
$vm = Get-AzVM -Name $VmName -ResourceGroupName $VmResourceGroup
$loc = $vm.Location

#VM Snapshot

Write-Output "VM $($vm.name) OS Disk Snapshot Begin"
$snapshotdisk = $vm.StorageProfile
$OSDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $snapshotdisk.OsDisk.ManagedDisk.id -CreateOption Copy -Location $loc -OsType Windows
$snapshotNameOS = "$($snapshotdisk.OsDisk.Name)_snapshot_$(Get-Date -Format MMddyyyy)"

# OS Disk Snapshot

try {
    New-AzSnapshot -ResourceGroupName $VmResourceGroup -SnapshotName $snapshotNameOS -Snapshot $OSDiskSnapshotConfig -ErrorAction Stop
} catch {
    $_
}
     
Write-Output "VM $($vm.name) OS Disk Snapshot End"

# Data Disk Snapshots 
 
Write-Output "VM $($vm.name) Data Disk Snapshots Begin"

$dataDisks = ($snapshotdisk.DataDisks).name

foreach ($datadisk in $datadisks) 
    {

    $dataDisk = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $datadisk
    Write-Output "VM $($vm.name) data Disk $($datadisk.Name) Snapshot Begin"
    $DataDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $dataDisk.Id -CreateOption Copy -Location $loc
    $snapshotNameData = "$($datadisk.name)_snapshot_$(Get-Date -Format MMddyyyy)"

    New-AzSnapshot -ResourceGroupName $VmResourceGroup -SnapshotName $snapshotNameData -Snapshot $DataDiskSnapshotConfig -ErrorAction Stop
    Write-Output "VM $($vm.name) data Disk $($datadisk.Name) Snapshot End"   
    }

Write-Output "VM $($vm.name) Data Disk Snapshots End"  

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,008 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,558 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,605 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 46,896 Reputation points
    2024-11-10T16:59:40.5866667+00:00

    Use New-AzSnapshotConfig with the -Incremental parameter to create an incremental snapshot of the VM. Supply the config using the -Snapshot parameter on the New-AzSnapshot cmdlet.

    By default, New-AzShapshot creates a full snapshot.

    To change the date format, use "Get-Date -Format MMMddyyyy" (use three "M"s instead of 4).

    EDIT: corrected a couple of typos.

    0 comments No comments

  2. Mounika Reddy Anumandla 745 Reputation points Microsoft Vendor
    2024-11-11T07:42:49.7133333+00:00

    Hi Glenn Maxwell,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    In the PowerShell script you've provided for taking snapshots of a Windows VM in Azure, you are actually creating full snapshots rather than incremental ones.

    The documentation to create incremental snapshots is https://learn.microsoft.com/en-us/azure/virtual-machines/disks-incremental-snapshots?tabs=azure-cli#cli---list-incremental-snapshots. If you want to create incremental snapshots, the parameter Incremental should be mentioned in the script. And yes, by default as Rich Matheisen said, when running New-AzSnapshot, it creates a full snapshot of the managed disk.

    As per the documentation, you can take snapshots of Linux VMs in Azure using the same approach you've implemented for Windows VMs. Simply changing the -OsType parameter from Windows to Linux in your New-AzSnapshotConfig cmdlet will be appropriate for Linux VMs. Reference: https://learn.microsoft.com/en-us/powershell/module/az.compute/new-azsnapshotconfig?view=azps-12.4.0#example-1. As per my understanding, specifying -OsType Linux does indeed cover all Linux distributions in Azure, including Red Hat, Ubuntu, SUSE, CentOS, Debian, and more.

    The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. Get-Date can format the date and time in several .NET and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs.

    Please let me know if you have tried the date format suggested by Rich Matheisen, as it is a recommended approach.
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-7.4&viewFallbackFrom=powershell-7.2

    Hope this helps! Feel free to tag me in your comments, if you have any further queries.

    If you feel that your queries have been resolved, please accept the answer by clicking the "Upvote" and "Accept Answer" on the post.
    User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.