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 DynamicsNAV 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 DynamicsNAVCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module DynamicsNAVCmdlets;
You can then use the Connect-DynamicsNAV cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-DynamicsNAV -URL "http://myserver:7048" -ServerInstance "DYNAMICSNAV71" -Password "MyPassword" -User "MyServer\MyUser"
Connecting to Microsoft Dynamics NAV
Before the cmdlet can connect with Microsoft Dynamics NAV, OData services need to be enabled on the server. Once OData Services are enabled, the cmdlet will be able to query any services that are published on the server.
In addition, specify a Url to a valid Microsoft Dynamics NAV server organization root (e.g. http://MyServer:7048) and a ServerInstance (e.g. DynamicsNAV71). If there is not a Service Default Company for the server, set the Company (e.g. 'CRONUS Canada, Inc.') as well.
In a multitenant installation, specify the tenant Id in Tenant (e.g. 'Cronus1').
Authenticating to Microsoft Dynamics NAV
To authenticate, set the User and Password properties to valid Microsoft Dynamics NAV logon credentials or Windows user credentials. Select the appropriate authentication method in AuthScheme.
Auth Schemes
The available authentication schemes are configured in IIS where Dynamics NAV is hosted. In IIS you can select to enable or disable Digest, Basic, Windows, or Anonymous authentication. Please consult with your Dynamics NAV admin to determine which authentication scheme is appropriate for you. Set AuthScheme to one of the following:
- NEGOTIATE (default) - It is part of the Windows authentication, also known as Kerberos.
- BASIC - Basic authentication.
- DIGEST - Digest authentication.
- NTLM - Part of the Windows authentication.
- NONE - Anonymous authentication.
Retrieving Data
The Select-DynamicsNAV cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-DynamicsNAV -Connection $conn -Table "Customer" -Columns @("No, Contact") -Where "Name='CData, Inc.'"The Invoke-DynamicsNAV 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-DynamicsNAV -Connection $conn -Table Customer -Where "Name <> 'CData, Inc.'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myCustomerData.csv -NoTypeInformation
You will notice that we piped the results from Select-DynamicsNAV 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-DynamicsNAV -URL "http://myserver:7048" -ServerInstance "DYNAMICSNAV71" -Password "MyPassword" -User "MyServer\MyUser" PS C:\> $row = Select-DynamicsNAV -Connection $conn -Table "Customer" -Columns (No, Contact) -Where "Name <> 'CData, Inc.'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Customer", "Columns": [ ], "No": "MyNo", "Contact": "MyContact" }
Deleting Data
The following line deletes any records that match the criteria:
Select-DynamicsNAV -Connection $conn -Table Customer -Where "Name = 'CData, Inc.'" | Remove-DynamicsNAV
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 NAV, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyCustomerUpdates.csv | %{ $record = Select-DynamicsNAV -Connection $conn -Table Customer -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-DynamicsNAV -Connection $conn -Table Customer -Columns @("No","Contact") -Values @($_.No, $_.Contact) -Where "Id = `'$_.Id`'" }else{ Add-DynamicsNAV -Connection $conn -Table Customer -Columns @("No","Contact") -Values @($_.No, $_.Contact) } }