Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our CosmosDB Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Connecting to Azure Cosmos DB
Azure Cosmos DB supports connecting and authenticating by Account Key, through Azure AD, or through Azure Service Principal.
Account Key
Log in to the Azure Portal, select Azure Cosmos DB, and select your account.
Set the following to authenticate:
- AccountEndpoint: The Cosmos DB account URL. Set this to the URI value found in the Settings > Keys blade of the Cosmos DB account.
- AccountKey: A master key token or a resource token for connecting to Azure Cosmos DB. Set this to the PRIMARY KEY value found in the Settings > Keys blade of the Cosmos DB account.
- TokenType: (optional). Set this to "master" (the default value) if you are using a Master Token, which is a full permissions token generated during account creation. Otherwise, set this property to "resource" if you are using a Resource Token, which is a custom permissions token generated when a database user is set up.
Entra ID (Azure AD)
Note: Microsoft has rebranded Azure AD as Entra ID. In topics that require the user to interact with the Entra ID Admin site, we use the same names Microsoft does. However, there are still CData connection properties whose names or values reference "Azure AD".
Microsoft Entra ID is a multi-tenant, cloud-based identity and access management platform. It supports OAuth-based authentication flows that enable the driver to access Azure Cosmos DB endpoints securely.
Authentication to Entra ID via a web application always requires that you first create and register a custom OAuth application. This enables your application to define its own redirect URI, manage credential scope, and comply with organization-specific security policies.
For full instructions on how to create and register a custom OAuth application, see Creating an Entra ID (Azure AD) Application.
After setting AuthScheme to AzureAD, the steps to authenticate vary, depending on the environment. For details on how to connect from desktop applications, web-based workflows, or headless systems, see the following sections.
Desktop Applications
You can authenticate from a desktop application using either the driver's embedded OAuth application or a custom OAuth application registered in Microsoft Entra ID.
Option 1: Use the Embedded OAuth Application
This is a pre-registered application, included with the driver. It simplifies setup and eliminates the need to register your own credentials and is ideal for development environments, single-user tools, or any setup where quick and easy authentication is preferred.
Set the following connection properties:
- AuthScheme: AzureAD
- InitiateOAuth:
- GETANDREFRESH – Use for the initial login. Launches the login page and saves tokens.
- REFRESH – Use this setting when you have already obtained valid access and refresh tokens. Reuses stored tokens without prompting the user again.
When you connect, the driver opens the Microsoft Entra sign-in page in your default browser. After signing in and granting access, the driver retrieves the access and refresh tokens and saves them to the path specified by OAuthSettingsLocation.
Option 2: Use a Custom OAuth Application
If your organization requires more control, such as managing security policies, redirect URIs, or application branding, you can instead register a custom OAuth application in Microsoft Entra ID and provide its values during connection.
During registration, record the following values:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was that was generated when you registered your custom OAuth application.
- CallbackURL: A redirect URI you defined during application registration.
For full instructions on how to register a custom OAuth application and configure redirect URIs, see Creating an Entra ID (Azure AD) Application.
Set the following connection properties:
- AuthScheme: AzureAD
- InitiateOAuth:
- GETANDREFRESH – Use for the initial login. Launches the login page and saves tokens.
- REFRESH – Use this setting when you have already obtained valid access and refresh tokens. Reuses stored tokens without prompting the user again.
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- CallbackURL: A redirect URI you defined during application registration.
After authentication, tokens are saved to OAuthSettingsLocation. These values persist across sessions and are used to automatically refresh the access token when it expires, so you don't need to log in again on future connections.
Headless Machines
Headless environments like CI/CD pipelines, background services, or server-based integrations do not have an interactive browser. To authenticate using AzureAD, you must complete the OAuth flow on a separate device with a browser and transfer the authentication result to the headless system.
Setup options:
- Obtain and exchange a verifier code
- Use another device to sign in and retrieve a verifier code, which the headless system uses to request tokens.
- Transfer an OAuth settings file
- Authenticate on another device, then copy the stored token file to the headless environment.
Using a Verifier Code
- On a device with a browser:
- If using a custom OAuth app, set the following properties:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- Call the GetOAuthAuthorizationURL stored procedure to generate a sign-in URL.
- Open the returned URL in a browser. Sign in and grant grant permissions to the driver. You are redirected to the callback URL, which contains the verifier code.
- After signing in, save the value of the code parameter from the redirect URL. You will use this later to set the OAuthVerifier connection property.
- If using a custom OAuth app, set the following properties:
- On the headless machine:
- Set the following properties:
- AuthScheme: AzureAD
- OAuthVerifier: The verifier code you saved.
- OAuthSettingsLocation: The path of the file that holds the OAuth token values.
- For custom applications:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- After tokens are saved, reuse them by setting:
- OAuthSettingsLocation: Make sure this location grants read and write permissions to the driver to enable the automatic refreshing of the access token.
- For custom applications:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
- Set the following properties:
Transferring OAuth Settings
- On a device with a browser:
- Connect using the instructions in the Desktop Applications section.
- After connecting, tokens are saved to the file path in OAuthSettingsLocation. The default filename is OAuthSettings.txt.
- On the headless machine:
- Copy the OAuth settings file to the machine.
- Set the following properties:
- AuthScheme: AzureAD
- OAuthSettingsLocation: Make sure this location grants read and write permissions to the driver to enable the automatic refreshing of the access token.
- For custom applications:
- OAuthClientId: The client Id that was generated when you registered your custom OAuth application.
- OAuthClientSecret: The client secret that was generated when you registered your custom OAuth application.
After setup, the driver uses the stored tokens to refresh the access token automatically, no browser or manual login is required.
Azure Service Principal
Note: Microsoft has rebranded Azure AD as Entra ID. In topics that require the user to interact with the Entra ID Admin site, we use the same names Microsoft does. However, there are still CData connection properties whose names or values reference "Azure AD".
Azure Service Principal is role-based application-based authentication. This means that authentication is done per application, rather than per user.
All tasks taken on by the application are executed without a default user context, but based on the assigned roles.
The application access to the resources is controlled through the assigned roles' permissions.
For information about how to set up Azure Service Principal authentication, see Creating a Service Principal App in Entra ID (Azure AD).
Creating a Connection Object
You can then use the Connect-CosmosDB cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-CosmosDB -AccountEndpoint "myAccountEndpoint" -AccountKey "myAccountKey"
Retrieving Data
The Select-CosmosDB cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-CosmosDB -Connection $conn -Table "[CData].[Entities].Customers" -Columns @("City, CompanyName") -Where "Country='US'"
The Invoke-CosmosDB cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-CosmosDB -Connection $conn -Table [CData].[Entities].Customers -Where "Country = 'US'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[CData].[Entities].CustomersData.csv -NoTypeInformation
You will notice that we piped the results from Select-CosmosDB into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-CosmosDB -AccountEndpoint "myAccountEndpoint" -AccountKey "myAccountKey"
PS C:\> $row = Select-CosmosDB -Connection $conn -Table "[CData].[Entities].Customers" -Columns (City, CompanyName) -Where "Country = 'US'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
"Connection": {
},
"Table": "[CData].[Entities].Customers",
"Columns": [
],
"City": "MyCity",
"CompanyName": "MyCompanyName"
}
Deleting Data
The following line deletes any records that match the criteria:
Select-CosmosDB -Connection $conn -Table [CData].[Entities].Customers -Where "Country = 'US'" | Remove-CosmosDB
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Azure Cosmos DB, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\My[CData].[Entities].CustomersUpdates.csv | %{
$record = Select-CosmosDB -Connection $conn -Table [CData].[Entities].Customers -Where ("_id = `'"+$_._id+"`'")
if($record){
Update-CosmosDB -Connection $conn -Table [CData].[Entities].Customers -Columns @("City","CompanyName") -Values @($_.City, $_.CompanyName) -Where "_id = `'$_._id`'"
}else{
Add-CosmosDB -Connection $conn -Table [CData].[Entities].Customers -Columns @("City","CompanyName") -Values @($_.City, $_.CompanyName)
}
}