Get-CosmosDocument.ps1

function Get-CosmosDocument {
    <#
    .SYNOPSIS
    Gets a specific or all Documents from a specific Collection
     
    .DESCRIPTION
    Gets a specific or all Documents from a specific Collection in a specific Database
     
    .PARAMETER DatabaseName
    Name of the Database holding the Collection with the Documents you want
     
    .PARAMETER CollectionName
    The name of the Collection with the Documents you want
     
    .PARAMETER DocumentId
    The id of the document you want
     
    .PARAMETER All
    This switch returns all Documents in the Collection you specified in CollectionName
     
    .PARAMETER CosmosDBVariables
    This is the Script variable generated by Connect-CosmosDB - no need to supply this variable, unless you get really creative
     
    .EXAMPLE
    Get-CosmosDocument -All
 
    Returns all Documents in the Collection specified
 
    .EXAMPLE
    Get-CosmosDocument -DatabaseName MyPrivateCosmos -CollectionName Chaos -DocumentId 5598ae69-784f-40e6-bfdb-94d74b3706e7
     
    .NOTES
    https://docs.microsoft.com/en-us/rest/api/documentdb/list-documents
    https://docs.microsoft.com/en-us/rest/api/documentdb/get-a-document
    #>

    [CmdletBinding(DefaultParameterSetName='Named')]
    param (
        [Parameter(Mandatory=$true,
        HelpMessage='Name of the Database containing the Document')]
        [string]$DatabaseName,
        [Parameter(Mandatory=$true,
        HelpMessage='Name of the Collection containing the Document')]
        [string]$CollectionName,
        [Parameter(ParameterSetName='Named',
        Mandatory=$true,
        HelpMessage='Id of the Document')]
        [string]$DocumentId,
        [Parameter(ParameterSetName='All')]
        [switch]$All,
        [Parameter(Mandatory=$false,
        HelpMessage="Use Connect-CosmosDB to create this Variable collection")]
        [hashtable]$CosmosDBVariables=$Script:CosmosDBVariables
    )
    
    begin {
        Test-CosmosDBVariable $CosmosDBVariables
        $Database = $Script:CosmosDBConnection[($DatabaseName + '_db')]
        if (-not $Database) {
            Write-Warning "$DatabaseName not found"
            continue
        }
        $Collection = $Script:CosmosDBConnection[$DatabaseName][$CollectionName]
        if (-not $Collection) {
            Write-Warning "$CollectionName not found"
            continue
        }
    }
    
    process {
        $Verb = 'GET'
        $Url = '{0}/{1}docs' -f $CosmosDBVariables['URI'],$Collection._self
        $ResourceType = 'docs'
        $Header = New-CosmosDBHeader -resourceId $Collection._rid -resourceType $ResourceType -Verb $Verb
        try {
            $Return = Invoke-RestMethod -Uri $Url -Headers $Header -Method $Verb -ErrorAction Stop
            if ($All) {
                $Return.Documents
            }
            if ($DocumentId) {
                $Return.Documents | Where-Object {$_.id -match $DocumentId}
            }
        }
        catch {
            Write-Warning -Message $_.Exception.Message
        }
    }
    
    end {
    }
}