Automate SAP Business One Integration Tasks from PowerShell

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

The CData ADO.NET Provider for SAP Business One 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 SAP Business One.

ADO.NET Provider

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

Once you have acquired the necessary connection properties, accessing SAP Business One data in PowerShell can be enabled in three steps.

To authenticate to SAP Business One you must provide the Userand Passwordproperties.

To connect to data, specify Url. This is your SAP Business One Service Layer root URL.

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for SAP Business One\lib\System.Data.CData.SAPBusinessOne.dll")
  2. Connect to SAP Business One:

    $conn= New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneConnection("Url=http://localhost:50000/b1s/v1;User=username;Password=password;CompanyDB=dbname;InitiateOAuth=GETANDREFRESH;")
    $conn.Open()
  3. Instantiate the SAPBusinessOneDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT DocEntry, DocType from Orders"
    
    $da= New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneDataAdapter($sql, $conn)
    $dt= New-Object System.Data.DataTable
    $da.Fill($dt)
    
    $dt.Rows | foreach {
        Write-Host $_.docentry $_.doctype
    }

Update SAP Business One Data

$cmd =  New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneCommand("UPDATE Orders SET DocType='dDocument_Items' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()

Insert SAP Business One Data

$cmd =  New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneCommand("INSERT INTO Orders (DocType) VALUES (@myDocType)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneParameter("@myDocType","dDocument_Items")))
$cmd.ExecuteNonQuery()

Delete SAP Business One Data

$cmd =  New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()

Ready to get started?

Download a free trial of the SAP Business One Data Provider to get started:

 Download Now

Learn more:

SAP Business One Icon SAP Business One ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with SAP Business One data including Accounts, Activities, Orders, Customers, and more!