Ready to get started?

Learn more about the CData ADO.NET Provider for Microsoft CDS or download a free trial:

Download Now

Automate Microsoft CDS Integration Tasks from PowerShell

Are you looking for a quick and easy way to access Microsoft CDS data from PowerShell? We show how to use the Cmdlets for Microsoft CDS and the CData ADO.NET Provider for Microsoft CDS to connect to Microsoft CDS data and synchronize, automate, download, and more.

The CData Cmdlets for Microsoft CDS 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 Microsoft CDS.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Microsoft CDS API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Microsoft CDS data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Microsoft CDS. To access Microsoft CDS data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Microsoft CDS.

After obtaining the needed connection properties, accessing Microsoft CDS data in PowerShell consists of three basic steps.

You can connect without setting any connection properties for your user credentials. Below are the minimum connection properties required to connect.

  • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
  • OrganizationUrl: Set this to the organization URL you are connecting to, such as https://myorganization.crm.dynamics.com.
  • Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.

When you connect the Common Data Service OAuth endpoint opens in your default browser. Log in and grant permissions. The OAuth process completes automatically.

PowerShell

  1. Install the module:

    Install-Module CDSCmdlets
  2. Connect:

    $cds = Connect-CDS -OrganizationUrl "$OrganizationUrl"
  3. Search for and retrieve data:

    $name = "MyAccount" $accounts = Select-CDS -Connection $cds -Table "Accounts" -Where "Name = `'$Name`'" $accounts

    You can also use the Invoke-CDS cmdlet to execute SQL commands:

    $accounts = Invoke-CDS -Connection $cds -Query 'SELECT * FROM Accounts WHERE Name = @Name' -Params @{'@Name'='MyAccount'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Microsoft CDS\lib\System.Data.CData.CDS.dll")
  2. Connect to Microsoft CDS:

    $conn= New-Object System.Data.CData.CDS.CDSConnection("OrganizationUrl=https://myaccount.crm.dynamics.com/InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the CDSDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT AccountId, Name from Accounts" $da= New-Object System.Data.CData.CDS.CDSDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.accountid $_.name }

Update Microsoft CDS Data

PowerShell

Update-CDS -Connection $CDS -Columns @('AccountId','Name') -Values @('MyAccountId', 'MyName') -Table Accounts -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.CDS.CDSCommand("UPDATE Accounts SET Name='MyAccount' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.CDS.CDSParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Microsoft CDS Data

PowerShell

Add-CDS -Connection $CDS -Table Accounts -Columns @("AccountId", "Name") -Values @("MyAccountId", "MyName")

ADO.NET

$cmd = New-Object System.Data.CData.CDS.CDSCommand("INSERT INTO Accounts (Name) VALUES (@myName)", $conn) $cmd.Parameters.Add(new System.Data.CData.CDS.CDSParameter("@myName","MyAccount")) $cmd.ExecuteNonQuery()

Delete Microsoft CDS Data

PowerShell

Remove-CDS -Connection $CDS -Table "Accounts" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.CDS.CDSCommand("DELETE FROM Accounts WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.CDS.CDSParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()