Cmdlets for Microsoft Dynamics 365

Build 24.0.9060

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 Dynamics365 Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.

Installing and Connecting

If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.

Install-Module Dynamics365Cmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module Dynamics365Cmdlets;

You can then use the Connect-Dynamics365 cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-Dynamics365 -OrganizationUrl "https://myaccount.operations.dynamics.com/" -Edition "Sales"

Connecting to Microsoft Dynamics 365

Edition and OrganizationUrl are required connection properties for connection. The Microsoft Dynamics 365 driver supports connecting to the following Microsoft Dynamics 365 editions:

  • CustomerService
  • FieldService
  • FinOpsOnline
  • FinOpsOnPremise
  • HumanResources
  • Marketing
  • ProjectOperations
  • Sales

Note that "Supply Chain Management" is the same as "Finance and Operations." You can connect to it by setting the Edition to either "FinOpsOnline" or "FinOpsOnPremise."

For Microsoft Dynamics 365 Business Central, use the separate Microsoft Dynamics 365 Business Central driver.

OrganizationUrl is the URL to your Microsoft Dynamics 365 organization. For instance, https://orgcb42e1d0.crm.dynamics.com

Authenticating to Microsoft Dynamics 365

OAuth

If required by your organization, Admin Consent is needed when authorizing a new OAuth application for your Azure Tenant. In all OAuth flows, the first time someone in your organization installs and uses an OAuth application, an administrator for the organization must approve the application for their Azure Tenant.

Your organization can choose, however, to disable this requirement.

Desktop Applications

CData provides an embedded OAuth application that simplifies OAuth desktop Authentication. Alternatively, you can create a custom OAuth application. See Creating an Azure AD Application for information about creating custom applications and reasons for doing so.

For authentication, the only difference between the two methods is that you must set two additional connection properties when using custom OAuth applications. After setting the following connection properties, you are ready to connect:

  • AuthScheme: The AzureAD in your application settings.
  • OAuthClientId: (custom applications only) The client Id in your application settings.
  • OAuthClientSecret: (custom applications only) The client secret in your application settings.
  • CallbackURL: The Redirect URL in your application settings.

When you connect the cmdlet opens the OAuth endpoint in your default browser. Log in and grant permissions to the application.

Web Applications

When connecting via a Web application, you need to create and register a custom OAuth application with Microsoft Dynamics 365. See Creating an Azure AD Application for more information about custom applications. You can then use the cmdlet to get and manage the OAuth token values.

Get an OAuth Access Token

Set the following connection properties to obtain the OAuthAccessToken:

  • AuthScheme: AzureAD.
  • OAuthClientId: The client Id in your application settings.
  • OAuthClientSecret: The client secret in your application settings

Then call stored procedures to complete the OAuth exchange:

  1. Call the GetOAuthAuthorizationUrl stored procedure. Set the CallbackURL input to the callback URL you specified in your application settings. If necessary, set the Scope parameter to request custom permissions. The stored procedure returns the URL to the OAuth endpoint.
  2. Open the URL, log in, and authorize the application. You are redirected back to the callback URL.
  3. Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL. If necessary, set the Scope parameter to request custom permissions.

Once you have obtained the access and refresh tokens, you can connect to data and refresh the OAuth access token either automatically or manually.

Automatic Refresh of the OAuth Access Token

To have the driver automatically refresh the OAuth access token, set the following on the first data connection:

  • InitiateOAuth: REFRESH.
  • OAuthClientId: The client Id in your application settings.
  • OAuthClientSecret: The client secret in your application settings.
  • OAuthAccessToken: The access token returned by GetOAuthAccessToken.
  • OAuthRefreshToken: The refresh token returned by GetOAuthAccessToken.
  • OAuthSettingsLocation: The location where the cmdlet saves the OAuth token values, which persist across connections.
On subsequent data connections, the values for OAuthAccessToken and OAuthRefreshToken are taken from OAuthSettingsLocation.

Manual Refresh of the OAuth Access Token

The only value needed to manually refresh the OAuth access token when connecting to data is the OAuth refresh token.

Use the RefreshOAuthAccessToken stored procedure to manually refresh the OAuthAccessToken after the ExpiresIn parameter value returned by GetOAuthAccessToken has elapsed, then set the following connection properties:

  • OAuthClientId: The client Id in your application settings.
  • OAuthClientSecret: The client secret in your application settings.

Then call RefreshOAuthAccessToken with OAuthRefreshToken set to the OAuth refresh token returned by GetOAuthAccessToken. After the new tokens have been retrieved, open a new connection by setting the OAuthAccessToken property to the value returned by RefreshOAuthAccessToken.

Finally, store the OAuth refresh token so that you can use it to manually refresh the OAuth access token after it has expired.

Headless Machines

To configure the driver to use OAuth with a user account on a headless machine, you need to authenticate on another device that has an internet browser.

  1. Choose one of these two options:
    • Option 1: Obtain the OAuthVerifier value as described in "Obtain and Exchange a Verifier Code" below.
    • Option 2: Install the cmdlet on another machine and transfer the OAuth authentication values after you authenticate through the usual browser-based flow, as described in "Transfer OAuth Settings" below.
  2. Then configure the cmdlet to automatically refresh the access token from the headless machine.

