Connect Workday to your favorite reporting tools without moving data.
Learn More →Automate Dynamics GP Integration Tasks from PowerShell
Are you looking for a quick and easy way to access Dynamics GP data from PowerShell? We show how to use the Cmdlets for Dynamics GP and the CData ADO.NET Provider for Dynamics GP to connect to Dynamics GP data and synchronize, automate, download, and more.
The CData Cmdlets for Dynamics GP 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 GP.
Cmdlets or ADO.NET?
The cmdlets are not only a PowerShell interface to the Dynamics GP API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Dynamics GP data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Dynamics GP. To access Dynamics GP data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Dynamics GP.
After obtaining the needed connection properties, accessing Dynamics GP data in PowerShell consists of three basic steps.
To authenticate set the User and Password connection properties.
To connect set the URL to the Web services endpoint; for example, http://{servername}:{port}/Dynamics/GPService. Additionally, set CompanyId; you can obtain this value in the company setup window: Click Tools -> Setup -> Company.
By default, data summaries are not returned to save performance. Set LookupIds to true to return details such as line items; however, note that entities must be retrieved one at a time.
PowerShell
-
Install the module:
Install-Module DynamicsGPCmdlets
-
Connect:
$dynamicsgp = Connect-DynamicsGP -CompanyId "$CompanyId" -user "$user" -password "$password" -URL "$URL"
-
Search for and retrieve data:
$customername = "Bob" $salesinvoice = Select-DynamicsGP -Connection $dynamicsgp -Table "SalesInvoice" -Where "CustomerName = `'$CustomerName`'" $salesinvoice
You can also use the Invoke-DynamicsGP cmdlet to execute SQL commands:
$salesinvoice = Invoke-DynamicsGP -Connection $dynamicsgp -Query 'SELECT * FROM SalesInvoice WHERE CustomerName = @CustomerName' -Params @{'@CustomerName'='Bob'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Dynamics GP\lib\System.Data.CData.DynamicsGP.dll")
-
Connect to Dynamics GP:
$conn= New-Object System.Data.CData.DynamicsGP.DynamicsGPConnection("CompanyId=mycompanyId;user=myuser;password=mypassword;URL= http://{servername}:{port}/Dynamics/GPService;") $conn.Open()
-
Instantiate the DynamicsGPDataAdapter, execute an SQL query, and output the results:
$sql="SELECT CustomerName, TotalAmount from SalesInvoice" $da= New-Object System.Data.CData.DynamicsGP.DynamicsGPDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.customername $_.totalamount }
Update Dynamics GP Data
PowerShell
Update-DynamicsGP -Connection $DynamicsGP -Columns @('CustomerName','TotalAmount') -Values @('MyCustomerName', 'MyTotalAmount') -Table SalesInvoice -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.DynamicsGP.DynamicsGPCommand("UPDATE SalesInvoice SET CustomerName='Bob' WHERE Id = @myId", $conn)
$cmd.Parameters.Add(new System.Data.CData.DynamicsGP.DynamicsGPParameter("@myId","10456255-0015501366"))
$cmd.ExecuteNonQuery()
Insert Dynamics GP Data
PowerShell
Add-DynamicsGP -Connection $DynamicsGP -Table SalesInvoice -Columns @("CustomerName", "TotalAmount") -Values @("MyCustomerName", "MyTotalAmount")
ADO.NET
$cmd = New-Object System.Data.CData.DynamicsGP.DynamicsGPCommand("INSERT INTO SalesInvoice (CustomerName) VALUES (@myCustomerName)", $conn)
$cmd.Parameters.Add(new System.Data.CData.DynamicsGP.DynamicsGPParameter("@myCustomerName","Bob"))
$cmd.ExecuteNonQuery()
Delete Dynamics GP Data
PowerShell
Remove-DynamicsGP -Connection $DynamicsGP -Table "SalesInvoice" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.DynamicsGP.DynamicsGPCommand("DELETE FROM SalesInvoice WHERE Id=@myId", $conn)
$cmd.Parameters.Add(new System.Data.CData.DynamicsGP.DynamicsGPParameter("@myId","001d000000YBRseAAH"))
$cmd.ExecuteNonQuery()
CodeProject