GinShell.Azure/Public/ConvertTo-GsAzureDiskToSnapshot.ps1

function ConvertTo-GsAzureDiskToSnapshot {
    <#
    .SYNOPSIS
        Creates Azure snapshots from one or more managed disks.
    .DESCRIPTION
        For each disk, creates a snapshot named snapshot_<DiskName>_<Date>_vhd.
        If a snapshot already exists with the same name, it is updated.
    .PARAMETER Disk
        One or more Azure disk objects (from Get-AzDisk).
    .PARAMETER Location
        Azure region for the snapshot. Defaults to CentralIndia.
    .EXAMPLE
        $vmInfo = Get-GsAzureVmFullInfo -VmName 'VM1'
        ConvertTo-GsAzureDiskToSnapshot -Disk $vmInfo.Disks
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter(Mandatory)]
        [Object[]]$Disk,

        [string]$Location = 'CentralIndia'
    )

    $OutputSnapshots = @()

    foreach ($currentDisk in $Disk) {
        $snapshotName  = "snapshot_$($currentDisk.Name)_$(Get-Date -Format 'yyyy_MM_dd')_vhd"
        $resourceGroup = $currentDisk.ResourceGroupName

        if (-not $PSCmdlet.ShouldProcess($currentDisk.Name, "Create snapshot '$snapshotName'")) { continue }

        try {
            $existingSnapshot = Get-AzSnapshot -SnapshotName $snapshotName -ResourceGroupName $resourceGroup -ErrorAction SilentlyContinue
            $snapshotConfig   = New-AzSnapshotConfig -SourceUri $currentDisk.Id -Location $Location -CreateOption Copy

            if (-not $existingSnapshot) {
                Write-GsLog -Message "Creating snapshot for disk '$($currentDisk.Name)' as '$snapshotName'" -Type Action
                $newSnapshot = New-AzSnapshot -Snapshot $snapshotConfig -SnapshotName $snapshotName -ResourceGroupName $resourceGroup -ErrorAction Stop
            }
            else {
                Write-GsLog -Message "Snapshot '$snapshotName' already exists. Updating." -Type Warning
                $newSnapshot = Update-AzSnapshot -Snapshot $existingSnapshot -SnapshotName $snapshotName -ResourceGroupName $resourceGroup -ErrorAction Stop
            }

            $OutputSnapshots += $newSnapshot
        }
        catch {
            Write-GsLog -Message "Failed to create snapshot for disk '$($currentDisk.Name)': $($_.Exception.Message)" -Type Error
        }
    }

    return $OutputSnapshots
}