-1

I developed the PowerShell script for creating Database and Collection in the Azure Cosmos DB.

 #region Parameters

$connectionString='XXXXXXXXXXXXXXXXX=='
$accountName='demo-account-01'
$databaseName='demo-db-01'
$collectionName='demo-collection-01'

#endregion

#region Login into Azure using Interactive Mode

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

Connect-AzAccount 

#Set the current azure subscription
Select-AzSubscription  -subscription 'Visual Studio Ultimate with MSDN'

#endregion

#region Create Collection and insert some data into it

$key = Get-CosmosDbAccountMasterKey -Name $accountName -ResourceGroupName 'Demo'
$cosmosDbContext = New-CosmosDbContext -Account $accountName -Key $key
New-CosmosDbDatabase -Context $cosmosDbContext -Id $databaseName
#New-CosmosDbCollection -Context $cosmosDbContext -Id $collectionName -PartitionKey 'account' -OfferThroughput 50000 -Database $databaseName
New-CosmosDbCollection -Context $cosmosDbContext -Id $collectionName -OfferThroughput 2500 -Database $databaseName


#endregion

But when run the above script I am getting the error like shown in below:

Invoke-WebRequest : The remote server returned an error: (403) Forbidden. 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 issue?

Pradeep
  • 5,101
  • 14
  • 68
  • 140
  • What did you use to log in, service principal or user account? – Joy Wang Jan 21 '19 at 07:27
  • Besides, `az account set ` is azure cli not the powershell, if you want to select subscription, you need to use `Select-AzSubscription`. – Joy Wang Jan 21 '19 at 07:30
  • What means `The same I am getting` ? Could you look into my comment? – Joy Wang Jan 21 '19 at 07:34
  • Your script has more than one error, could you tell me what did you use to log in, service principal or user account? Because I see `#Login into Azure using Service Principal Details` and `Connect-AzAccount`. – Joy Wang Jan 21 '19 at 07:38
  • You can try to add `-Database $databaseName` parameter in this command `New-CosmosDbCollection`. – Joy Wang Jan 21 '19 at 07:46
  • My `CosmosDB` module version is `3.1.0.293`, the command works fine on my side. – Joy Wang Jan 21 '19 at 09:14
  • Did you add your user account as a role in `Access control (IAM)` of your cosmos db account? – Joy Wang Jan 21 '19 at 09:30
  • Where did you run the command? Did you set the firewall? I ran it locally. – Joy Wang Jan 21 '19 at 09:39
  • 1
    @JoyWang, The issue is with Firewall. whenever I added the my IP Address under `Firewall and virtual networks` section, then I am executing the above script successfully without any issues. – Pradeep Jan 21 '19 at 12:58
  • @JoyWang, Can you please look it once https://stackoverflow.com/questions/54303253/the-remote-server-returned-an-error-400-bad-request-at-c-program-files-wind – Pradeep Jan 22 '19 at 07:32

1 Answers1

1

You need to add your IP address in the Firewall and virtual networks, then it will work.

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • Is that the name of an application? It's called Windows Firewall. Then you have inbound/outboud rules and the program to allow. – jaques-sam Oct 01 '19 at 08:52