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
0 Kudos


To use SAP GUI Scripting inside PowerShell you can find here more information about this possibility. The basis of this approach is Visual Basic Script (VBS) resp. the Microsoft Script Control. On this way it is very easy to use the existing scripts furthermore. Here I have compared the use of .NET invoke methods with the use of MSScriptControl. This was a step forward to .NET, but the disadvantage is that you can't use the advantages of the Integrated Script Environment (ISE) like code completion.

To walk around this shortcoming I searched for a method to cast the ___COMObject types to correct data types in a namespace of a .NET assembly. I found the method CreateWrapperOfType. All I have to do is to build with the type library importer (TLBIMP) a .NET assembly from the SAPFEWSE.OCX library - thats all. If you don't habe the possibility to build it at your own, look in your NWBC or SapDtsCOMFramework directory, here you find the assembly Interop.SAPFEWSELib.dll, it is also possible to use this assembly.

Hint: The assembly of the NWBC use the namespace Interop.SAPFEWSELib, the assembly of the Dynamic Test Script framework use only SAPFEWSELib. I find that very awkward. If you use the assembly of the NWBC you must modfy the types below and add Interop. in front of the type names.

A code line with a ___COMObject casting to another type of a SAP GUI Scripting object looks like this:

[SAPFEWSELib.GuiApplication]$Application =
    [System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($Application,
    [SAPFEWSELib.GuiApplicationClass])

Here I cast the variable $Application from the type System.___COMObject to GuiApplication - our base friend of the SAP GUI Scripting object hierarchy. I think it is easy to understand what happens.

Here now the complete code:
#-Begin-----------------------------------------------------------------

[Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")

[Reflection.Assembly]::LoadFile("SAPFEWSELib.dll")

$SapGuiAuto = [Microsoft.VisualBasic.Interaction]::GetObject("SAPGUI")
If ($SapGuiAuto -eq $Null) {
Break
}

$Application = $SapGuiAuto.GetType().InvokeMember("GetScriptingEngine",
[System.Reflection.Bindingflags]::InvokeMethod,
$null, $SapGuiAuto, $null, $null, $null, $null)
[SAPFEWSELib.GuiApplication]$Application =
[System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($Application,
[SAPFEWSELib.GuiApplicationClass])
If ($Application -eq $Null) {
Break
}

$Connection = $Application.Children.Item(0)
[SAPFEWSELib.GuiConnectionClass]$Connection =
[System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($Connection,
[SAPFEWSELib.GuiConnectionClass])
If ($Connection -eq $Null) {
Break
}

$Session = $Connection.Children.Item(0)
[SAPFEWSELib.GuiSession]$Session =
[System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($Session,
[SAPFEWSELib.GuiSessionClass])
If ($Session -eq $Null) {
Break
}

$Mandt = $Session.FindById("wnd[0]/usr/txtRSYST-MANDT")
[SAPFEWSELib.GuiTextField]$Mandt =
[System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($Mandt,
[SAPFEWSELib.GuiTextFieldClass])
$BName = $Session.FindById("wnd[0]/usr/txtRSYST-BNAME")
[SAPFEWSELib.GuiTextField]$BName =
[System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($BName,
[SAPFEWSELib.GuiTextFieldClass])
$Langu = $Session.FindById("wnd[0]/usr/txtRSYST-LANGU")
[SAPFEWSELib.GuiTextField]$Langu =
[System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($Langu,
[SAPFEWSELib.GuiTextFieldClass])
$BCode = $Session.FindById("wnd[0]/usr/pwdRSYST-BCODE")
[SAPFEWSELib.GuiPasswordField]$BCode =
[System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($BCode,
[SAPFEWSELib.GuiPasswordFieldClass])

$Mandt.Text = "001"
$BName.Text = "BCUSER"
$Langu.Text = "EN"
$Bcode.Text = "minisap"

$MainWin = $Session.FindById("wnd[0]")
[SAPFEWSELib.GuiMainWindow]$MainWin =
[System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($MainWin,
[SAPFEWSELib.GuiMainWindowClass])
$MainWin.SendVKey(0)

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

 

This example is equivalent to the examples in the links above. Sure it is more complex as VBScript, but it offers all adavantages of the integration in the .NET environment.



This possibility opens now the gate wide to use SAP GUI Scripting seamlessly in PowerShell and in any other language ot the .NET environment.

Hint after: As far as I know uses the NetWeaver Business Client (NWBC), version 4 and 5, as well as the Dynamic Test Scripts (DTS) framework the same method. SAP dts is a test framework which enables automation of testing SAP applications. Look at US patent US 20120023485 A1 for more information about DTS.

Enjoy it.

Labels in this area