Has anyone ever used powershell with SAP Front End? I am trying to build a script that creates a user in SAP GUI 7.30 and then assigns roles to the user. Does anyone ahve any reading material where I can learn this? I have looked all over the internet but can not find anywhere to start. Thanks in advance!
-
Is there a good reason for not using the officially supported API (`BAPI_USER_CREATE1`, see http://www.sapdatasheet.org/abap/func/bapi_user_create1.html and `BAPI_USER_ACTGROUPS_ASSIGN`, see http://www.sapdatasheet.org/abap/func/bapi_user_actgroups_assign.html)? – vwegert May 23 '15 at 09:02
-
Im afraid I don't quite know how that would be implemented in powershell. – Tyler S May 23 '15 at 09:04
-
You'd probably use the SAP .NET connector - does http://scn.sap.com/community/interoperability-microsoft-net/blog help? – vwegert May 23 '15 at 11:09
1 Answers
There are a couple of options you could use.
1) Stefan Schnell has created a COM connector for SAP GUI Scripting with Native PowerShell. You can start your research here and here. Disclaimer: I did not use it because Stefan’s site is currently down, and the component is thus inaccessible for download. And there are other options available.
2) Powershell + vbs: SAP GUI 7.30 has a WYSIWYG functionality that allows scripting out user actions into a .vbs file.
So you could create a user while script recording is on (this outputs a .vbs file with your scripted actions) and then launch this script from Powershell.
c:\windows\system32\cscript.exe "C:\Users\xxxxxxxx\AppData\Roaming\SAP\SAP GUI\Scripts\CreateUserAndAssignRole.vbs"
If your SAP session is inactive, you’ll first need to launch the .exe file. Then your Powershell part would look like that:
#-Set the path to the SAP GUI directory-------------------------------
$SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
#-Set the SAP system ID-----------------------------------------------
$SID = "Your system ID (a description/name from SAP Logon)"
#-Set the instance number of the SAP system---------------------------
$InstanceNo = "00"
#-Start the SAP GUI---------------------------------------------------
$SAPGUI = $SAPGUIPath + "sapgui.exe"
& $SAPGUI $SID $InstanceNo
#-Call the logon script---------------------------------
c:\windows\system32\cscript.exe "C:\Users\xxxxxxx\AppData\Roaming\SAP\SAP GUI\Scripts\login.vbs"
And the logon script:
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 IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "yourMandant"
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "yourUsername"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "yourPass"
session.findById("wnd[0]/usr/txtRSYST-LANGU").text = "yourLanguage"
session.findById("wnd[0]").sendVKey 0
not much uglier than SAP, is it not?
3) You could automate SAP GUI in .NET. A working example is here.
Personally I’ve done a lot of (3) in C# and although it provides many useful features (especially logging and error-handling), the c# code gets procedural and very cumbersome. Thus I migrate my solutions to (2).
Happy scripting!
Constantine.

- 694
- 6
- 16