Replicate Cosmos DB Data from PowerShell



Write a quick PowerShell script to query Cosmos DB data. Use connectivity to the live data to replicate Cosmos DB data to SQL Server.



The CData ODBC Driver for Cosmos DB enables out-of-the-box integration with Microsoft's built-in support for ODBC. The ODBC driver instantly integrates connectivity to the real Cosmos DB data with PowerShell.

You can use the .NET Framework Provider for ODBC built into PowerShell to quickly automate integration tasks like replicating Cosmos DB data to other databases. This article shows how to replicate Cosmos DB data to SQL Server in 5 lines of code.

You can also write PowerShell code to execute create, read, update, and delete (CRUD) operations. See the examples below.

Create an ODBC Data Source for Cosmos DB

If you have not already, first specify connection properties in an ODBC DSN (data source name). This is the last step of the driver installation. You can use the Microsoft ODBC Data Source Administrator to create and configure ODBC DSNs.

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.

Connect to Cosmos DB

The code below shows how to use the DSN to initialize the connection to Cosmos DB data in PowerShell:

$conn = New-Object System.Data.Odbc.OdbcConnection $conn.ConnectionString = "DSN=CData CosmosDB Source x64"

Back Up Cosmos DB Data to SQL Server

After you enable caching, you can use the code below to replicate data to SQL Server.

Set the following connection properties to configure the caching database:

  • CacheProvider: The name of the ADO.NET provider. This can be found in the Machine.config for your version of .NET. For example, to configure SQL Server, enter System.Data.SqlClient.

  • CacheConnection: The connection string of properties required to connect to the database. Below is an example for SQL Server:

    Server=localhost;Database=RSB;User Id=sqltest;Password=sqltest;

The SQL query in the example can be used to refresh the entire cached table, including its schema. Any already existing cache is deleted.

$conn.Open() # Create and execute the SQL Query $SQL = "CACHE DROP EXISTING SELECT * FROM " + $Customers $cmd = New-Object System.Data.Odbc.OdbcCommand($sql,$conn) $count = $cmd.ExecuteNonQuery() $conn.Close()

The driver gives you complete control over the caching functionality. See the help documentation for more caching commands and usage examples. See the help documentation for steps to replicate to other databases.

Other Operations

To retrieve Cosmos DB data in PowerShell, call the Fill method of the OdbcDataAdapter method. To execute data manipulation commands, initialize the OdbcCommand object and then call ExecuteNonQuery. Below are some more examples CRUD commands to Cosmos DB through the .NET Framework Provider for ODBC:

Retrieve Cosmos DB Data

$sql="SELECT City, CompanyName from Customers" $da= New-Object System.Data.Odbc.OdbcDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { $dt.Columns | foreach ($col in dt{ Write-Host $1[$_] } }

Update Cosmos DB Data

$cmd = New-Object System.Data.Odbc.OdbcCommand("UPDATE Customers SET Name='Morris Park Bake Shop' WHERE id = @myid", $conn) $cmd.Parameters.Add(new System.Data.Odbc.OdbcParameter("myid","001d000000YBRseAAH") $cmd.ExecuteNonQuery()

Insert Cosmos DB Data

$cmd = New-Object System.Data.Odbc.OdbcCommand("INSERT INTO Customers SET Name='Morris Park Bake Shop' WHERE id = @myid", $conn) $cmd.Parameters.Add(new System.Data.Odbc.OdbcParameter("myid","001d000000YBRseAAH") $cmd.ExecuteNonQuery()

Delete Cosmos DB Data

$cmd = New-Object System.Data.Odbc.OdbcCommand("DELETE FROM Customers WHERE id = @myid", $conn) $cmd.Parameters.Add(new System.Data.Odbc.OdbcParameter("myid","001d000000YBRseAAH") $cmd.ExecuteNonQuery()

Ready to get started?

Download a free trial of the Cosmos DB ODBC Driver to get started:

 Download Now

Learn more:

Cosmos DB Icon Cosmos DB ODBC Driver

The Cosmos DB ODBC Driver is a powerful tool that allows you to connect with live Cosmos DB document databases, directly from any applications that support ODBC connectivity.

Access Cosmos DB like you would a database - read, write, and update through a standard ODBC Driver interface.