cancel
Showing results for 
Search instead for 
Did you mean: 

Using VBScript for Automated Login to SAP

Former Member
0 Kudos

All,

I'm attempting to create a VBScript that will login to my companies SAP and basically run a report that outputs an HTM file for use with some other reports.  Generating the actual report goes off without a hitch, but I would like to set up a chron job (recurring windows task) that would completely automate this process.

I am primarily running into a problem with the automated login to SAP.  I have code that can open the SAP executable and I have code that can input the username and password into the login screen but I am at a loss on how to select and execute the target server (it is auto selected at the beginning but sending WshShell.SendKeys "{ENTER}" does not seem to work).  Any help would be greatly appreciated!

My code can reviewed on github with comments:

warroom/SAPLogin.vbs at master · PaulStreet/warroom · GitHub

For convenience I have also pasted the code (VBScript below).

'The below section will create an SAP session.

set WshShell = CreateObject("WScript.Shell")

Set proc = WshShell.Exec("C:\Program Files\SAP\FrontEnd\SAPgui\saplogon.exe")

            Do While proc.Status = 0

            WScript.Sleep 100

      Loop

   Set SapGui = GetObject("SAPGUI")

Set Appl = SapGui.GetScriptingEngine

''Deprecated alternate code, wait for 6 seconds

'Dim dteWait

'dteWait = DateAdd("s", 6, Now())

'Do Until (Now() > dteWait)

'Loop

'Wait for 5 seconds then press enter.

WScript.Sleep 5000

WshShell.SendKeys "{ENTER}"

''This commented section of code doesn't seem to work for me.

'Set Connection = Appl.Openconnection("Test SAP", True)

'Set session = Connection.Children(0)

'session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "USERNAME"

'session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "PASSWORD"

'session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "E"

'session.findById("wnd[0]").sendVKey 0

'The below code is what I can record once I have gotten to the SAP login for the target server.

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-BNAME").text = "USERNAME"

session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "PASSWORD"

session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus

session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10

session.findById("wnd[0]").sendVKey 0

Attached is a screenshot of the screen I cannot get past (SAP executable with server select); ie the screen I cannot get past (there are other servers that I have censored out).

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor

Hello Paul,

welcome in the scripting forum.

You can use this VBScript to open a connection to an SAP system.

'-Begin-----------------------------------------------------------------


  '-Directives----------------------------------------------------------

    Option Explicit


  '-Variables-----------------------------------------------------------

    Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle


  '-Main----------------------------------------------------------------

    Set WSHShell = WScript.CreateObject("WScript.Shell")

    If IsObject(WSHShell) Then


      '-Set the path to the SAP GUI directory---------------------------

        SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"


      '-Set the SAP system ID-------------------------------------------

        SID = "NSP"


      '-Set the instance number of the SAP system-----------------------

        InstanceNo = "00"


      '-Starts the SAP GUI----------------------------------------------

        WSHShell.Exec SAPGUIPath & "sapgui.exe " & SID & " " & _

          InstanceNo

      '-Set the title of the SAP GUI window here------------------------

        WinTitle = "SAP"

      While Not WSHShell.AppActivate(WinTitle)

        WScript.Sleep 250

      Wend

      Set WSHShell = Nothing

    End If

    MsgBox "Here now your script..."

'-End-------------------------------------------------------------------

So it is not necessary to work with the SAP Logon or SAP Logon Pad directly. But if it is required you find on my blog an older solution from 19.02.2009 with AutoIt.

Let us know your results.

Cheers

Stefan

Former Member
0 Kudos

Stefan!

Thanks a ton, works perfectly.  If you ever are in Greenville, SC (USA) let me know and I will buy you a beer.  I added some variables to be declared at the beginning due to the Option Explicit added my script to the end and it went off without a hitch.  End results are available at: https://github.com/PaulStreet/sapscripts/blob/master/SAPLogin.vbs

And I have placed the script below for preview.

REM The following script was written to log into the SAP server automatically.

REM To view historical information and credit for this script please see

REM the following thread on the SAP Community Network:

REM http://scn.sap.com/thread/3763970

REM This script was last updated by Paul Street on 7/1/15

REM Directives

    Option Explicit

  REM Variables!  Must declare before using because of Option Explicit

    Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle, SapGuiAuto, application, connection, session

  REM Main

    Set WSHShell = WScript.CreateObject("WScript.Shell")

    If IsObject(WSHShell) Then

      REM Set the path to the SAP GUI directory

        SAPGUIPath = "C:\Program Files\SAP\FrontEnd\SAPgui\"

      REM Set the SAP system ID

        SID = "NBP"

      REM Set the instance number of the SAP system

        InstanceNo = "00"

      REM Starts the SAP GUI

        WSHShell.Exec SAPGUIPath & "SAPgui.exe " & SID & " " & _

          InstanceNo

      REM Set the title of the SAP GUI window here

        WinTitle = "SAP"

      While Not WSHShell.AppActivate(WinTitle)

        WScript.Sleep 250

      Wend

      Set WSHShell = Nothing

    End If

  REM Remove this if you need to test the above script and want a message box at the end launching the login screen.

    REM MsgBox "Here now your 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-BNAME").text = "USERNAME"

session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "PASSWORD"

session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus

session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10

session.findById("wnd[0]").sendVKey 0

stefan_schnell
Active Contributor
0 Kudos

Hello Paul,

thanks for your reply, your source and your beer invitation.

Perhaps in the Blue Ridge or Connolly's.

Cheers

Stefan

0 Kudos

Hello,

Thank you for this script.

I'm using it with my own variables for SAPGUIPath, SID and InstanceNo. I get this error while connecting:

---------------------------

