# 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. ## Syntax ```{code-block} powershell :linenos: true $sapConnect ``` ## Remarks 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. ## Examples **Connecting to a SAP service with CSRF protection** ```{code-block} powershell :linenos: true 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:** ```{code-block} :emphasize-lines: 4 :linenos: true $sapConnect_System2 = { param($settings) # tokens expire every 24h on this system $reconnectInterval = 86400 * 1000 ([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 ``` ## Customization > In case the default implementation does not fit your needs ({download}`Download Example `) the PowerShell module and extract it to your projects location: ```{code-block} powershell :linenos: true .\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: ```{code-block} powershell :linenos: true Import-Module ".\Modules\connections_sap.psm1" ``` In order to use the customized SAP Connect for the authentication with powerGate, use the variable *\$extendedSapConnect*: ```{code-block} powershell :linenos: true $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: ```{code-block} powershell :linenos: true $global:erpSettings = $settings $settings.OnCreateMessageHandler = { $customHandler = New-Object Your.Custom.Handler # Your other custom code $erpSetting.OnApplyClientHandler.Invoke($customHandler) return $customHandler } ```