您可以使用几个选项。
1) Stefan Schnell 为使用本机 PowerShell 的 SAP GUI 脚本创建了一个COM 连接器。你可以开始你的研究here 和here。免责声明:我没有使用它,因为 Stefan 的网站目前已关闭,因此无法下载该组件。还有其他可用的选项。
2) Powershell + vbs:SAP GUI 7.30 具有所见即所得的功能,允许将用户操作脚本化到 .vbs 文件中。
因此,您可以在脚本录制开启时创建一个用户(这会输出带有脚本操作的 .vbs 文件),然后从 Powershell 启动此脚本。
c:\windows\system32\cscript.exe "C:\Users\xxxxxxxx\AppData\Roaming\SAP\SAP GUI\Scripts\CreateUserAndAssignRole.vbs"
如果您的 SAP 会话处于非活动状态,您首先需要启动 .exe 文件。那么您的 Powershell 部分将如下所示:
#-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"
还有登录脚本:
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
不比 SAP 丑多少,不是吗?
3) 您可以在 .NET 中自动化 SAP GUI。一个工作示例是here。
我个人在 C# 中完成了很多 (3),尽管它提供了许多有用的功能(尤其是日志记录和错误处理),但 c# 代码变得程序化并且非常繁琐。因此,我将我的解决方案迁移到 (2)。
脚本编写愉快!
康斯坦丁。