Getting 404 Powershell Queries from Cosmos DB

I am using this script from the download at this link. https://gallery.technet.microsoft.com/scriptcenter/How-to-query-Azure-Cosmos-0a9aa517 However, for some reason I get a 404 response.

I copied the direct db url. Entering a fake url gives me a "failed to resolve" error, so I know this place exists.

Based on the Azure CosmosDB API documentation: https://docs.microsoft.com/en-us/rest/api/documentdb/databases $ databaseID is set by the user and just needs to be unique, so I set it the same as the db name, and assigned it to the url.

Changing it to be different still gives me the same 404 response message (below).

Edit: Removed the original introduction of read comments

Powershell Script:

# add necessary assembly
#
Add-Type -AssemblyName System.Web

# generate authorization key
Function Generate-MasterKeyAuthorizationSignature
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][String]$verb,
        [Parameter(Mandatory=$true)][String]$resourceLink,
        [Parameter(Mandatory=$true)][String]$resourceType,
        [Parameter(Mandatory=$true)][String]$dateTime,
        [Parameter(Mandatory=$true)][String]$key,
        [Parameter(Mandatory=$true)][String]$keyType,
        [Parameter(Mandatory=$true)][String]$tokenVersion
    )

    $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);

    [System.Web.HttpUtility]::UrlEncode("type=$keyType&ver=$tokenVersion&sig=$signature")
}

# query
Function Query-CosmosDb
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][String]$EndPoint,
        [Parameter(Mandatory=$true)][String]$DataBaseId,
        [Parameter(Mandatory=$true)][String]$CollectionId,
        [Parameter(Mandatory=$true)][String]$MasterKey,
        [Parameter(Mandatory=$true)][String]$Query
    )

    $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}
    $contentType= "application/json "#  The original said "application/query+json", I tried both
    $queryUri = "$EndPoint$ResourceLink/docs"

    $result = Invoke-RestMethod -Method $Verb -ContentType $contentType -Uri $queryUri -Headers $header -Body $queryJson

    $result | ConvertTo-Json -Depth 10
}

# fill the target cosmos database endpoint uri, database id, collection id and masterkey

$DatabaseName = "" # name goes here
$MasterKey = "" #key goes here
$CollectionId = "transientUsers"

$DatabaseId = $DatabaseName
$CosmosDBEndPoint = "https://$DatabaseId.documents.azure.com:443/"

# query string
$Query = "SELECT * FROM transientUsers"

# execute
Query-CosmosDb -EndPoint $CosmosDBEndPoint -DataBaseId $DataBaseId -CollectionId $CollectionId -MasterKey $MasterKey -Query $Query

The error I am getting is:

Invoke-RestMethod : The remote server returned an error: (404) Not Found.
At D:\querycosmos\PowerShell\QueryCosmosDB.ps1:69 char:12
+ ...   $result = Invoke-RestMethod -Method $Verb -ContentType $contentType ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
+4
source share
1 answer

I noticed that you used $DatabaseIdin two places:

$ResourceLink = "dbs/$DatabaseId/colls/$CollectionId"

and

$CosmosDBEndPoint = "https://$DatabaseId.documents.azure.com:443/"

If it $DatabaseIdrefers to your account name, you will need to change your variable $ResourceLinkand use the database name inside your account containing the collection. If, however, it $DatabaseIdrefers to the database name, you will need to change $CosmosDBEndPointand use the account name there.

+1
source

Source: https://habr.com/ru/post/1678545/


All Articles