GinShell.Azure/Public/ConvertTo-GsAzureDiskToSnapshot.ps1

function ConvertTo-GsAzureDiskToSnapshot {
    param (
        [Parameter(Mandatory = $true)]
        [Object[]]$Disk,

        [string]$Location = 'CentralIndia'
    )

    $OutputSnapshots = @()

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

        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
            }
            else {
                Write-GsLog -Message "Snapshot '$snapshotName' already exists. Updating it instead." -Type Warning
                $newSnapshot = Update-AzSnapshot -Snapshot $existingSnapshot -SnapshotName $snapshotName -ResourceGroupName $resourceGroup -Verbose
            }

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

    return $OutputSnapshots
}