Public/Get-ZomentumDocuments.ps1

function Get-ZomentumDocuments {
    <#
        .SYNOPSIS
            Gets Documents from the Zomentum API.
        .DESCRIPTION
            Retrieves Documents from the Zomentum API - supports a variety of filtering parameters.
        .OUTPUTS
            A powershell object containing the response.
    #>

    [CmdletBinding()]
    Param(
        # Document ID for retrieving a single Document
        [Parameter( ParameterSetName = 'SingleFile', Mandatory = $True )]
        [Parameter( ParameterSetName = 'SinglePath', Mandatory = $True )]
        [Parameter( ParameterSetName = 'Single', Mandatory = $True )]
        [string]$DocumentID,
        # An Object containing filter variables
        [Parameter( ParameterSetName = 'Multiple')]
        [PSCustomObject]$Filters,
        # Allow Writing Directly to File, using the specified path and file name eg c:\temp\myfile.txt
        [Parameter( ParameterSetName = 'SingleFile', Mandatory = $True )]
        [String]$OutFile,
        # Allow Writing Directly to File, using the specified path and the original file name eg c:\temp\
        [Parameter( ParameterSetName = 'SinglePath', Mandatory = $True )]
        [String]$OutPath
    )
          
    if ($DocumentID) {
            Write-Verbose "Fetching Document"
            $DocumentResult = Invoke-ZomentumRequest -method get -resource "documents/download/$DocumentID" -RawResult
            Write-Verbose 'Processing Processing Document'
            if ($OutFile -or $OutPath) {
                # Get the file name or set it
                if ($OutPath) {
                    Write-Verbose "Attempting to output to path $OutPath"
                    $ContentDisposition = $DocumentResult.Headers.'Content-Disposition'
                    $Disposition = [System.Net.Mime.ContentDisposition]::new($ContentDisposition)
                    $FileName = $Disposition.FileName
                    $Path = Join-Path $OutPath $FileName
                } else {
                    Write-Verbose "Attempting to output to file $OutFile"
                    $Path = $OutFile
                }

                Write-Verbose "Writing File $Path"
                $File = [System.IO.FileStream]::new($Path, [System.IO.FileMode]::Create)
                $File.write($DocumentResult.Content, 0, $DocumentResult.RawContentLength)
                $File.close()
            } else {
                return $DocumentResult.Content
            }
    } else {
        Write-Verbose "Fetching All Document Metadata"
        $Documents = Invoke-ZomentumRequest -method get -resource "documents/zapier" -Filters $Filters -MultiFetch
        return $Documents
    }
    
  
}