遠程服務器返回錯誤:(400) 錯誤請求。在 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)


問題描述

遠程服務器返回錯誤:(400) 錯誤請求。在 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 :遠程服務器返回錯誤:(400)錯誤請求。在 C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1:5275 char:30 + ... $requestResult = Invoke‑WebRequest @invokeWebRequestParameters + ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke‑ WebRequest],WebException + FullyQualifiedErrorId :


參考解法

方法 1:

You created the collection with the $partitionkey= 'demo', so you need to new the document with it, then it will work fine.

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

enter image description here

Update:

Try the complete command as below, it should work.

$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"

}
}

(by PradeepJoy Wang)

參考文件

  1. The remote server returned an error: (400) Bad Request. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1 (CC BY‑SA 2.5/3.0/4.0)

#azure #powershell #azure-cosmosdb-sqlapi #azure-cosmosdb






相關問題

將屬性添加到 Azure 表存儲中的實體似乎會將空屬性添加到表中的所有實體 (Adding property to entity in Azure Table Storage seems to add null property to all entities in table)

帶有 Azure WCF 服務的 Windows 8 應用 (Windows 8 App with Azure WCF Service)

當我嘗試查看連接到 TFS 的網站的部署選項卡時,無法檢索部署歷史記錄 (Failed to retrieve deployment history when I try to view the deployment tab of a web site connected to TFS)

是否可以在單個 Azure 項目中同時使用 New Relic 和 Azure Application Insights? (Is it possible to use both New Relic and Azure Application Insights together in single Azure project?)

有沒有辦法從 Azure 中獲取所有 DocumentDb 資源? (Is there a way to fetch all DocumentDb resoruces from Azure?)

Azure Search .Net SDK 中的複雜類型支持 (Complex Types support in Azure Search .Net SDK)

遠程服務器返回錯誤:(400) 錯誤請求。在 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)

如何從狀態“False(MissingEndpoints)”啟用 kube-system/metrics-server? (How to enable kube-system/metrics-server from status "False (MissingEndpoints)"?)

通過 smtp 從安裝為 azure 中的 IaaS 的服務器發送電子郵件 (sending emails via smtp from a server installed as IaaS in azure)

cosmosDB RU 吞吐量如何強制執行 (How is cosmosDB RU throughput enforced)

在使用 azure 流量管理器和 azure 應用程序網關與 WAF 時實現國家級阻止 (Achieve country level blocking while using azure traffic manager and azure application gateway with WAF)

Azure 計算機視覺將圖像旋轉 180 度 (Azure Computer Vision Rotating the image By 180 degrees)







留言討論