GinShell.Azure/Public/Invoke-GsAzureVmDiskToStorageAsSnapshotBlob.ps1

function Invoke-GsAzureVmDiskToStorageAsSnapshotBlob {
    <#
    .SYNOPSIS
        End-to-end workflow: authenticate, snapshot VM disks, copy to blob storage, clean up.
    .DESCRIPTION
        Orchestrates the full snapshot-and-transfer pipeline:
        1. Authenticate to Azure
        2. Fetch VM and disk information
        3. Create snapshots from all attached disks
        4. Transfer snapshots to a storage account as blobs
        5. Delete temporary snapshots
    .PARAMETER TenantId
        Source Azure AD tenant ID.
    .PARAMETER SubscriptionId
        Source Azure subscription ID.
    .PARAMETER VmName
        One or more source VM names.
    .PARAMETER StorageAccountName
        Destination storage account.
    .PARAMETER ContainerName
        Destination blob container.
    .PARAMETER StorageAccountKey
        Access key for the destination storage account.
    .EXAMPLE
        Invoke-GsAzureVmDiskToStorageAsSnapshotBlob -TenantId $tid -SubscriptionId $sid -VmName 'VM1' -StorageAccountName 'store1' -ContainerName 'snaps' -StorageAccountKey $key
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter(Mandatory)]
        [string]$TenantId,

        [Parameter(Mandatory)]
        [string]$SubscriptionId,

        [Parameter(Mandatory)]
        [string[]]$VmName,

        [Parameter(Mandatory)]
        [string]$StorageAccountName,

        [Parameter(Mandatory)]
        [string]$ContainerName,

        [Parameter(Mandatory)]
        [string]$StorageAccountKey
    )

    # Step 1: Authenticate
    try {
        Write-GsLog -Message "Authenticating to Azure with Tenant: $TenantId and Subscription: $SubscriptionId" -Type Action
        Connect-GsAzureAccount -TenantId $TenantId -SubscriptionId $SubscriptionId
    }
    catch {
        Write-GsLog -Message "Failed to authenticate to Azure: $($_.Exception.Message)" -Type Error
        return
    }

    # Step 2: Fetch VM info
    try {
        Write-GsLog -Message "Fetching VM information for: $($VmName -join ', ')" -Type Info
        $vmInfo = Get-GsAzureVmFullInfo -VmName $VmName
        if (-not $vmInfo -or -not $vmInfo.Disks) {
            Write-GsLog -Message "No disks found for the specified VMs." -Type Error
            return
        }
    }
    catch {
        Write-GsLog -Message "Error fetching VM information: $($_.Exception.Message)" -Type Error
        return
    }

    # Step 3: Create snapshots
    try {
        Write-GsLog -Message "Creating snapshots from attached disks..." -Type Info
        $snapshots = ConvertTo-GsAzureDiskToSnapshot -Disk $vmInfo.Disks
        if (-not $snapshots -or $snapshots.Count -eq 0) {
            Write-GsLog -Message "No snapshots were created." -Type Error
            return
        }
    }
    catch {
        Write-GsLog -Message "Error creating snapshots: $($_.Exception.Message)" -Type Error
        return
    }

    # Step 4: Transfer to storage
    $blobs = $null
    try {
        Write-GsLog -Message "Starting snapshot copy to storage account '$StorageAccountName' container '$ContainerName'" -Type Info
        $blobs = Send-GsAzureSnapshotsToStorage -Snapshot $snapshots -StorageAccountName $StorageAccountName -ContainerName $ContainerName -StorageAccountKey $StorageAccountKey -WaitForComplete
        Write-GsLog -Message "Snapshots successfully transferred. Total blobs: $($blobs.Count)" -Type Success
    }
    catch {
        Write-GsLog -Message "Snapshot copy failed: $($_.Exception.Message)" -Type Error
    }

    # Step 5: Clean up snapshots
    try {
        Write-GsLog -Message "Deleting temporary snapshots..." -Type Warning
        Remove-GsAzureSnapshot -Snapshot $snapshots
        Write-GsLog -Message "All snapshots successfully deleted." -Type Success
    }
    catch {
        Write-GsLog -Message "Snapshot deletion failed: $($_.Exception.Message)" -Type Error
    }

    return $blobs
}