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

How to use SAP GUI Scripting Inside Windows PowerShell

Two and a half year ago I wrote about the possiblity how to use SAP GUI Scripting with Windows PowerShell. But this solution uses Microsoft Script Control engine and it is not clear how the future will bring. So I develop a solution which works with PowerShell without the Microsoft Script Control engine:

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

  #-Get-Property--------------------------------------------------------
    function Get-Property {
      param([__ComObject] $object, [String] $propertyName)
      $objectType = [System.Type]::GetType($object)
      $objectType.InvokeMember($propertyName,
        "GetProperty", $NULL, $object, $NULL)
    }

  #-Set-Property--------------------------------------------------------
    function Set-Property {
      param([__ComObject] $object, [String] $propertyName,
        $propertyValue)
      $objectType = [System.Type]::GetType($object)
      [Void] $objectType.InvokeMember($propertyName,
        "SetProperty", $NULL, $object, $propertyValue)
    }

  #-Invoke-Method-------------------------------------------------------
    function Invoke-Method {
      param([__ComObject] $object, [String] $methodName,
        $methodParameters)
      $objectType = [System.Type]::GetType($object)
      $output = $objectType.InvokeMember($methodName,
        "InvokeMethod", $NULL, $object, $methodParameters)
      if ( $output ) { $output }
    }

  #-Main----------------------------------------------------------------
    $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-------------------------------------------------------------------

 

Hint: The wrapper functions are from Bill Stewart – thanks for that.

Here the equivalent code but with Microsoft Script Control engine:

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

  $VB = New-Object -COMObject MSScriptControl.ScriptControl

$Cmd = @"
Set SapGuiAuto = GetObject(`"SAPGUI`")`n
Set application = SapGuiAuto.GetScriptingEngine`n
Set connection = application.Children(0)`n
Set session = connection.Children(0)`n
session.findById(`"wnd[0]/usr/txtRSYST-MANDT`").text = `"001`"`n
session.findById(`"wnd[0]/usr/txtRSYST-BNAME`").text = `"BCUSER`"`n
session.findById(`"wnd[0]/usr/pwdRSYST-BCODE`").text = `"minisap`"`n
session.findById(`"wnd[0]/usr/txtRSYST-LANGU`").text = `"EN`"`n
session.findById(`"wnd[0]`").sendVKey 0`n
"@

  $VB.Language = "VBScript"
  $VB.AllowUI = $TRUE
  $VB.ExecuteStatement($Cmd)

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

As you can see looks the code a little bit unusual, but on this way you use only a VB.net function and PowerShell natively.

2016/03/08
Minor changes, explicit type detection for PowerShell 5 compatibility.

Assigned Tags

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

      Does't work