Skip to Content

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.

5 Comments
You must be Logged on to comment or reply to a post.
  • 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

  • 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

  • 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

  • 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

    • 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