powergate:code_reference:commandlets:objects:sapconnect

SapConnect

The SapConnect object is of type ScriptBlock and can be used with Connect-ERP to work with SAP systems.

The $sapConnect variable is based on the SapConnect implementation from the .NET library.

$sapConnect

The $sapConnect variable is available globally in every powershell session where the powerGate Module is imported.
It only supports connecting to the services from a single SAP system, by retrieving the X-CSRF-Token and cookie just once.

Connecting to a SAP service with CSRF protection

Connect-ERP -Service "http://sap.coolorange.com" -User "EX_DEMO" -Password "secret" -OnConnect $global:sapConnect

Connecting multiple SAP systems with different CSRF-Token expiration intervals:

$sapConnect_System2 = {
      param($settings)
               $reconnectInterval = 86400 ‬* 1000 # tokens expire every 24h on this system
               ([Action[powerGate.Erp.Client.ErpClientSettings]](New-Object powerGate.Erp.Client.SapConnect($reconnectInterval))).Invoke($settings)
}

Connect-ERP "http://sap.coolorange.com/CATALOGSERVICE" -OnConnect $Global:sapConnect
Connect-ERP "http://sap.some_other_company.com/CATALOGSERVICE" -OnConnect $sapConnect_System2

In case the default implementation does not fit your needs download the PowerShell module and extract it to your projects location:

.\Modules\connections_sap.psm1
Freely customize the module to your needs!

To make use of the module in your current PowerShell environment it has to be imported:
Import-Module ".\Modules\connections_sap.psm1"

In order to use the customized SAP Connect for the authentication with powerGate, use the variable $extendedSapConnect:
$connected = Connect-ERP "https://sap.coolorange.com/CATALOGSERVICE" -OnConnect $global:extendedSapConnect

Attention

If you are setting $settings.OnCreateMessageHandler then the $settings.OnApplyClientHandler will not be called anymore and the $extendedSapConnect will not work anymore. Therefore be sure to invoke it afterwards with the returned handler:

$global:erpSettings = $settings
$settings.OnCreateMessageHandler = {
     $customHandler = New-Object Your.Custom.Handler
     # Your other custom code
     $erpSetting.OnApplyClientHandler.Invoke($customHandler)
     return $customHandler
}