Use SAP GUI Scripting with Native PowerShell
A few days ago I presented here the new version of Scripting Tracker. The new version supports now native PowerShell code generating.
Here an example of a PowerShell script from a logon process.
You see the SAP GUI for Windows screen and the recorded script inside Scripting Tracker. With Scripting Tracker it is seamless possible to switch in the PowerShell ISE and here you find the complete code. Now you can use all the possibilities of the ISE like single step debugging or examination of variables.
Scripting Tracker uses a COM bridge for the communication with SAP GUI Scripting, which I presented here. On this way it is possible to generate code which is nearly similar to well known VBScript.
Here an example of an activity in an tree control in VBScript:
session.findById(“wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell”).expandNode “0000000003”
session.findById(“wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell”).selectedNode = “0000000004”
Here the same example in PowerShell:
$ID = Invoke-Method $session “findById” @(“wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell”)
Invoke-Method $ID “expandNode” @(“0000000003”)
$ID = Invoke-Method $session “findById” @(“wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell”)
Set-Property $ID “selectedNode” @(“0000000004”)
The comparisation shows that code in VBScript contains one line for one activity and PowerShell two lines. The code generation creates for findById one call of Invoke-Method and with the returned ID it calls the next activity e.g. like expandNode.
In some cases the code generation creates well known code like
$ID.elementAt(3).width = 19
which should sets the width of the third column of the table control.
But the method elementAt doesn’t work in this context, it is necessary to replace it with Item like
$ID.Item(3).width = 19
which works perfectly.
Since version 5, which I presented here, offers PowerShell new language features like classes.
It has many advantages more to use PowerShell instead VBScript with SAP GUI Scripting:
- PowerShell is the actual Scripting language in Windows environments.
- It is available on all Windows systems without further installations.
- It is in further development.
- Its security mechanisms are technically mature.
- PowerShell offers an Integrated Scripting Environment (ISE) e.g. for single step debugging.
- etc.
Hi, sorry to bother you, but this is my first time working with SAP APIs. My objective is to create a PowerShell script that takes a username as input and sets the following fields within SAP GUI:
Logon Data > Validity Period
and lastly to lock the account.
If it's not too much to ask, would you mind showing me what the PowerShell code for this would look like, including how to have my PowerShell script connect to SAP to execute those commands in the first place?
Sorry if these questions seem trivial / ridiculous, I'm used to working with PowerShell cmdlets from Microsoft that are very intuitive / self-explanatory that also come with documentation and examples.
Hello Jeffrey,
welcome in the SAP Community.
Here a PowerShell script recorded with Scripting Tracker:
I call TAC SU01 and fill the fields you described and press save button.
Here a snippet to login, replace the user, password and language with yours.
Best regards
Stefan
Awesome, thanks so much!
I’m going to give an example of what I think the full script would look like, using standard PowerShell variables:
Am I putting the variables in the correct places? Does my example script cover all 3 of my objectives from my original comment?
Also, a few final questions:
Thanks again for all your help!
Hello Jeffrey,
here the total script to login to an SAP system:
You need also the include file COM.ps1 which stores some COM procedures.
The session variable is from type __COMObject and contains the dispatcher to all methods and attributes of the SAP GUI API for the session.
In my opinion is EN the correct language code.
No, SU01 is a transaction code, not a server name. You can find detailed information about your system in menu System > Status...
Yes, you need the include and a few lines of code, look at my example.
Best regards
Stefan
A little more easy :
Replacement :
With :