Ready to get started?

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

Download Now

Automate Dynamics NAV Integration Tasks from PowerShell

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

The CData Cmdlets for Dynamics NAV 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 Dynamics NAV.

Cmdlets or ADO.NET?

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

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

Before you can connect, OData Services will need to be enabled on the server. Once OData Services are enabled, you will be able to query any Services that are published on the server.

The User and Password properties, under the Authentication section, must be set to valid Dynamics NAV user credentials. In addition, you will need to specify a URL to a valid Dynamics NAV server organization root and a ServerInstance. If there is not a Service Default Company for the server, you will need to set the Company as well.

PowerShell

  1. Install the module:

    Install-Module DynamicsNAVCmdlets
  2. Connect:

    $dynamicsnav = Connect-DynamicsNAV -http://myserver:7048 "$http://myserver:7048" -User "$User" -Password "$Password" -ServerInstance "$ServerInstance"
  3. Search for and retrieve data:

    $name = "Bob" $customer = Select-DynamicsNAV -Connection $dynamicsnav -Table "Customer" -Where "Name = `'$Name`'" $customer

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

    $customer = Invoke-DynamicsNAV -Connection $dynamicsnav -Query 'SELECT * FROM Customer WHERE Name = @Name' -Params @{'@Name'='Bob'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.DynamicsNAV.DynamicsNAVConnection("http://myserver:7048;User=myserver\Administrator;Password=admin;ServerInstance=DYNAMICSNAV71;") $conn.Open()
  3. Instantiate the DynamicsNAVDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Name, Prices_Including_VAT from Customer" $da= New-Object System.Data.CData.DynamicsNAV.DynamicsNAVDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.prices_including_vat }

Update Dynamics NAV Data

PowerShell

Update-DynamicsNAV -Connection $DynamicsNAV -Columns @('Name','Prices_Including_VAT') -Values @('MyName', 'MyPrices_Including_VAT') -Table Customer -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsNAV.DynamicsNAVCommand("UPDATE Customer SET Name='Bob' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.DynamicsNAV.DynamicsNAVParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Dynamics NAV Data

PowerShell

Add-DynamicsNAV -Connection $DynamicsNAV -Table Customer -Columns @("Name", "Prices_Including_VAT") -Values @("MyName", "MyPrices_Including_VAT")

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsNAV.DynamicsNAVCommand("INSERT INTO Customer (Name) VALUES (@myName)", $conn) $cmd.Parameters.Add(new System.Data.CData.DynamicsNAV.DynamicsNAVParameter("@myName","Bob")) $cmd.ExecuteNonQuery()

Delete Dynamics NAV Data

PowerShell

Remove-DynamicsNAV -Connection $DynamicsNAV -Table "Customer" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsNAV.DynamicsNAVCommand("DELETE FROM Customer WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.DynamicsNAV.DynamicsNAVParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()