免密自动登陆SAPGui

下面介绍两种免密自动登陆的方式

使用bat(批处理)文件自动登陆

将如下代码通过文本编辑器保存为bat文件

start sapshcut.exe -user=ITL_WEIXP -pw=Qq12345678 -language=ZH -SYSTEM=S4D -CLIENT=300 -sysname=xxxdev -maxgui

修改相应的参数

以上代码的意思是启动sapshcut.exe程序,并使用如下参数.

  • -user:用户
  • -pw:密码
  • -language:语言
  • -SYSTEM:系统标识
  • -CLIENT:集团
  • -sysname:连接名称.连接名称无法识别中文

双击文件登陆

如果对应的连接的client已经打开,则不会打开新的gui窗口.
参考:https://blogs.sap.com/2016/11/02/sap-automatic-logon-using-bat-file/

关于sapshcut.exe的其他参数含义可以通过:run->sapshcut.exe /? 查看.

使用VBS加SAP GUI scripting(脚本录制和回放)自动登陆

  • 使用前要确保相应的系统已经开启脚本录制和回放功能.
  • 使用代码RZ11查看或者修改参数sapgui/user_scripting的值为true.

    *在Gui的选项中开启脚本录制和回放.

将下边代码复制到文本编辑器中并保存为vbs格式.

'------------------------------------------------------------------
'- VBScript to start SAP Logon if process doesn't exists and connect
'- to an SAP system. The SAP Logon process is detected via WMI
'- (Windows Management Instrumentarium), also the termination.
'-----------------------------------------------------------------------
  '-Directives----------------------------------------------------------
    Option Explicit
  '-Variables-----------------------------------------------------------
    Dim SAPLogon, SAPLogonTitle
    SAPLogon = "saplogon.exe" : SAPLogonTitle = "SAP Logon 770"
    'SAPLogon = "saplgpad.exe" : SAPLogonTitle = "SAP Logon Pad 740"
    Dim SysDescr, SysIP
    '-----------------------------------------------------------------
    'Sysdescr系统描述,SysIP连接地址.两选一.使用ip的时候如果用用router,
    '需要将router加在地址前边.用/H/开始
    '-----------------------------------------------------------------
    'SysDescr = "xxxdev" 
    'SysIP = "/H/218.77.58.137"
     SysIP = "/H/HY.XX.NET/H/192.168.18.12"
  '-Function FindProcess------------------------------------------------
    Function FindProcess(ProcessName)
      '-Variables-------------------------------------------------------
        Dim WMIServ, Processes, Process
      FindProcess = False
      Set WMIServ = GetObject("winmgmts:{impersonationLevel=" & _
       "impersonate}!\\.\root\cimv2") 
      Set Processes = WMIServ.ExecQuery("Select * from Win32_Process " & _
        "Where Name = '" & ProcessName & "'")
      For Each Process In Processes
        FindProcess = True
        Exit Function
      Next
    End Function
  '-Sub TerminateProcess------------------------------------------------
    Sub TerminateProcess(ProcessName)
      '-Variables-------------------------------------------------------
      Dim WMIServ, Processes, Process
      Set WMIServ = GetObject("winmgmts:{impersonationLevel=" & _
        "impersonate}!\\.\root\cimv2") 
      Set Processes = WMIServ.ExecQuery("Select * from Win32_Process " & _
        "Where Name = '" & ProcessName & "'")
      For Each Process In Processes
        Process.Terminate()
        Exit Sub
      Next
    End Sub
  '-Function GetSAPGUIObject--------------------------------------------
  '- This function starts the SAP Logon if it is necessary and delivers
  '- its object
  '---------------------------------------------------------------------
    Function GetSAPGUIObject()
      '-Variables-------------------------------------------------------
        Dim WshShell, Exec
      If FindProcess(SAPLogon) Then
        Set GetSAPGUIObject = GetObject("SAPGUI")
      Else
        Set WshShell = CreateObject("WScript.Shell")
        Set Exec = WshShell.Exec(_
          "c:\Program Files (x86)\SAP\FrontEnd\SAPgui\" & SAPLogon)
        Do While Not WshShell.AppActivate(SAPLogonTitle) 
          WScript.Sleep 500 
        Loop
        Set GetSAPGUIObject = GetObject("SAPGUI")
      End If
    End Function
  '-Sub Main------------------------------------------------------------
    Sub Main()
      '-Variables-------------------------------------------------------
      Dim SapGuiAuto, Application, Connection, Session
      Set SapGuiAuto = GetSAPGUIObject()
      If Not IsObject(SapGuiAuto) Then
        Exit Sub
      End If
      Set Application = SapGuiAuto.GetScriptingEngine
      If Not IsObject(Application) Then
        Set SapGuiAuto = Nothing
        Exit Sub
      End If
      '-----------------------------------------------------------------
      '- 在下边决定用系统描述或者ip地址
      '-----------------------------------------------------------------
      'Set Connection = Application.OpenConnection(SysDescr, True)
      Set Connection = Application.OpenConnectionByConnectionString( SysIP, True)
      If Not IsObject(Connection) Then
        Set Application = Nothing
        Set SapGuiAuto = Nothing
        Exit Sub
      End If
      Set Session = Connection.Children(0)
      If Not IsObject(Session) Then
        Set Connection = Nothing
        Set Application = Nothing
        Set SapGuiAuto = Nothing
        Exit Sub
      End If
      MyScript(Session)
      'Set Session = Nothing
      'Set Connection = Nothing
      'Set Application = Nothing
      'Set SapGuiAuto = Nothing
      'TerminateProcess SAPLogon
    End Sub
  '-Sub MyScript--------------------------------------------------------
    Sub MyScript(Session)
      '-----------------------------------------------------------------
      '- 在下边更改用户,密码,语言client.
      '-----------------------------------------------------------------
      session.findById("wnd[0]").maximize
      session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "800"
      session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "weixp"
      session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "qwer1234"
      session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "ZH"
      session.findById("wnd[0]").sendVKey 0
    End Sub
  '-Main----------------------------------------------------------------
  Main

修改代码中的系统描述,系统ip,用户,密码等

需要注意的是系统描述和用户名不支持中文.

双击文件登陆系统

修改文件图标

由于bat,vbs文件的图标不太好看.可以通过创建对应文件的快捷方式,修改快捷方式的图标来进行调整

修改快捷方式的图标

sap图片地址:%ProgramFiles(x86)%\SAP\FrontEnd\SAPgui\saplogon.exe
也就是在浏览中选择saplogon.exe文件.

两种方式的比较

  • bat方式比较简单
  • bat只能使用系统描述登陆
  • vbs方式相对复杂一些
  • vbs方式可以使用ip登陆
  • vbs方式可以添加GUI scripting(脚本录制和回放)代码,实现自动处理.
posted @ 2021-10-19 15:27  女鬼的朋友  阅读(4928)  评论(0编辑  收藏  举报