远程服务器返回错误:(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脚本在集合内插入一些虚拟记录.
I have a PowerShell script for creating database and collection inside Azure Cosmos DB. I am trying to insert some dummy records inside collection by using the below PowerShell script.
#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
但是,每当我运行上述脚本时,都会出现如下所示的错误:
But whenever I run the above script, I am getting the error like shown in below:
Invoke-WebRequest:远程服务器返回错误:(400)错误的请求.在C:\ Program Files \ WindowsPowerShell \ Modules \ CosmosDB \ 3.1.0.293 \ CosmosDB.psm1:5275 char:30+ ... $ requestResult =调用WebRequest @invokeWebRequestParameters+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:InvalidOperation:(System.Net.HttpWebRequest:HttpWebRequest)[Invoke-WebRequest],WebException+ FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Invoke-WebRequest : The remote server returned an error: (400) Bad Request. At 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 : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
那么,有人可以建议我如何解决上述问题吗?
So, can anyone suggest me how to resolve the above issue?
推荐答案
您使用 $ partitionkey ='demo'
创建了集合,因此需要使用它来新建文档工作正常.
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"
更新:
尝试如下所示的完整命令,它应该可以工作.
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"
}
}
这篇关于远程服务器返回错误:(400)错误的请求.在C:\ Program Files \ WindowsPowerShell \ Modules \ CosmosDB \ 3.1.0.293 \ CosmosDB.psm1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!