Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate Cosmos DB Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Cosmos DB data from PowerShell? This article demonstrates how to utilize the Cosmos DB Cmdlets for tasks like connecting to Cosmos DB data, automating operations, downloading data, and more.
The CData Cmdlets for Cosmos DB are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Cosmos DB.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Cosmos DB, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Cosmos DB data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Cosmos DB. To access Cosmos DB data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Cosmos DB.
Once you have acquired the necessary connection properties, accessing Cosmos DB data in PowerShell can be enabled in three steps.
To obtain the connection string needed to connect to a Cosmos DB account using the SQL API, log in to the Azure Portal, select Azure Cosmos DB, and select your account. In the Settings section, click Connection String and set the following values:
- AccountEndpoint: The Cosmos DB account URL from the Keys blade of the Cosmos DB account
- AccountKey: In the Azure portal, navigate to the Cosmos DB service and select your Azure Cosmos DB account. From the resource menu, go to the Keys page. Find the PRIMARY KEY value and set AccountKey to this value.
PowerShell
-
Install the module:
Install-Module CosmosDBCmdlets
-
Connect:
$cosmosdb = Connect-CosmosDB -AccountEndpoint "$AccountEndpoint" -AccountKey "$AccountKey"
-
Search for and retrieve data:
$name = "Morris Park Bake Shop" $customers = Select-CosmosDB -Connection $cosmosdb -Table "Customers" -Where "Name = `'$Name`'" $customers
You can also use the Invoke-CosmosDB cmdlet to execute SQL commands:
$customers = Invoke-CosmosDB -Connection $cosmosdb -Query 'SELECT * FROM Customers WHERE Name = @Name' -Params @{'@Name'='Morris Park Bake Shop'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Cosmos DB\lib\System.Data.CData.CosmosDB.dll")
-
Connect to Cosmos DB:
$conn= New-Object System.Data.CData.CosmosDB.CosmosDBConnection("AccountEndpoint=myAccountEndpoint;AccountKey=myAccountKey;") $conn.Open()
-
Instantiate the CosmosDBDataAdapter, execute an SQL query, and output the results:
$sql="SELECT City, CompanyName from Customers" $da= New-Object System.Data.CData.CosmosDB.CosmosDBDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.city $_.companyname }
Update Cosmos DB Data
PowerShell
Update-CosmosDB -Connection $CosmosDB -Columns @('City','CompanyName') -Values @('MyCity', 'MyCompanyName') -Table Customers -Id "Myid"
ADO.NET
$cmd = New-Object System.Data.CData.CosmosDB.CosmosDBCommand("UPDATE Customers SET Name='Morris Park Bake Shop' WHERE id = @myid", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.CosmosDB.CosmosDBParameter("@myid","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Cosmos DB Data
PowerShell
Add-CosmosDB -Connection $CosmosDB -Table Customers -Columns @("City", "CompanyName") -Values @("MyCity", "MyCompanyName")
ADO.NET
$cmd = New-Object System.Data.CData.CosmosDB.CosmosDBCommand("INSERT INTO Customers (Name) VALUES (@myName)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.CosmosDB.CosmosDBParameter("@myName","Morris Park Bake Shop")))
$cmd.ExecuteNonQuery()
Delete Cosmos DB Data
PowerShell
Remove-CosmosDB -Connection $CosmosDB -Table "Customers" -Id "Myid"
ADO.NET
$cmd = New-Object System.Data.CData.CosmosDB.CosmosDBCommand("DELETE FROM Customers WHERE id=@myid", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.CosmosDB.CosmosDBParameter("@myid","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject