Automate Dynamics NAV Integration Tasks from PowerShell

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access Dynamics NAV data from PowerShell? This article demonstrates how to utilize the Dynamics NAV Cmdlets for tasks like connecting to Dynamics NAV data, automating operations, downloading data, and more.

The CData ADO.NET Provider for Dynamics NAV is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Dynamics NAV.

ADO.NET Provider

The ADO.NET Provider provides a SQL interface for Dynamics NAV; this tutorial shows how to use the Provider to create, retrieve, update, and delete Dynamics NAV data.

Once you have acquired the necessary connection properties, accessing Dynamics NAV data in PowerShell can be enabled in three 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, 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, set the Company as well.

  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

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

Insert Dynamics NAV Data

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

Delete Dynamics NAV Data

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

Ready to get started?

Download a free trial of the Dynamics NAV Data Provider to get started:

 Download Now

Learn more:

Dynamics NAV Icon Dynamics NAV ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Dynamics NAV data including Items, Sales Orders, Purchase Orders, and more!