Skip to Content

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.

12 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 https://www.sapien.com/
      2. Go to My Account > Download Quick Guides and more
      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) 
  2. Manuel Hildemaro Ramos Sanchez

    Hello Stefan.

    I took your blog as guide to solve a problem. but what i i did is not working:

    1.- Install ActiveXPoshV3x64.exe and ActiveXPoshV3x86.exe ( i’am working on x64 ).

     

    2.- Generete reg. file using RegAsm.exe and add CodeBase for x64 and x86

     

    3.- In ABAP program   CREATE OBJECT ps ‘SAPIEN.ActiveXPoSHV3’ is returning sy-subrc = 2

     

    Can you think of any advice on this?

     

    Thanks.

     

    Regards,

     

    Manuel H.

    (1) 
    1. Stefan Schnell Post author

      Hello Manuel,

      thanks for your reply.

      Your way looks very good.

      Try it direct with the following commands to register the libraries:

      C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe "C:\Program Files (x86)\SAPIEN Technologies, Inc\ActiveXPowerShell V3\\ActiveXPoshV3.dll" /codebase
      
      C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe "C:\Program Files\SAPIEN Technologies, Inc\ActiveXPowerShell V3\ActiveXPoshV3.dll" /codebase

      You must be login as an administrator to use this commands successfully.

      Let us know your results.

      Best regards
      Stefan

      (0) 
  3. Manuel Hildemaro Ramos Sanchez

    Hi Stefan.

    Thanks for your reply.

    I registered the libraries with the command:

    x64

    x86

    However CREATE OBJECT ps ‘SAPIEN.ActiveXPoSHV3’ still is returning sy-subrc = 2

     

    I tried uninstalling and installing again ( ommit regedit changes with /regfile , only /codebase ) but the result is the same.

    Should the library register  in SOLE ?

    Thanks again.

    Regards,

    Manuel H.

     

     

     

    (0) 
    1. Stefan Schnell Post author

      Hello Manuel,

      try this VBScript and let us know the results.

      Best regards
      Stefan

      '-Begin-----------------------------------------------------------------
      
        '-Directives----------------------------------------------------------
        Option Explicit
      
        '-Constants-----------------------------------------------------------
        Const OUTPUT_BUFFER = 2
      
        '-Sub Main------------------------------------------------------------
        Sub Main()
      
          '-Variables---------------------------------------------------------
          Dim PS, PSCode, PSVersion
      
          Set PS = CreateObject("SAPIEN.ActiveXPoSHV3")
          If Not IsObject(PS) Then
            MsgBox "Can't create ActiveXPoSH", vbOkOnly, "Important hint"
          End If
      
          If PS.IsPowerShellInstalled() = vbFalse Then
            Set PS = Nothing
            MsgBox "PowerShell is not installed", vbOkOnly, "Important hint"
            Exit Sub
          End If
      
          If PS.Init(vbFalse) <> 0 Then
            Set PS = Nothing
            MsgBox "Can't instantiate PowerShell engine", vbOkOnly, "Important hint"
            Exit Sub
          End If
      
          PS.OutputMode = OUTPUT_BUFFER
      
          PSCode = "Write-Host -Separator '' " & _
            "$PSVersionTable.PSVersion.Major . " & _
            "$PSVersionTable.PSVersion.Minor . " & _
            "$PSVersionTable.PSVersion.Build . " & _
            "$PSVersionTable.PSVersion.MinorRevision" & vbCrLf
      
          PS.Execute(PSCode)
          PSVersion = PS.OutputString()
          PS.ClearOutput()
          MsgBox PSVersion, vbOkOnly, "PowerShell version"
      
          Set PS = Nothing
      
        End Sub
      
        '-Main----------------------------------------------------------------
        Main
      
      '-End-------------------------------------------------------------------

       

      (0) 
  4. Manuel Hildemaro Ramos Sanchez

    Hi Stefan.

     

    I ran the script and i think that works.

    Result:

     

    However in ABAP program CREATE OBJECT ps ‘SAPIEN.ActiveXPoSHV3’ still is returning sy-subrc = 2 

     

    In this case, libraries registered with regasm.exe should be configured in SOLE ? or is not necessary ?

     

    Regards,

    Manuel H.

     

    (1) 
    1. Stefan Schnell Post author

      Hello Manuel,

      yes, ActiveXPoshV3 works on your computer, but not with the SAP GUI for Windows.

      No, it is not necessary to configure the library with the TAC SOLE.

      It is possible to forbid the access to ActiveX libaries in the SAP Logon:

      If exists a rule which forbids the access it isn’t possible for the SAP GUI for Windows to use this library. Maybe in your case it is the same. Please take a look at your security configuration if there is an entry like *.dll Control Execute Deny Enabled.

      Let us know your result.

      Best regards
      Stefan

       

      (0) 

Leave a Reply