![]() |
VOOZH | about |
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.
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:
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'}
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-CosmosDB -Connection $CosmosDB -Columns @('City','CompanyName') -Values @('MyCity', 'MyCompanyName') -Table Customers -Id "Myid"
$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()
Add-CosmosDB -Connection $CosmosDB -Table Customers -Columns @("City", "CompanyName") -Values @("MyCity", "MyCompanyName")
$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()
Remove-CosmosDB -Connection $CosmosDB -Table "Customers" -Id "Myid"
$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
Download a free trial of the Cosmos DB Data Provider to get started:
Download NowLearn more:
👁 Cosmos DB IconRapidly create and deploy powerful .NET applications that integrate with Cosmos DB document databases.