New-CosmosDocument.ps1

function New-CosmosDocument {
    <#
    .SYNOPSIS
    Creates a new Cosmos Document
     
    .DESCRIPTION
    Creates a new Cosmos Document in the Collection you specify
     
    .PARAMETER DatabaseName
    Name of the Database containing the Collection where the document should be created
     
    .PARAMETER CollectionName
    Name of the Collection where the Document should be created
     
    .PARAMETER Document
    A hashtable containing your document. Remember the id key. The cmdlet converts the hashtable to json
     
    .PARAMETER CosmosDBVariables
    This is the Script variable generated by Connect-CosmosDB - no need to supply this variable, unless you get really creative
     
    .EXAMPLE
    $Document = @{
            id='91387b17-2f3b-4a2d-bb6f-d4b9362990f0'
            GivenName='Utter'
            Surname='Despair'}
 
    New-CosmosDocument -DatabaseName MyPrivateCosmos -CollectionName Chaos -Document $Document
 
     
    .NOTES
    https://docs.microsoft.com/en-us/rest/api/documentdb/create-a-document
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true,
        HelpMessage='Name of the Database containing the Collection')]
        [string]$DatabaseName,
        [Parameter(Mandatory=$true,
        HelpMessage='Name of the Collection where the Document should be created')]
        [string]$CollectionName,
        [Parameter(Mandatory=$true,
        HelpMessage='The document formatted as a hashtable')]
        [hashtable]$Document,
        [Parameter(Mandatory=$false,
        HelpMessage="Use Connect-CosmosDB to create this Variable collection")]
        [hashtable]$CosmosDBVariables=$Script:CosmosDBVariables
    )
    
    begin {
        Test-CosmosDBVariable $CosmosDBVariables
        $Database = $CosmosDBConnection[($DatabaseName + '_db')]
        if (-not $Database) {
            Write-Warning "$DatabaseName not found"
            continue
        }
        $Collection = $CosmosDBConnection[$DatabaseName][$CollectionName]
        if (-not $Collection) {
            Write-Warning "Collection $CollectionName Database $DatabaseName not found"
            continue
        }
        if (!$Document['id']){
            Write-Warning "There is no id value in the document"
            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-is-upsert'] = 'true'
        $CosmosBody = $Document | ConvertTo-Json
        Write-Verbose $CosmosBody
        try {
            $Return = Invoke-RestMethod -Uri $Url -Headers $Header -Method $Verb -Body $CosmosBody -ErrorAction Stop
            Write-Verbose "$($Document['id']) has been created in $CollectionName"
        }
        catch {
            Write-Warning -Message $_.Exception.Message
        }
    }
    
    end {
    }
}