0

I'm trying to write a script that automatically logs into an SAP system via SAP GUI. I want the SAP GUI fields to be filled automatically with the script below.

Can you tell me, if I'm on the right way? How can I let it work?

#-Begin-----------------------------------------------------------------

 #-Includes------------------------------------------------------------
    ."$PSScriptRoot\COM.ps1"

  $hWSH = Create-Object "Wscript.Shell"

  $hWSH.Popup("testmessage", 2, "goto", 1)
  
  Free-Object $hWSH

  #-Signatures----------------------------------------------------------
    $Sig = @'
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
'@

  # FindWindow function---------------------------------------------
    $Win32 = Add-Type -Namespace Win32 -Name Funcs -MemberDefinition $Sig -PassThru

  #-Set the path to the SAP GUI directory-------------------------------
    $SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"

  # SAP system ID-----------------------------------------------
    $SID = "test.lan"

  #instance number of the SAP system---------------------------
    $InstanceNo = "10"

  #-Start  SAP GUI---------------------------------------------------
    $SAPGUI = $SAPGUIPath + "sapgui.exe"
    & $SAPGUI $SID $InstanceNo

  #-Wait until the session is available---------------------------------
    While ($Win32::FindWindow("SAP_FRONTEND_SESSION", "SAP") -eq 0) {
      Start-Sleep -Milliseconds 250
    }

  #-Logon to SAP --------------------------------------------
    
    
   
    $user="test"

    
    $SAPGUI.document.getElementById("Benutzer").value= "$user" 
    $SAPGUI.document.getElementById("loginform").submit()


```
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
du7ri
  • 67
  • 1
  • 10
  • 1
    Does this answer your question? [How do I login through SAP using command line](https://stackoverflow.com/questions/16809492/how-do-i-login-through-sap-using-command-line) – iRon Mar 29 '21 at 07:26
  • Your code seems non-sense because you are running SAP GUI for Windows but you try to fill a HTML form, which are not related at all. – Sandra Rossi Mar 29 '21 at 11:57

1 Answers1

0

You could use SAP shortcut:

cd "c:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
sapshcut -guiparm="[hostname] [installation number]" -system=[system id] -client=[client] -user=[user name] -pw=[password]

Replace the parameters (square brackets) with the appropriate values. You will see a confirmation popup when you execute this command for the first time (for a specific set of parameters), but you can disable the dialog for future automatic logins. You may omit parameter -guiparm="[hostname] [installation number]" if the system ID is created in SAPlogon.

Thomas Erdösi
  • 534
  • 3
  • 15