Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor
I presented here the possibility how to use Windows PowerShell Script inside ABAP. Also is it possible to use Windows PowerShell in combination with SAP GUI Scripting. You Need therefor the free COM library ActiveXPosh.dll from SAPIEN.

Here an example how to use a DLL call from the Windows API inside an SAP GUI script:
'-Begin-----------------------------------------------------------------
'-
'- Example how to use a DLL call via PowerShell with component
'- ActiveXPosh inside SAP GUI Scripting
'-
'-----------------------------------------------------------------------

'-Directives----------------------------------------------------------
Option Explicit

'-Constants-----------------------------------------------------------
Const OUTPUT_CONSOLE = 0
Const OUTPUT_WINDOW = 1
Const OUTPUT_BUFFER = 2

'-Variables-----------------------------------------------------------
Dim PSCode

'-Sub Add-------------------------------------------------------------
Sub Add(Code)
PSCode = PSCode & Code & vbCrLf
End Sub

'-Sub Main------------------------------------------------------------
Sub Main()

'-Variables-------------------------------------------------------
Dim SapGuiAuto, application, connection, session
Dim PS

If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If

If Not IsObject(connection) Then
Set connection = application.Children(0)
End If

If Not IsObject(session) Then
Set session = connection.Children(0)
End If

If Not IsObject(PS) Then
Set PS = CreateObject("SAPIEN.ActiveXPoSH")
If PS.Init(vbFalse) <> 0 Then
MsgBox "Can't instantiate PowerShell engine"
Exit Sub
End If
If PS.IsPowerShellInstalled = vbFalse Then
Msgbox "PowerShell is not installed"
Exit Sub
End If
End If

PS.OutputMode = OUTPUT_BUFFER

'-PowerShell Begin------------------------------------------------------
PSCode = ""

Add "$sig = @"""
Add "[DllImport(""User32.dll"")]"
Add "public static extern int MessageBoxA(int hWnd, String Text, String Caption, int Type);"
Add """@;"
Add "$DLL_User32 = Add-Type PBexpMsgBox -MemberDefinition $sig -PassThru"
Add "$DLL_User32::MessageBoxA(0, ""Hello World"", ""From WinAPI"", 0);"

PS.Execute(PSCode)
'-PowerShell End--------------------------------------------------------

session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE38"
session.findById("wnd[0]/tbar[0]/btn[0]").press

End Sub

'-Main----------------------------------------------------------------
Main()

'-End-------------------------------------------------------------------

Here an example how to use VB# (Visual Basic for dotNET) code inside SAP GUI Scripting:
'-Begin-----------------------------------------------------------------
'-
'- Example how to use a dotNET VB# code via PowerShell with component
'- ActiveXPosh inside SAP GUI Scripting
'-
'-----------------------------------------------------------------------

'-Directives----------------------------------------------------------
Option Explicit

'-Constants-----------------------------------------------------------
Const OUTPUT_CONSOLE = 0
Const OUTPUT_WINDOW = 1
Const OUTPUT_BUFFER = 2

'-Variables-----------------------------------------------------------
Dim PSCode

'-Sub Add-------------------------------------------------------------
Sub Add(Code)
PSCode = PSCode & Code & vbCrLf
End Sub

'-Sub Main------------------------------------------------------------
Sub Main()

'-Variables-------------------------------------------------------
Dim SapGuiAuto, application, connection, session
Dim PS, str, outText

If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If

If Not IsObject(connection) Then
Set connection = application.Children(0)
End If

If Not IsObject(session) Then
Set session = connection.Children(0)
End If

If Not IsObject(PS) Then
Set PS = CreateObject("SAPIEN.ActiveXPoSH")
If PS.Init(vbFalse) <> 0 Then
MsgBox "Can't instantiate PowerShell engine"
Exit Sub
End If
If PS.IsPowerShellInstalled = vbFalse Then
Msgbox "PowerShell is not installed"
Exit Sub
End If
End If

PS.OutputMode = OUTPUT_BUFFER

'-PowerShell Begin------------------------------------------------------
PSCode = ""

Add "$VBCode = @"""

'-VB# Begin-------------------------------------------------------------
Add "Option Strict On"

Add "Imports System"
Add "Imports Microsoft.VisualBasic"

Add "Namespace VBCode"

Add " Public Class VB"

Add " Public Shared Function Hello1() As String"
Add " Return ""Hello World!"""
Add " End Function"

Add " Public Function Hello2(ByVal Name As String) As String"
Add " Return ""Hello "" & Name & ""!"""
Add " End Function"

Add " Public Sub Hello3(ByVal Name As String)"
Add " MsgBox(Name, MsgBoxStyle.OkOnly, ""Hello"")"
Add " End Sub"

Add " End Class"

Add "End Namespace"

'-VB# End---------------------------------------------------------------

Add """@;"

Add "Add-Type -TypeDefinition $VBCode -Language VisualBasic"
Add "$VB = new-Object VBCode.VB"

Add "[VBCode.VB]::Hello1()"
Add "$VB.Hello2(""Stefan"")"
Add "$VB.Hello3(""Stefan"")"

PS.Execute(PSCode)
'-PowerShell End--------------------------------------------------------

For Each str In PS.Output
outText = outText & str & vbCrLf
Next
MsgBox outText

session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE16"
session.findById("wnd[0]/tbar[0]/btn[0]").press

End Sub

'-Main----------------------------------------------------------------
Main()

'-End-------------------------------------------------------------------

Also you have the possibility to use all commands from PowerShell itself.

Expand your SAP GUI Scripts with the possibilities of PowerShell Script.
Labels in this area