Ready to get started?

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

 Download Now

Learn more:

SAP ERP Icon SAP ERP ADO.NET Provider

Straightforward SAP ERP integration. Now accessing SAP RFC's from .NET applications is as easy as querying SQL Server.

Automate SAP Integration Tasks from PowerShell



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

The CData Cmdlets for SAP are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time access to SAP.

PowerShell Cmdlets or ADO.NET Provider?

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

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

You can connect to SAP systems using either librfc32.dll, librfc32u.dll, NetWeaver, or Web Services (SOAP). Set the ConnectionType connection property to CLASSIC (librfc32.dll), CLASSIC_UNICODE (librfc32u.dll), NETWEAVER, or SOAP.

If you are using the SOAP interface, set the Client, RFCUrl, SystemNumber, User, and Password properties, under the Authentication section.

Otherwise, set Host, User, Password, Client, and SystemNumber.

Note: We do not distribute the librfc32.dll or other SAP assemblies. You must find them from your SAP installation and install them on your machine.

For more information, see this guide on obtaining the connection properties needed to connect to any SAP system.

PowerShell

  1. Install the module:

    Install-Module SAPERPCmdlets
  2. Connect:

    $saperp = Connect-SAPERP -Host "$Host" -User "$User" -Password "$Password" -Client "$Client" -System Number "$System Number" -ConnectionType "$ConnectionType" -Location "$Location"
  3. Search for and retrieve data:

    $ernam = "BEHRMANN" $mara = Select-SAPERP -Connection $saperp -Table "MARA" -Where "ERNAM = `'$ERNAM`'" $mara

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

    $mara = Invoke-SAPERP -Connection $saperp -Query 'SELECT * FROM MARA WHERE ERNAM = @ERNAM' -Params @{'@ERNAM'='BEHRMANN'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.SAPERP.SAPERPConnection("Host=sap.mydomain.com;User=EXT90033;Password=xxx;Client=800;System Number=09;ConnectionType=Classic;Location=C:/mysapschemafolder;") $conn.Open()
  3. Instantiate the SAPERPDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT MANDT, MBRSH from MARA" $da= New-Object System.Data.CData.SAPERP.SAPERPDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.mandt $_.mbrsh }