You can follow the headless OAuth authentication flow using the OAuth credentials for your custom OAuth app. To create a custom OAuth application, see Creating an Azure AD Application. This section describes the procedure to authenticate and connect to data.

Option 1: Obtain and Exchange a Verifier Code

First, set the following properties on the headless machine:

  • InitiateOAuth: Set to OFF.
  • OAuthClientId: Set to the Client ID in your OAuth Integration settings.
  • OAuthClientSecret: Set to the Client Secret in your OAuth Integration settings.

Next, authenticate from another machine and obtain the OAuthVerifier connection property:

  1. Call the GetOAuthAuthorizationUrl stored procedure. Set the CallbackURL input to the Redirect URI you specified in your application settings. The stored procedure returns the URL to the OAuth endpoint.
  2. Open the returned URL in a browser. Log in and grant permissions to the cmdlet. You are then redirected to the callback URL, which contains the verifier code.
  3. Save the value of the Verifier. You must set the value of the Verifier in the OAuthVerifier connection property.

Finally, on the headless machine, set the following connection properties to obtain the OAuth authentication values:

  • OAuthClientId: Set to the Client ID in your OAuth Integration settings.
  • OAuthClientSecret: Set to the Client Secret in your OAuth Integration settings.
  • OAuthVerifier: Set to the verifier code.
  • OAuthSettingsLocation: Set to persist the encrypted OAuth authentication values to the specified location.
  • InitiateOAuth: Set to REFRESH.

Connect to Data

After the OAuth settings file is generated, set the following properties to connect to data:

  • OAuthSettingsLocation: Set to the location containing the encrypted OAuth authentication values. Make sure this location gives read and write permissions to the provider to enable the automatic refreshing of the access token.
  • InitiateOAuth: Set to REFRESH.

Option 2: Transfer OAuth Settings

To install the cmdlet on another machine, authenticate, and then transfer the resulting OAuth values:

  1. On a second machine, install the cmdlet and connect with the following properties set:
    • OAuthSettingsLocation: Set to a writable location.
    • OAuthClientId: Set to the client ID in your application settings.
    • OAuthClientSecret: Set to the client secret in your application settings.
    • CallbackURL: Set to the callback URL in your application settings.
  2. Test the connection to authenticate. The resulting authentication values are written and encrypted to the path specified by OAuthSettingsLocation. After you have successfully tested the connection, copy the OAuth settings file to your headless machine. On the headless machine, set the following connection properties to connect to data:
    • InitiateOAuth: Set to REFRESH.
    • OAuthSettingsLocation: Set to the location of your OAuth settings file. Make sure this location gives read and write permissions to the cmdlet to enable the automatic refreshing of the access token.

Admin Consent

Admin consent refers to when the Admin for an Azure Active Directory tenant grants permissions to an application which requires an admin to consent to the use case. The embedded application within the CData Cmdlets PowerShell Module for Microsoft Dynamics 365, contains no permissions that require admin consent. Therefore, this information applies only to custom applications.

Admin Consent Permissions

When creating a new OAuth application in the Azure Portal, you must specify which permissions the application requires. Some permissions may be marked stating "Admin Consent Required". For example, all Groups permissions require Admin Consent. If your application requires admin consent, there are a couple of ways this can be done.

The easiest way to grant admin consent is to just have an admin log into portal.azure.com and navigate to the application you have created in App Registrations. Under API Permissions, click Grant Consent, which grants your application the necessary permissions on the tenant on which it was created.

If your organization has multiple tenants or the application needs to be granted permissions for other tenants outside your organization, you can use the GetAdminConsentURL to generate the Admin Authorization URL. Unlike the GetOAuthAuthorizationUrl, there is no important information returned from this endpoint. If the tenant grants access, it returns a boolean indicating that permissions were granted.

After an admin grants consent, authentication may be performed as normal.

Client Credentials

Client credentials refers to a flow in OAuth where there is no direct user authentication taking place. Instead, credentials are created for just the application itself. All tasks taken by the application are done without a default user context. This makes the authentication flow a bit different from standard.

Client OAuth Flow

All permissions related to the client oauth flow require admin consent. This means the application embedded with the CData Cmdlets PowerShell Module for Microsoft Dynamics 365 cannot be used in the client oauth flow. You must create your own OAuth application to use client credentials. See Creating an Azure AD Application for more details.

In "App Registration" in portal.azure.com, navigate to API Permissions and select the Microsoft Graph permissions. There are two distinct sets of permissions - Delegated and Application permissions. The permissions used during client credential authentication are under Application Permissions. Select the applicable permissions you require for your integration.

