Skip to Content

Hello community,

one year ago I presented here the possibility to use Windows Power Shell 2 scripting language inside ABAP. As examples I presented here how to use VB# with this szenario, and here how to call DLL functions. Now a tiny update to the version 3 of PowerShell.

You can download from SAPIEN Technologies Inc. a free library to use PowerShell 3 with ABAP – ActiveXPoshV3x86.exe. Install and register it. It is now very easy possible to use PowerShell inside ABAP, here an example which stores the PowerShell script inside an ABAP include – look here for a detailed description with a VBScript example. Our PowerShell script containes a VB# class with three methods.

#-Begin—————————————————————–

$VBCode = @”

Option Strict On

Imports System

Imports Microsoft.VisualBasic

Namespace VBCode

  Public Class VB

    Public Shared Function Hello1() As String

      Return “Hello World!”

    End Function

    Public Function Hello2(ByVal Name As String) As String

      Return “Hello ” & Name & “!”

    End Function

    Public Sub Hello3(ByVal Name As String)

      MsgBox(Name, MsgBoxStyle.OkOnly, “Hello”)

    End Sub

  End Class

End Namespace

“@;

Add-Type -TypeDefinition $VBCode -Language VisualBasic

$VB = new-Object VBCode.VB

[VBCode.VB]::Hello1()

$VB.Hello2(“Stefan”)

$VB.Hello3(“Stefan”)

#-End——————————————————————-

It is necessary to store this code in an own include.

zPowerShell.JPG

With the function module zReadInclAsString and the following report you can use the PowerShell code and execute it inside your ABAP report.

“-Begin—————————————————————–

  Program zUseVBSharp.

    “-TypePools———————————————————

      Type-Pools OLE2 .

    “-Constants——————————————————–

      Constants OUTPUT_CONSOLE Type i Value 0.

      Constants OUTPUT_WINDOW Type i Value 1.

      Constants OUTPUT_BUFFER Type i Value 2.

    “-Variables———————————————————

      Data PS Type OLE2_OBJECT.

      Data Result Type i Value 0.

      Data strResult Type String Value ”.

      Data tabResult Type Table Of String.

      Data PSCode Type String Value ”.

      Data InclName Type SOBJ_NAME Value ‘ZPOWERSHELL’.

    “-Main————————————————————–

      Create Object PS ‘SAPIEN.ActiveXPoSHV3’.

      If sy-subrc = 0 And PS-Handle <> 0 And PS-Type = ‘OLE2’.

        Call Method Of PS ‘Init’ = Result Exporting #1 = 0.

        If Result = 0.

          Call Method Of PS ‘IsPowerShellInstalled’ = Result.

          If Result <> 0.

            Set Property Of PS ‘OutputMode’ = OUTPUT_BUFFER.

            “-Read PowerShell code from include file——————–

              Call Function ‘ZREADINCLASSTRING’

                Exporting I_InclName = InclName

                Importing E_strIncl = PSCode.

            Call Method Of PS ‘Execute’ Exporting

              #1 = PSCode.

            Call Method Of PS ‘OutputString’ = strResult.

            Split strResult At cl_abap_char_utilities=>cr_lf

              Into Table tabResult.

            Loop At tabResult Into strResult.

              Write: / strResult.

            EndLoop.

          EndIf.

        EndIf.

        Free Object PS.

      EndIf.

“-End——————————————————————-

zUseVBSharpABAP.JPG

Here the result.

Result.JPG

With the library from SAPIEN you can use also actual PowerShell 3 and dotNET 4 functionalities easily inside your ABAP program.

Enjoy it.

Cheers

Stefan

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Praveer Sen

    Hi,

    Nice blog, i was trying the same,

    could you explain more about the below step.,

    “SAPIEN Technologies Inc. a free library to use PowerShell 3 with ABAP – ActiveXPoshV3x86.exe. Install and register”

    I was searching in SAPIEN site, but not able to find the ActiveXPoshV3x86 related informaiton, is it comes with PowerShell Studio?

    And also provide the Registration Informaiton for the same.

    Regards.

    Praveer.

    (0) 
    1. Stefan Schnell Post author

      Hello Praveer,

      thanks for your request.

      At first I will answer your question to get the installation files from SAPIEN.

      1. Go to http://www.sapien.com/
      2. Go to Support > Support Downloads
        /wp-content/uploads/2015/01/001_624328.jpg
      3. Log in and go to Free Components

        /wp-content/uploads/2015/01/002_624359.jpg
        Here you find the installation files.

      4. Download both or the x86 file.

        /wp-content/uploads/2015/01/003_624360.jpg

      5. Install both or the x86 version.

      This is the first step. My way to register the library I will describe in my next reply.

      You are welcome.

      Cheers

      Stefan

      (0) 
    2. Stefan Schnell Post author

      Hello Praveer,

      here now the second part, my way to register the library. Don’t be surprised or scared about my way, it is absolut different from anything you know. The description is for an x86 OS.

      1. At first I generate via the command
        RegAsm.exe C:\MyPath\x86\ActiveXPoshV3.dll /regfile
        a file to register.
      2. I switch all keys from HKCR to HKCU – with this trick it is not necessary to need local admin rights to register the library.
      3. I add the key CodeBase to InprocServer32 and InprocServer32\3.0.0.0 with the value file:///C:\MyPath\x86\ActiveXPoshV3.dll.
      4. With the execution of this file I register the library.

      This is the second step.

      Let us know your results.

      Cheers

      Stefan

      (0) 
        1. Stefan Schnell Post author

          Hello Praveer,

          yes, but you need the x86 library, because SAP GUI for Windows is an x86 application. In this case you must change the keys to HKCU too, but also you must add the node Wow6432Node into the path, e.g. like this:

          HKCU\SOFTWARE\Classes\Wow6432Node\CLSID\{45334B68-7AC6-4CC0-B96A-3050AA0794B2}\Implemented Categories.

          Cheers

          Stefan

          (0) 

Leave a Reply