How to query Azure Cosmos DB resources using the REST API by PowerShell


This sample demonstrates how to query Azure Cosmos DB resources using the REST API by PowerShell.



In Microsoft Build 2017, Microsoft announces the global distribution database Azure Cosmos DB. It offers turnkey global distribution across any number of Azure regions by transparently scaling and replicating your data wherever your users are. It also natively supports multiple data models including documents, key-value, graph, and column-family.


Users can store and query data on Azure Cosmos DB by API, the API currently supports C#, Node.JS, Java, Python and Ruby. However, when IT Pros want to take a look the data from command line. They can’t find a clear way to do so.


This sample provides a PowerShell way to query Azure Cosmos DB using its REST API.


• Azure Subscription

• Create Azure Cosmos DB account

• PowerShell 3.0 or above versions


• Unzip the sample archive and open the script file

• Modify line 75 in script, fill in your Azure Cosmos DB endpoint, you can find it in Azure portal like the screenshot at below

• Modify line 76, 77, fill in your DatabaseId and CollectionId

• Modify line 78, fill the master key, you can find the key in Azure portal like the screenshot at below

• Modify line 81 to change the query according to your needs.



After running the script you will get similar output like the screenshot at below.


Here are some code snippets for your reference.


# generate authorization key 
Function Generate-MasterKeyAuthorizationSignature 
    $hmacSha256 = New-Object System.Security.Cryptography.HMACSHA256 
    $hmacSha256.Key = [System.Convert]::FromBase64String($key) 
    $payLoad = "$($verb.ToLowerInvariant())`n$($resourceType.ToLowerInvariant())`n$resourceLink`n$($dateTime.ToLowerInvariant())`n`n" 
    $hashPayLoad = $hmacSha256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($payLoad)) 
    $signature = [System.Convert]::ToBase64String($hashPayLoad); 
# query 
Function Query-CosmosDb 
    $Verb = "POST" 
    $ResourceType = "docs"; 
    $ResourceLink = "dbs/$DatabaseId/colls/$CollectionId" 
    $dateTime = [DateTime]::UtcNow.ToString("r") 
    $authHeader = Generate-MasterKeyAuthorizationSignature -verb $Verb -resourceLink $ResourceLink -resourceType $ResourceType -key $MasterKey -keyType "master" -tokenVersion "1.0" -dateTime $dateTime 
    $queryJson = @{query=$Query| ConvertTo-Json 
    $header = @{authorization=$authHeader;"x-ms-documentdb-isquery"="True";"x-ms-version"="2017-02-22";"x-ms-date"=$dateTime} 
    $queryUri = "$EndPoint$ResourceLink/docs" 
    $result = Invoke-RestMethod -Method $Verb -ContentType $contentType -Uri $queryUri -Headers $header -Body $queryJson 
    $result | ConvertTo-Json -Depth 10 
Additional Resources


• Azure Cosmos DB:

• Azure Cosmos DB Document:

Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.