MessageBox 6E74E3

---------------------------

hostname 'B0Y' unknown

Check your application server name

Do you want to see the detailed error description?

---------------------------

OK   Cancel

---------------------------

---------------------------

SAP GUI for Windows 730

---------------------------

hostname 'B0Y' unknown

Time        Sat Jul 25 16:17:08 2015

Component    NI (network interface)

Release        730

Version        40

Module        ninti.c

Line        897

Method        NiPGetHostByName: 'B0Y' not found

Return Code    -2

System Call    getaddrinfo

Counter        1

---------------------------

OK 

---------------------------

Any idea how to resolve this error?

Kind regards,

Sebastjan

stefan_schnell
Active Contributor
0 Kudos

Hello Sebastjan,

welcome in Scripting Language forum.

You can try instead of the SID, in your case B0Y, the IP address of the target SAP system like this:

'-Set the SAP system ID-------------------------------------------

  SID = "10.100.202.145"

'-Set the instance number of the SAP system-----------------------

  InstanceNo = "63"

'-Starts the SAP GUI----------------------------------------------

  WSHShell.Exec SAPGUIPath & "sapgui.exe " & SID & " " & _

    InstanceNo

In my case it works also.

Let us know your results.

Best regards

Stefan

0 Kudos

Hello Stefan,

thank you for the fast reply.

Unfortunatelly I get the same error.

I haven't specified my connection parameters in my post above. I connect via Astaro VPN and use message server and SAP router to connect to the system.

Kind regards,

Sebastjan

0 Kudos

Hello Stefan,

I managed to get this to work by providing the SID as:

<router><message server>

I've also found a lot information in the SAPNote 103019

Kind regards,

Sebastjan

stefan_schnell
Active Contributor
0 Kudos

Hello Sebastjan,

thank you very much for this interesting Information.

Best regards

Stefan

Former Member
0 Kudos

Hi, I have question about this. Whenever i log on to the system for the first time, it works fine but when i try to log on to the second system while still connected to the first one, the procedure stops at the login screen. For instance i first log in to BPD then to BAP and want to be and stay connected to both.

What would i have to modify to the code for this to work via this script? Thanks Lucien

stefan_schnell
Active Contributor
0 Kudos

Hello Lucien,

welcome in the Scripting Languages forum.

It seems a problem of the code line WSHShell.AppActivate(WinTitle) - which is not very reliable.

Is it possible for you to use PowerShell, like I describe here, or other libraries which offers these function, like AutoItX?

Cheers

Stefan

0 Kudos

Hello Stefan,

I have a similar issue. And I thought it's the same as Lucien has described but now I'm not so sure. Whenever I run a macro to do some stuff in a system I have to implement the system logoff as well in order to something in another system. If I already have a system open, then my macro for the next automated procedure just opens the next system and logs on. After that the procedure stops.

I've diagnosed this as the connection issue by using the sap scripting tracker tool ().

The first open system has the ID of

/app/con[0]/ses[0]/wnd[0]

and the second one has the Id of

/app/con[1]/ses[0]/wnd[0]

Notice the con element incremented by one.

In my code I'm only calling the IDs by for example "session.findById("wnd[0]/usr/txtRSYST-MANDT")" leaving out the /app/con[0]/ part. Hence The script doesn't know which one to activate and perform the actions. Am I right?

Can this be bypassed by either explicitly calling also the connection element or by implementing some sort of connection count array to track all opened connections?

Kind regards,

Sebastjan

stefan_schnell
Active Contributor
0 Kudos

Hello Sebastjan,

yeah, you are right

You can find a function to get the connection number of the next logon screen of a given SID here.

Thanks for your analysis, I was on the wrong track.

Cheers

Stefan

Former Member
0 Kudos

Hi, this was also my idea since the first log on to any SAP system worked and it was just the second time where the script failed. It seems logical that it has to do with some connection numbering/order.

Ideally the script would always check for any active connection and simply choose the next one. Any help on this would be great.

I will further investigate as well, Cheers guys! Lucien

Former Member
0 Kudos

What did I not define using the coding above as reference? I get an error that says 'Object variable or With block variable not set on this line:

    Dim WSHShell As Object

    Dim SID As String

    Dim InstanceNo As String

    Dim WinTitle As String

    Dim SapGuiPath As String

    Dim WScript As Object

Set WSHShell = WScript.CreateObject("WScript.Shell")

Answers (1)

Answers (1)

0 Kudos

Hi st.schnell,

'-Begin-----------------------------------------------------------------

'-Directives----------------------------------------------------------

Option Explicit

'-Variables-----------------------------------------------------------

Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle

'-Main----------------------------------------------------------------

Set WSHShell = WScript.CreateObject("WScript.Shell")

If IsObject(WSHShell) Then

'-Set the path to the SAP GUI directory---------------------------

SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"

'-Set the SAP system ID-------------------------------------------

SID = "NSP"

'-Set the instance number of the SAP system-----------------------

InstanceNo = "00"

'-Starts the SAP GUI----------------------------------------------

WSHShell.Exec SAPGUIPath & "sapgui.exe " & SID & " " & _

InstanceNo

'-Set the title of the SAP GUI window here------------------------

WinTitle = "SAP"

While Not WSHShell.AppActivate(WinTitle)

WScript.Sleep 250

Wend

Set WSHShell = Nothing

End If

MsgBox "Here now your script..."

'-End-----------------------------------------------

Can you please continue this script until the logging in using a username and a password?

Since I already have an opened session I will really appreciate it if maybe you can add where the script will loop or cycle through active connections or sessions so that it will be able to log in successfully in the second SID.

Thanks in advance.

Regards,

Randolp