Quantcast
Channel: Scripting Languages
Viewing all articles
Browse latest Browse all 68

How to Call BAPI Function Modules with PowerShell via NCo

$
0
0

Hello community,

 

I presented here the possibility to use PowerShell via dotNET Connector (NCo) with SAP. Also I presented here two auxiliary functions for an easier using of PowerShell via dotNET Connector (NCo) with SAP.

 

Here now a short description with two examples how to call BAPI function modules (FM) with PowerShell and NCo. I use the FM BAPI_USER_CREATE1 and BAPI_USER_DELETE. In comparisaon to a normal function call we use also the FM BAPI_TRANSACTION_COMMIT to do an external commit. Also we use an additional context to start a stateful call sequence, to bind the destination’s connection pool with the current thread of execution, with BeginContext and EndContext.

 

Here the first example how to create the user MYUSER:

 

#-Begin-----------------------------------------------------------------

 

  #-Function Load-NCo---------------------------------------------------

    ...

 

  #-Function Get-Destination--------------------------------------------

    ...

 

  #-Function Invoke-SAPFunctionModule-----------------------------------

    Function Invoke-SAPFunctionModule {

 

      $destination = Get-Destination

 

      #-Metadata--------------------------------------------------------

        try {

          [SAP.Middleware.Connector.IRfcFunction]$bapiCreateUser =

            $destination.Repository.CreateFunction("BAPI_USER_CREATE1")

          [SAP.Middleware.Connector.IRfcFunction]$bapiTransactionCommit =

            $destination.Repository.CreateFunction("BAPI_TRANSACTION_COMMIT")

        }

        catch [SAP.Middleware.Connector.RfcBaseException] {

          Write-Host "Metadata error"

          break

        }

 

      #-Set importparameter---------------------------------------------

        $bapiCreateUser.SetValue("USERNAME", "MYUSER")

        [SAP.Middleware.Connector.IRfcStructure]$imPassword =

          $bapiCreateUser.GetStructure("PASSWORD")

        $imPassword.SetValue("BAPIPWD", "initial")

        [SAP.Middleware.Connector.IRfcStructure]$imAddress =

          $bapiCreateUser.GetStructure("ADDRESS")

        $imAddress.SetValue("FIRSTNAME", "My")

        $imAddress.SetValue("LASTNAME", "User")

        $imAddress.SetValue("FULLNAME", "MyUser")

 

      #-Open context----------------------------------------------------

        $rc =

          [SAP.Middleware.Connector.RfcSessionManager]::BeginContext($destination)

 

      #-Call function modules-------------------------------------------

        try {

          #-Create user-------------------------------------------------

            $bapiCreateUser.Invoke($destination)

          [SAP.Middleware.Connector.IRfcTable]$return =

            $bapiCreateUser.GetTable("RETURN")

          foreach ($line in $return) {

            Write-Host $line.GetValue("TYPE") "-" $line.GetValue("MESSAGE")

          }

          #-Commit------------------------------------------------------

            $bapiTransactionCommit.Invoke($destination)

        }

        finally {

          #-Close context-----------------------------------------------

            $rc =

              [SAP.Middleware.Connector.RfcSessionManager]::EndContext($destination)

        }

 

    }

 

  #-Main----------------------------------------------------------------

    Load-NCo

    Invoke-SAPFunctionModule

 

#-End-------------------------------------------------------------------

 

Here now the second example how to delete the user MYUSER:

 

#-Begin-----------------------------------------------------------------

 

  #-Function Load-NCo---------------------------------------------------

    ...

 

  #-Function Get-Destination--------------------------------------------

    ....

 

  #-Function Invoke-SAPFunctionModule-----------------------------------

    Function Invoke-SAPFunctionModule {

 

      $destination = Get-Destination

 

      #-Metadata--------------------------------------------------------

        try {

          [SAP.Middleware.Connector.IRfcFunction]$bapiDeleteUser =

            $destination.Repository.CreateFunction("BAPI_USER_DELETE")

          [SAP.Middleware.Connector.IRfcFunction]$bapiTransactionCommit =

            $destination.Repository.CreateFunction("BAPI_TRANSACTION_COMMIT")

        }

        catch [SAP.Middleware.Connector.RfcBaseException] {

          Write-Host "Metadaten-Fehler"

          break

        }

 

      #-Set importparameter---------------------------------------------

        $bapiDeleteUser.SetValue("USERNAME", "MYUSER")

 

      #-Open context----------------------------------------------------

        $rc =

          [SAP.Middleware.Connector.RfcSessionManager]::BeginContext($destination)

 

 

      #-Call function modules-------------------------------------------

        try {

          #-Delete user-------------------------------------------------

            $bapiDeleteUser.Invoke($destination)

          [SAP.Middleware.Connector.IRfcTable]$return =

            $bapiDeleteUser.GetTable("RETURN")

          foreach ($line in $return) {

            Write-Host $line.GetValue("TYPE") "-" $line.GetValue("MESSAGE")

          }

          #-Commit------------------------------------------------------

            $bapiTransactionCommit.Invoke($destination)

        }

        finally {

          #-Close context-----------------------------------------------

            $rc =

              [SAP.Middleware.Connector.RfcSessionManager]::EndContext($destination)

        }

 

    }

 

  #-Main----------------------------------------------------------------

    Load-NCo

    Invoke-SAPFunctionModule

 

#-End-------------------------------------------------------------------

 

This examples shows also how to use structures and tables in the import and export parameters.

 

PowerShell offers a lot of possibilities and among the shadow of the circumstance of the end of support of VBScript in a shorter distant future - first signs look here - seems it better to look for an alternative. In my case offers PowerShell all the things to replace VBScript.

 

Enjoy it.

 

Cheers

Stefan


Viewing all articles
Browse latest Browse all 68

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>