Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How to Use PowerShell for Automated Login to SAP

Here is an interesting discussion how to automated logon to an SAP system via VBScript. But VBScript offers not really a function to detect the moment if the session is available. Here now an implementation how to do same with PowerShell. PowerShell offers with its interface to the Windows API an easy way to detect the availability of the new session.

Here now the code how to detect the availability of the session:

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

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

  #-Add 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\"

  #-Set the SAP system ID or the IP address-----------------------------
    $SID = "NSP"

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

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

  While ($Win32::FindWindow("SAP_FRONTEND_SESSION", "SAP") -eq 0) {
    Start-Sleep -Milliseconds 250
  }

  Write-Host "Here now your script..."

#-End-------------------------------------------------------------------

 

Here now the code how to logon automatically:

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

  #-Includes------------------------------------------------------------
    ."COM.ps1"

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

  #-Add 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\"

  #-Set the SAP system ID-----------------------------------------------
    $SID = "localhost"

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

  #-Start the 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 GUI session--------------------------------------------
    $SapGuiAuto = [Microsoft.VisualBasic.Interaction]::GetObject("SAPGUI")
    $application = Invoke-Method $SapGuiAuto "GetScriptingEngine"
    $connection = Get-Property $application "Children" @(0)
    $session = Get-Property $connection "Children" @(0)

    $ID = Invoke-Method $session "findById" @("wnd[0]/usr/txtRSYST-MANDT")
    Set-Property $ID "Text" @("001")
    $ID = Invoke-Method $session "findById" @("wnd[0]/usr/txtRSYST-BNAME")
    Set-Property $ID "Text" @("BCUSER")
    $ID = Invoke-Method $session "findById" @("wnd[0]/usr/pwdRSYST-BCODE")
    Set-Property $ID "Text" @("minisap")
    $ID = Invoke-Method $session "findById" @("wnd[0]/usr/txtRSYST-LANGU")
    Set-Property $ID "Text" @("EN")

    $ID = Invoke-Method $session "findById" @("wnd[0]")
    Invoke-Method $ID "sendVKey" @(0)

#-End-------------------------------------------------------------------

You can find the COM include file here.

You can find another variant here.

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Stefan,

      I am getting following error when I run the automated login script. I have placed the COM.ps1 in same directory as SAP_Auto_login.ps1 script. Please help as I am new to Powershell scripting.

       

      C:\windows\system32> C:\Basis\PowerShell Scripting\SAP_Auto_Login.ps1
      . : The term 'COM.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
      spelling of the name, or if a path was included, verify that the path is correct and try again.
      At C:\Basis\PowerShell Scripting\SAP_Auto_Login.ps1:4 char:6
      + ."COM.ps1"
      + ~~~~~~~~~
      + CategoryInfo : ObjectNotFound: (COM.ps1:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException

      Author's profile photo Former Member
      Former Member

      Hello,

      I am getting following error when trying to run the above Powershell script for automated login. The COM.ps1 file is in same directory.

       

      S C:\windows\system32> C:\Basis\PowerShell Scripting\SAP_Auto_Login.ps1
      . : The term 'COM.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
      spelling of the name, or if a path was included, verify that the path is correct and try again.
      At C:\Basis\PowerShell Scripting\SAP_Auto_Login.ps1:4 char:6
      + ."COM.ps1"
      + ~~~~~~~~~
      + CategoryInfo : ObjectNotFound: (COM.ps1:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException

       

      Thanks,

      Dnyandev

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello,

      try this to solve your problem:

      #-Begin-----------------------------------------------------------------
      
        #-Includes------------------------------------------------------------
          ."$PSScriptRoot\COM.ps1"
      
        $hWSH = Create-Object "Wscript.Shell"
      
        $hWSH.Popup("testmessage", 2, "goto", 1)
        
        Free-Object $hWSH
          
      #-End-------------------------------------------------------------------

      On this way it is possible to run the script from every path.

      Cheers
      Stefan

      Author's profile photo Anurag Das
      Anurag Das

      Hello Stefan,

       

      Apologies for such a late reply. Understand that my comment is coming almost 1 year later.

      There is a line from the code- Detecting the Availability of the session.

       

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

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

       

      So in our scenario, we have a nfs path from where the SID information is there.  \\vf0013.gha.kfplc.com\shared\Group\NWBC_Config\XXXX.xml

      So can you kindly suggest how I can modify it please ?

      Do I have to add any lines extra or can alter it.

       

      Thanks and Regards
      Anurag Das

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Anurag,

      thanks for your reply.

      I am not really sure that I understand you correctly. It is not necessary with this approach to modify the path of the XML file.

      If you need the content of the XML file, you can try this to get it:

      #-Begin-----------------------------------------------------------------
      
        #-Sub Main------------------------------------------------------------
        Function Main() {
      
          [XML]$NFS = Get-Content "\\YourServer\YourPath$\saplogon\SAPUILandscape.xml";
          $StringWriter = [System.IO.StringWriter]::new();
          $XmlWriter = [System.Xml.XmlTextWriter]::new($StringWriter);
          $XmlWriter.Formatting = [System.xml.formatting]::Indented;
          $NFS.WriteContentTo($XmlWriter);
          $SAPUILandscape = $StringWriter.ToString();
      
          Write-Host $SAPUILandscape;
      
        }
      
        #-Main----------------------------------------------------------------
        Main;
      
      #-End-------------------------------------------------------------------

      Let us know your results.

      Cheers
      Stefan

      Author's profile photo Putra Ahmad
      Putra Ahmad

      Hi stefan,

      im new in powershell. I didn’t understand the COM.ps1 part. What is it? Do i have to create it or is it already exists? How can i create it and where should i save it? Your answer would mean a world to me thank you!

      Author's profile photo Putra Ahmad
      Putra Ahmad

      Hi stefan,

      im new in powershell. I didn’t understand the COM.ps1 part. What is it? Do i have to create it or is it already exists? How can i create it and where should i save it? Your answer would mean a world to me thank you!