New-CosmosDatabase.ps1

function New-CosmosDatabase {
    <#
    .SYNOPSIS
    Creates a new CosmosDB Database
     
    .DESCRIPTION
    Creates a new CosmosDB Database
     
    .PARAMETER DatabaseName
    Name of the Database to create
     
    .PARAMETER CosmosDBVariables
    This is the Script variable generated by Connect-CosmosDB - no need to supply this variable, unless you get really creative
     
    .EXAMPLE
    New-CosmosDatabase -DatabaseName MyPrivateCosmos
     
    .NOTES
    https://docs.microsoft.com/en-us/rest/api/documentdb/create-a-database
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true,
        HelpMessage='Name of your new database')]
        [string]$DatabaseName,
        [Parameter(Mandatory=$false,
        HelpMessage="Use Connect-CosmosDB to create this Variable collection")]
        [hashtable]$CosmosDBVariables=$Script:CosmosDBVariables
    )
    
    begin {
        Test-CosmosDBVariable -CosmosDBVariables $CosmosDBVariables
    }
    
    process {
        $Database = $CosmosDBConnection[($DatabaseName + '_db')]
        if ($Database) {
            Write-Warning "$DatabaseName exists"
            continue
        }
        $Verb = 'POST'
        $Url = '{0}/{1}' -f $CosmosDBVariables['URI'],'dbs'
        $ResourceType = 'dbs'
        $Header = New-CosmosDBHeader -ResourceType $ResourceType -Verb $Verb
        $CosmosBody = @{id=$DatabaseName} | ConvertTo-Json
        try {
            $Return = Invoke-RestMethod -Uri $Url -Headers $Header -Method $Verb -Body $CosmosBody -ErrorAction Stop
            $script:CosmosDBConnection[$($Return.id + '_db')] = $Return
        }
        catch {
            Write-Warning -Message $_.Exception.Message
        }
    }
    
    end {
        If ($Return -and $return.id -eq $DatabaseName){
            Write-Verbose "$DatabaseName has been created"
        }
    }
}