New-CosmosDocumentQuery.ps1

function New-CosmosDocumentQuery {
    <#
    .SYNOPSIS
    Query a Cosmos Collection
     
    .DESCRIPTION
    Execute a SQL query against a Cosmos Collection
     
    .PARAMETER DatabaseName
    Name of the Database containing the Collection you want to query against
     
    .PARAMETER CollectionName
    The name of the Collection you want to query against
     
    .PARAMETER Query
    PYour SQL query
     
    .PARAMETER Parameter
    a Hash table containing variables for your query
     
    .PARAMETER CosmosDBVariables
    This is the Script variable generated by Connect-CosmosDB - no need to supply this variable, unless you get really creative
     
    .EXAMPLE
    New-CosmosDocumentQuery -DatabaseName MyPrivateCosmos -CollectionName Chaos -Query "select * from c"
 
    Returns all documents in the Chaos collection
     
    .EXAMPLE
    $Parameters = @{
        '@GivenName' = 'Utter'
    }
    New-CosmosDocumentQuery -DatabaseName MyPrivateCosmos -CollectionName Chaos -Query "select * from c where c.GivenName = @GivenName" -Parameter $Parameters -Verbose
     
    For some reason this returns nothing. If anyone has any idea why queries more specific the "select * from c" returns nothing, please enlighten me!
    .NOTES
    https://docs.microsoft.com/en-us/rest/api/documentdb/query-documents
    #>

    [CmdletBinding()]
    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(Mandatory=$true,
        HelpMessage='SQL formatted query')]
        [string]$Query,
        [Parameter(Mandatory=$false,
        HelpMessage='Parameters in a hashtable form')]
        [hashtable]$Parameter,
        [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 = 'POST'
        $Url = '{0}/{1}docs' -f $CosmosDBVariables['URI'],$Collection._self
        $ResourceType = 'docs'
        $Header = New-CosmosDBHeader -resourceId $Collection._rid -resourceType $ResourceType -Verb $Verb
        $Header['x-ms-documentdb-isquery'] = $true
        $Header["Content-Type"] = "application/query+json"
        $BodyHash = [ordered]@{
            'query' = $Query
        }
        if ($Parameter) {
            $BodyHash['parameter'] = $Parameter.Keys | ForEach-Object {[PSCustomObject]@{'Name'=$_;'Value'=$Parameter[$_]}}
        }
        $CosmosBody = $BodyHash | ConvertTo-Json
        Write-Verbose $CosmosBody
        try {
            $Return = Invoke-RestMethod -Uri $Url -Headers $Header -Method $Verb -Body $CosmosBody 
        }
        catch {
            Write-Warning -Message $_.Exception.Message
        }
    }
    
    end {
        if ($Return) {
            $Return.Documents
        }
    }
}