Quantcast
Channel: SCN : Document List - All Communities
Viewing all articles
Browse latest Browse all 2380

Tip: Complete Launch Sequence of SAP GUI Scritping in PowerShell

$
0
0

Hello community,

 

in the context of the discussion here I developed and presented a complete launch sequence of SAP GUI Scripting in VBScript here. But for the future it seems better to use PowerShell, for reasons which I have described here. Now the equivalent in PowerShell script language:

 

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

 

  #-Includes------------------------------------------------------------

    . "$PSScriptRoot\COM.ps1"

 

  #-Sub Main------------------------------------------------------------

    Function Main() {

 

      $OpenNewConnFlag = $false

 

      if (-Not (Get-Process saplogon -ErrorAction silentlycontinue)) {

        Start-Process "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"

         do {

           Start-Sleep -Milliseconds 250

         } until(Get-Process | where {$_.mainWindowTitle -like "SAP Logon 740"})

      }

      $SapGuiAuto = Get-Object "SAPGUI"

      $application = Invoke-Method $SapGuiAuto "GetScriptingEngine"

      $connection = Get-Property $application "Children" @(0)

      if ($connection -eq $Null) {

        $OpenNewConnFlag = $true

        Invoke-Method $application "OpenConnectionByConnectionString" `

          @("/H/10.100.202.145/S/3263", $true) > $Null

        $connection = Get-Property $application "Children" @(0)

      }

      if ($application.Children().Count() -gt 1) {

        $connection = $connection[0]

      }

      $session = Get-Property $connection "Children" @(0)

      if ($connection.Children().Count() -gt 1) {

        $session = $session[0]

      }

 

      if ($OpenNewConnFlag -eq $true) {

        $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)

      }

 

      Free-Object $session

      Free-Object $connection

      Free-Object $application

      Free-Object $SapGuiAuto

 

    }

 

  #-Main----------------------------------------------------------------

    Main

 

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

 

 

Here now the facelifted COM include, which I introduced here:

 

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

 

  #-Load assembly-------------------------------------------------------

    [Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic") > $Null

 

  #-Function Create-Object----------------------------------------------

    Function Create-Object {

      param([String] $objectName)

      try {

        New-Object -ComObject $objectName

      }

      catch {

        [Void] [System.Windows.Forms.MessageBox]::Show(

          "Can't create object", "Important hint", 0)

      } 

    }

 

  #-Function Get-Object-------------------------------------------------

    Function Get-Object {

      param([String] $objectName)

      [Microsoft.VisualBasic.Interaction]::GetObject($objectName)

    }

 

  #-Sub Free-Object-----------------------------------------------------

    Function Free-Object {

      param([__ComObject] $object)

      [Void] [System.Runtime.Interopservices.Marshal]::ReleaseComObject($object)

    }

 

  #-Function Get-Property-----------------------------------------------

    function Get-Property {

      param([__ComObject] $object, [String] $propertyName)

      $objectType = [System.Type]::GetType($object)

      $objectType.InvokeMember($propertyName,

        "GetProperty", $NULL, $object, $NULL)

    }

 

  #-Sub Set-Property----------------------------------------------------

    function Set-Property {

      param([__ComObject] $object, [String] $propertyName,

        $propertyValue)

      $objectType = [System.Type]::GetType($object)

      [Void] $objectType.InvokeMember($propertyName,

        "SetProperty", $NULL, $object, $propertyValue)

    }

 

  #-Function 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 }

    }

 

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

 

 

Hint: This code is a different perspective from the solution here.

 

Enjoy it.

 

Cheers

Stefan


Viewing all articles
Browse latest Browse all 2380

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>