Ready to get started?

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

 Download Now

Learn more:

SAP ERP Icon SAP ERP Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to SAP ERP data. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.

PowerShell Scripting to Replicate SAP Data to MySQL



Write a simple PowerShell script to replicate SAP data to a MySQL database.

The CData Cmdlets for SAP offer live access to SAP data from within PowerShell. Using PowerShell scripts, you can easily automate regular tasks like data replication. This article will walk through using the CData Cmdlets for SAP and the CData Cmdlets for MySQL in PowerShell to replicate SAP data to a MySQL database.

After obtaining the needed connection properties, accessing SAP data in PowerShell and preparing for replication consists of four basic 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.

Collecting SAP Data

  1. Install the module:

    Install-Module SAPERPCmdlets
  2. Connect to SAP:

    $saperp = Connect-SAPERP -Host $Host -User $User -Password $Password -Client $Client -System Number $System Number -ConnectionType $ConnectionType -Location $Location
  3. Retrieve the data from a specific resource:

    $data = Select-SAPERP -Connection $saperp -Table "MARA"

    You can also use the Invoke-SAPERP cmdlet to execute pure SQL-92 statements:

    $data = Invoke-SAPERP -Connection $saperp -Query 'SELECT * FROM MARA WHERE ERNAM = @ERNAM' -Params @{'@ERNAM'='BEHRMANN'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting SAP Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the SAP data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the SAP resource (MARA) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "MARA" -Columns $columns -Values $values }

You have now replicated your SAP data to a MySQL database. This gives you freedom to work with SAP data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.

Notes

  • Once you have connected to SAP and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-SAPERP -Connection $saperp -Table "MARA" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "MARA" -Columns $columns -Values $values }
  • If you wish to replicate the SAP data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-SAPERP cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}