Private/Send-RequestFile.ps1

<#
    .DESCRIPTION
    Wrapper for Nutanix API version 0.3.
 
    .NOTES
    Author: Timothy Rasiah
#>


if ($PSVersionTable.PSVersion -ge [System.Version]"7.0") {
    function Send-RequestFile {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true)]
            [ValidateSet("GET", "PUT")]
            [String]$method,
            [Parameter(Mandatory=$true)]
            [String]$endpoint,
            # [PSObject]$data = @{},
            [Hashtable]$headers = @{},
            [Hashtable]$params = @{},
            [Parameter(Mandatory=$true)]
            [ValidateNotNullOrEmpty()]
            [String]$LiteralPath
        )

        if (!$Global:DefaultNutanixV3Connection) {
            Write-Error -Exception "You are not currently connected to a server. Please connect first using a Connect cmdlet."
            return
        }

        $url = "$($Global:DefaultNutanixV3Connection[0].BaseUrl)$($endpoint)"
        $querystring = foreach ($key in $params.Keys) { $key, $params[$key] -join '=' }
        $uri = if ($querystring) { "$($url)?$($querystring -join '&')" } else { $url }

        $headers["Authorization"] = "$($Global:DefaultNutanixV3Connection[0].BasicAuth)"
        $contenttype = "application/octet-stream"

        # $body = [System.IO.File]::ReadAllBytes($LiteralPath)

        switch ($method) {
            "GET" {
                $response = Invoke-RestMethod -Method $method -Uri $uri -Headers $headers -ContentType $contenttype -OutFile $LiteralPath -SkipCertificateCheck
            }
            "PUT" {
                $response = Invoke-RestMethod -Method $method -Uri $uri -Headers $headers -ContentType $contenttype -InFile $LiteralPath -SkipCertificateCheck
            }
        }
        
        return $response
    }
}
else {
    function Send-RequestFile {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true)]
            [ValidateSet("GET", "PUT")]
            [String]$method,
            [Parameter(Mandatory=$true)]
            [String]$endpoint,
            # [PSObject]$data = @{},
            [Hashtable]$headers = @{},
            [Hashtable]$params = @{},
            [Parameter(Mandatory=$true)]
            [ValidateNotNullOrEmpty()]
            [String]$LiteralPath
        )

        if (!$Global:DefaultNutanixV3Connection) {
            Write-Error -Exception "You are not currently connected to a server. Please connect first using a Connect cmdlet."
            return
        }

        Disable-SSLCertificationVerification

        $url = "$($Global:DefaultNutanixV3Connection[0].BaseUrl)$($endpoint)"
        $querystring = foreach ($key in $params.Keys) { $key, $params[$key] -join '=' }
        $uri = if ($querystring) { "$($url)?$($querystring -join '&')" } else { $url }

        $headers["Authorization"] = "$($Global:DefaultNutanixV3Connection[0].BasicAuth)"
        $contenttype = "application/octet-stream"

        # $body = [System.IO.File]::ReadAllBytes($LiteralPath)

        switch ($method) {
            "GET" {
                $response = Invoke-RestMethod -Method $method -Uri $uri -Headers $headers -ContentType $contenttype -OutFile $LiteralPath
            }
            "PUT" {
                $response = Invoke-RestMethod -Method $method -Uri $uri -Headers $headers -ContentType $contenttype -InFile $LiteralPath
            }
        }
        
        return $response
    }
}