![]() |
VOOZH | about |
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.
The Cmdlets are not only a PowerShell interface to Dynamics NAV, 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.
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.
Install the module:
Install-Module DynamicsNAVCmdlets
Connect:
$dynamicsnav = Connect-DynamicsNAV -http://myserver:7048 "$http://myserver:7048" -User "$User" -Password "$Password" -ServerInstance "$ServerInstance"
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'}
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")
Connect to Dynamics NAV:
$conn= New-Object System.Data.CData.DynamicsNAV.DynamicsNAVConnection("http://myserver:7048;User=myserver\Administrator;Password=admin;ServerInstance=DYNAMICSNAV71;")
$conn.Open()
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-DynamicsNAV -Connection $DynamicsNAV -Columns @('Name','Prices_Including_VAT') -Values @('MyName', 'MyPrices_Including_VAT') -Table Customer -Id "MyId"
$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()
Add-DynamicsNAV -Connection $DynamicsNAV -Table Customer -Columns @("Name", "Prices_Including_VAT") -Values @("MyName", "MyPrices_Including_VAT")
$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()
Remove-DynamicsNAV -Connection $DynamicsNAV -Table "Customer" -Id "MyId"
$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()
CodeProject
Download a free trial of the Dynamics NAV Data Provider to get started:
Download NowLearn more:
👁 Dynamics NAV IconRapidly create and deploy powerful .NET applications that integrate with Dynamics NAV data including Items, Sales Orders, Purchase Orders, and more!