Search code examples
vbscriptsap-gui

How to Logon on SAP with SSO via VBS?


I'm trying to write a VBScript that opens a SAP session, with Single-Sign On capability.

I've already found some information about this topic, or similar ones, here and on other sites, but none of them suit my requirement..

This is what I have so far, taken from a SAP Discussion Forum:

But an error happens in the SAP GUI;

Hostame 'PRD' unknown

Line: 896
Method: NiPGetHostByName: 'PRD' not found

enter image description here

Can anybody help me?

Option Explicit
Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle
Set WSHShell = WScript.CreateObject("WScript.Shell")
If IsObject(WSHShell) Then
    SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
    SID = "PRD"
    InstanceNo = "00"
    WSHShell.Exec SAPGUIPath & "sapgui.exe " & SID & " " & _
      InstanceNo
    WinTitle = "SAP"
    While Not WSHShell.AppActivate(WinTitle)
      WScript.Sleep 250
    Wend
    Set WSHShell = Nothing
End If

Solution

  • Full disclosure, I don't use SAP so anything I say is purely educated guess work.

    After looking at the question for a little while, I may have a suggestion.

    The error is coming directly from the SAP GUI and is quite detailed, which allows us to make some assumptions;

    1. The error is from the SAP GUI so the VBScript is executing without issue.
    2. The problem is likely with the information being passed to the SAP GUI.

    Let's try breaking down the error. With this particular error we are blessed with a wealth of information (will pick out some key ones);

    • Module: We are told what file this error originates in.
    • Line: The line where the error occurs.
    • Method: The method where the error occurs a long with the error that is raised.
    • Return Code: We can use this to lookup a specific SAP GUI return code. But, only if the documention is detailed enough to contain it. It's also possible to do an internet search to lookup product specific error codes.

    Armed with all this information, two things become apparent;

    1. The Method is called NiPGetHostByName which suggests the SAP GUI is expecting to locate the Host using the Name.
    2. Judging by the code in the question and the accompanying screenshot of the connections list in SAP the PRD value appears to be the SID column not the Name.

    So, should you possibly be passing the Name instead of the SID as it appears the SAP GUI is not working from the SID and wrongly assumes PRD is the Name and hence doesn't find it?

    I would suggest trying something like;

    Option Explicit
    Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle, Name
    Set WSHShell = WScript.CreateObject("WScript.Shell")
    If IsObject(WSHShell) Then
        SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
        Name = """1. SAP ECC Production (PRD)"""
        SID = "PRD"
        InstanceNo = "00"
        WSHShell.Exec SAPGUIPath & "sapgui.exe " & Name & " " & _
          InstanceNo
        WinTitle = "SAP"
        While Not WSHShell.AppActivate(WinTitle)
          WScript.Sleep 250
        Wend
        Set WSHShell = Nothing
    End If