{"id":64,"date":"2023-06-01T08:49:26","date_gmt":"2023-06-01T07:49:26","guid":{"rendered":"https:\/\/ishooscode.com\/?p=64"},"modified":"2023-06-01T09:12:16","modified_gmt":"2023-06-01T08:12:16","slug":"save-data-in-azure-cosmosdb-collection","status":"publish","type":"post","link":"https:\/\/ishooscode.com\/index.php\/2023\/06\/01\/save-data-in-azure-cosmosdb-collection\/","title":{"rendered":"Save data in Azure CosmosDb collection using powershell scripts"},"content":{"rendered":"\n<p>In this article, we are going to demonstrate how we can use PowerShell scripts to save data in Azure CosmosDb collection from a file leveraging\u00a0<a href=\"https:\/\/github.com\/PlagueHO\/CosmosDB\" target=\"_blank\" rel=\"noreferrer noopener\">CosmosDb<\/a>\u00a0PowerShell package<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/f4n3x6c5.stackpathcdn.com\/article\/pushing-data-to-cosmosdb-from-azure-pipeline-using-powershell\/Images\/PowershellToCosmosFlow.PNG\" alt=\"Save data in Azure CosmosDb collection\"\/><\/figure>\n\n\n\n<p>CosmosDb is a&nbsp;fully managed, nosql, non relational database which allows us to store data durably with several ACID configurations. Microsoft guarantees speed at any scale<\/p>\n\n\n\n<p>Let&#8217;s get started and write the code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#region param\n&#91;CmdletBinding()]\nparam (\n\t&#91;Parameter(Mandatory = $false)]\n\t&#91;string]$resourceGroup = \"\",\n\n\t&#91;Parameter(Mandatory = $false)]\n\t&#91;string]$database = \"\",\n\n\t&#91;Parameter(Mandatory = $false)]\n\t&#91;string]$cosmosAccount = \"\",\n\n\t&#91;Parameter(Mandatory = $false)]\n\t&#91;string]$filePath = \"..\/file.json\"\n)\n#endregion param\n\nFunction UpdateCosmosDb\n{\n\t$User = \"Azure-userId\"\n\t$PWord = ConvertTo-SecureString -String \"Password\" -AsPlainText -Force\n\t$tenant = \"Azure Tenant Id\"\n\t$subscription = \"cAzure Subscription Id\"\n\t$Credential = New-Object -TypeName \"System.Management.Automation.PSCredential\" -ArgumentList $User,$PWord\n\tConnect-AzAccount -Credential $Credential -Tenant $tenant -Subscription $subscription #Login into your azure account\n\n\t$module = Get-InstalledModule -Name 'CosmosDB'\n\tif($module -ne $null)\n\t{\n\t\twrite-host \"module CosmosDB avaiable\"\n\t}\n\telse\n\t{\n\t\twrite-host \"module CosmosDB not avaiable download in progress\"\n\t\tInstall-Module -Name CosmosDB -AllowClobber -force\n\t}\n\t$items = Get-Content -path $filePath -Encoding UTF8 -Raw | ConvertFrom-Json\n\t$cosmosDbContext = New-CosmosDbContext -Account $cosmosAccount -Database $database -ResourceGroup $resourceGroup\n\tforeach($item in $items)\n\t{\n\t\t$id = $item.id\n\t\t$item | Add-Member -NotePropertyName id -NotePropertyValue $id #creates new property in josn\n\t\t$document = $item | ConvertTo-Json #converts the object into json\n\t\t$query = \"SELECT c.id FROM c WHERE c.id = '$id'\"\n\t\twrite-host $query\n\t\t$existingDocument = Get-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'ContainerId' -Query $query\n\t\tif($existingDocument -ne $null)\n\t\t{\n\t\t\twrite-host \"Remove $document\"\n\t\t\tRemove-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'ContainerId' -PartitionKey \"$id\" -Id \"$id\"\n\t\t}\n\t\twrite-host \"New $document\"\n\t\tNew-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'ContainerId' -DocumentBody $document -PartitionKey \"$id\"\n\t}\n}\nUpdateCosmosDb<\/code><\/pre>\n\n\n\n<p><strong><ins>Note<\/ins><\/strong><br>If you are executing the above PowerShell in Azure pipeline with let&#8217;s say contributor role then you don&#8217;t need to perform Line No. 20 to 26<\/p>\n\n\n\n<p>Keep coding<\/p>\n\n\n\n<p>Cheers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to demonstrate how we can use PowerShell scripts to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":65,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[21],"tags":[6,7,10,9,8],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/64"}],"collection":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":3,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/64\/revisions\/157"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/media\/65"}],"wp:attachment":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}