You are ready to connect after setting one of the below connection properties groups depending on the authentication type.

  1. Client Secret
    • InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
    • AzureTenant: The tenant you wish to connect to.
    • OAuthGrantType: CLIENT.
    • OAuthClientId: The client Id in your app settings.
    • OAuthClientSecret: The client secret in your app settings.
  2. Certificate
    • InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
    • AzureTenant: The tenant you wish to connect to.
    • OAuthGrantType: CLIENT.
    • OAuthClientId: The client Id in your app settings.
    • OAuthJWTCert: The JWT Certificate store.
    • OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.

Azure AD

Azure AD is a connection type that leverages OAuth to authenticate. Set your AuthScheme to AzureAD.

Follow the steps below to authenticate with the credentials for a custom AzureAD app. See Creating an Azure AD Application.

There are two types of app authentication available: using a client secret and using a certificate. You can use any of them depending on the configured app authentication.

Get an AzureAD Access Token

You are ready to connect after setting one of the below connection properties groups depending on the authentication type.

  1. Client Secret
    • AuthScheme: AzureServicePrincipal in your app settings.
    • InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the AzureAD exchange and manually setting the OAuthAccessToken.
    • AzureTenant: The tenant you wish to connect to.
    • OAuthClientId: The Client Id in your app settings.
    • OAuthClientSecret: The client secret in your app settings.
  2. Certificate
    • AuthScheme: The AzureServicePrincipal in your app settings.
    • InitiateOAuth: GETANDREFRESH. You can use InitiateAzureAD to avoid repeating the AzureAD exchange and manually setting the OAuthAccessToken.
    • AzureTenant: The tenant you wish to connect to.
    • OAuthClientId: The client Id in your app settings.
    • OAuthJWTCert: The JWT Certificate store.
    • OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.

Azure Service Principal

The authentication as an Azure Service Principal is handled via the OAuth Client Credentials flow. It does not involve direct user authentication. Instead, credentials are created for just the application itself. All tasks taken by the app are done without a default user context, but based on the assigned roles. The application access to the resources is controlled through the assigned roles' permissions. Create an AzureAD App and an Azure Service Principal When authenticating using an Azure Service Principal, you must register an application with an Azure AD tenant. See Creating a Custom AzureAD Service Principal App for more information.

Assign a role to the application

To access resources in your subscription, you must assign a role to the application.

  1. Open the Subscriptions page by searching and selecting the Subscriptions service from the search bar.
  2. Select the subscription to assign the application to.
  3. Open the Access control (IAM) and select Add > Add role assignment to open the Add role assignment page.
  4. Select Owner as the role to assign to your created Azure AD app.
Complete the Authentication

To connect follow these steps:

  1. Set the AuthScheme to AzureServicePrincipal for using a client secret or AzureServicePrincipalCert for using a certificate in your application setting.
  2. Set the connection properties that apply to both schemes.
  3. Set the connection properties specific to the authentication scheme you selected.

For Both Client Secret and Certificate

Set these connection properties and then continue to the relevant section below:

  • InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
  • AzureTenant: The tenant you want to connect to.
Client Secret

Continue with the following:

  • OAuthClientId: The client Id in your application settings.
  • OAuthClientSecret: The client secret in your application settings.

Certificate

Continue with the following:

  • OAuthJWTCert: The JWT Certificate store.
  • OAuthJWTCertType: The type of the certificate store specified by OAuthJWTCert.

Retrieving Data

The Select-Dynamics365 cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-Dynamics365 -Connection $conn -Table "GoalHeadings" -Columns @("GoalHeadingId, GoalHeadingId") -Where "Name='MyAccount'"
The Invoke-Dynamics365 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-Dynamics365 -Connection $conn -Table GoalHeadings -Where "Name <> 'MyAccount'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myGoalHeadingsData.csv -NoTypeInformation

You will notice that we piped the results from Select-Dynamics365 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-Dynamics365 -OrganizationUrl "https://myaccount.operations.dynamics.com/" -Edition "Sales"
PS C:\> $row = Select-Dynamics365 -Connection $conn -Table "GoalHeadings" -Columns (GoalHeadingId, GoalHeadingId) -Where "Name <> 'MyAccount'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "GoalHeadings",
  "Columns":  [

  ],
  "GoalHeadingId":  "MyGoalHeadingId",
  "GoalHeadingId":  "MyGoalHeadingId"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-Dynamics365 -Connection $conn -Table GoalHeadings -Where "Name = 'MyAccount'" | Remove-Dynamics365

Modifying Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Microsoft Dynamics 365, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyGoalHeadingsUpdates.csv | %{
  $record = Select-Dynamics365 -Connection $conn -Table GoalHeadings -Where ("GoalHeadingId = `'"+$_.GoalHeadingId+"`'")
  if($record){
    Update-Dynamics365 -Connection $conn -Table GoalHeadings -Columns @("GoalHeadingId","GoalHeadingId") -Values @($_.GoalHeadingId, $_.GoalHeadingId) -Where "GoalHeadingId  = `'$_.GoalHeadingId`'"
  }else{
    Add-Dynamics365 -Connection $conn -Table GoalHeadings -Columns @("GoalHeadingId","GoalHeadingId") -Values @($_.GoalHeadingId, $_.GoalHeadingId)
  }
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9060