The remote server returned an error: (400) Bad Request. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1

The remote server returned an error: (400) Bad Request. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1

我有一个 PowerShell 脚本,用于在 Azure Cosmos DB 中创建数据库和集合。我正在尝试使用以下 PowerShell 脚本在集合中插入一些虚拟记录。

    #region Parameters

$clientId= "XXXXXXXXXXXXXXX"
$clientSecret= "XXXXXXXXXXXX="
$subscriptionName= "XXXXXXXXXXXXXXX"
$tenantId= "XXXXXXXXXXXXXXXX"
$resourceGroupName= "Demo"
$connectionString='XXXXXXXXXXXXXXXXx=='
$cosmosDBAccounts= @('demo-account-01')
$databaseName='demo-db-01'
$collectionName='demo-collection-01'
$partitionkey= 'demo'

#endregion

#region Login into Azure using Interactive Mode or Service Principal details

# sign in
Write-Host "Logging in...";

#Connect-AzAccount 
$securePassword = $clientSecret | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $clientId, $securePassword
Connect-AzAccount -Credential $cred -ServicePrincipal -TenantId $tenantId

#Set the current azure subscription
Select-AzSubscription  -subscription $subscriptionName

#endregion

#region Create Collection and insert some data into it

foreach($cosmosDBAccount in $cosmosDBAccounts){

    $key = Get-CosmosDbAccountMasterKey -Name $cosmosDBAccount -ResourceGroupName $resourceGroupName
    $cosmosDbContext = New-CosmosDbContext -Account $cosmosDBAccount -Key $key
    New-CosmosDbDatabase -Context $cosmosDbContext -Id $databaseName
    New-CosmosDbCollection -Context $cosmosDbContext -Id $collectionName -PartitionKey $partitionkey -OfferThroughput 2500 -Database $databaseName
0..9 | Foreach-Object {

$document = @"
{
         "id": "$([Guid]::NewGuid().ToString())",
         "name": "pradeep",         
         "demo": "AAA"  
}
"@
New-CosmosDbDocument -Context $cosmosDbContext -CollectionId $collectionName -DocumentBody $document -PartitionKey "AAA"

}
}



#endregion

但是每当我 运行 上面的脚本时,我都会收到如下所示的错误:

Invoke-WebRequest : The remote server returned an error: (400) Bad Request. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB.1.0.293\CosmosDB.psm1:5275 char:30 + ... $requestResult = Invoke-WebRequest @invokeWebRequestParameters + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

那么,谁能建议我如何解决上述问题?

您使用 $partitionkey= 'demo' 创建了集合,因此您需要使用它来新建文档,然后它才能正常工作。

 $document = @"
{
         "id": "$([Guid]::NewGuid().ToString())",
         "name": "pradeep",         
         "demo": "AAA"  
}
"@
New-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'cll3' -DocumentBody $document -PartitionKey "AAA"

更新:

尝试下面的完整命令,应该可以。

$cosmosDBAccounts= @('joycosmos')
$resourceGroupName = 'joywebapp'
$partitionkey = 'demo'
$databaseName = 'db1'
$collectionName = 'clle'

foreach($cosmosDBAccount in $cosmosDBAccounts){

    $cosmosDbContext = New-CosmosDbContext -Account $cosmosDbAccount -Database $databaseName -ResourceGroup $resourceGroupName    
    New-CosmosDbDatabase -Context $cosmosDbContext -Id $databaseName
    New-CosmosDbCollection -Context $cosmosDbContext -Id $collectionName -PartitionKey $partitionkey -OfferThroughput 2500 -Database $databaseName

0..9 | Foreach-Object {

$document = @"
{
         "id": "$([Guid]::NewGuid().ToString())",
         "name": "pradeep",         
         "demo": "AAA"  
}
"@

New-CosmosDbDocument -Context $cosmosDbContext -CollectionId $collectionName -DocumentBody $document -PartitionKey "AAA"

}
}