Public/Save-PrismOva.ps1

function Save-PrismOva {
    <#
    .SYNOPSIS
        Downloads an OVA to a local file.
 
    .DESCRIPTION
        Downloads the OVA file with the given external identifier and streams it to
        the path specified by -OutFile.
 
        The "Download an OVA" endpoint responds with an HTTP 302 redirect to a
        download URL along with an X-Redirect-Token cookie. This function follows that
        redirect, presenting the redirect token cookie, and writes the streamed
        response to disk.
 
    .NOTES
        Implements:
          GET /vmm/v{ApiVersion}/content/ovas/{ovaExtId}/file (operationId: getFileByOvaId)
 
        Uses the selected session's ApiVersion for the endpoint.
 
    .PARAMETER PrismSession
        The server/FQDN of the PrismSession to use. May be omitted when only one session exists.
 
    .PARAMETER ExtId
        The external identifier (UUID) of the OVA to download.
 
    .PARAMETER OutFile
        The local file path to write the downloaded OVA to.
 
    .EXAMPLE
        Save-PrismOva -ExtId '4886b652-32fb-4896-bf97-30583d18a76e' -OutFile 'C:\temp\server.ova'
 
    .OUTPUTS
        None. The OVA content is written to -OutFile.
    #>


    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false,
            HelpMessage = 'Prism Session Name')][string]$PrismSession,

        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = 'OVA external identifier (UUID)')]
        [Alias('Id', 'OvaExtId')][string]$ExtId,

        [Parameter(Mandatory = $true,
            HelpMessage = 'Local file path to write the OVA to')]
        [string]$OutFile
    )

    process {
        $PrismSessionConfig = Get-PrismSession $PrismSession
        if (-not $PrismSessionConfig) {
            Throw 'Prism Session Not Found. Use New-PrismSession command before using features.'
        }

        $ssl = $PrismSessionConfig.AllowInsecureSSL
        $uri = 'https://{0}:{1}/api/vmm/v{2}/content/ovas/{3}/file' -f
            $PrismSessionConfig.PrismServer, $PrismSessionConfig.PrismPort, $PrismSessionConfig.ApiVersion, $ExtId

        ## First request: do not auto-follow the redirect so we can capture the
        ## download Location and the X-Redirect-Token.
        $initialSplat = @{
            Uri                  = $uri
            Method               = 'GET'
            WebSession           = $PrismSessionConfig.PrismWebSession
            MaximumRedirection   = 0
            SkipHttpErrorCheck   = $true
            SkipCertificateCheck = $ssl
            ProgressAction       = 'SilentlyContinue'
        }

        try {
            $response = Invoke-WebRequest @initialSplat
        } catch {
            throw $_
        }

        if ($response.StatusCode -eq 302) {
            $location = $response.Headers.Location
            if ($location -is [array]) { $location = $location[0] }

            $redirectToken = $response.Headers['X-Redirect-Token']
            if ($redirectToken -is [array]) { $redirectToken = $redirectToken[0] }

            if (-not $location) {
                throw "Download an OVA: 302 redirect did not include a Location header for OVA [$ExtId]"
            }

            $downloadSplat = @{
                Uri                  = $location
                Method               = 'GET'
                OutFile              = $OutFile
                SkipCertificateCheck = $ssl
                ProgressAction       = 'SilentlyContinue'
            }
            if ($redirectToken) {
                $downloadSplat.Headers = @{ 'Cookie' = $redirectToken }
            }

            Invoke-WebRequest @downloadSplat
        }
        elseif ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300) {
            ## Some deployments may stream the content directly without a redirect.
            [System.IO.File]::WriteAllBytes($OutFile, $response.Content)
        }
        else {
            throw "Download an OVA failed for [$ExtId]. Status code: $($response.StatusCode)"
        }
    }
}