Skip to Content

PowerShell is a fantastic scripting language. It offers great possibilities to handle different kinds of situations in the context of SAP GUI Scripting easily. The SAP GUI uses at different places native OS dialogs, independently from the configuration in the SAP Logon. One of this places is the GOS, you can find more information here respectively here. To handle this kind of requirement you can use the great job mechanism of PowerShell on the one hand in combination with the fantastic AutoItX library on the other hand.

The following script opens TAC SGOSTEST and attaches a group of files from a directory via GOS. It defines a job which handles the native import dialog box.

It waits until the dialog is available, It activates it and send the key strokes Alt+n, the filename and Alt+f to close the dialog. If a security requester opens, it will be allow the activity.

This is a parallel activity beside the script, which waits until the dialog is closed. Now the script checks the status bar and if the document was created successfully it will print a message.

Here the script:

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

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

  #-Includes AutoItX----------------------------------------------------
  [Void] [System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\AutoIt\AutoItX3.Assembly.dll")

  #-Sub Main------------------------------------------------------------
  Function Main() {

    $SapGuiAuto = Get-Object "SAPGUI"
    If ($SapGuiAuto -IsNot [System.__ComObject]) {
      Return
    }

    $Application = Invoke-Method $SapGuiAuto "GetScriptingEngine"
    If ($Application -IsNot [System.__ComObject]) {
      Return
    }

    $Connection = Get-Property $Application "Children" @(0)
    If ($Connection -eq $Null) {
      Return
    }

    $Session = Get-Property $Connection "Children" @(0)
    If ($Session -eq $Null) {
      Return
    }

    $ID = Invoke-Method $session "findById" @("wnd[0]/tbar[0]/okcd")
    Set-Property $ID "text" @("/nsgostest")
    $ID = Invoke-Method $session "findById" @("wnd[0]")
    Invoke-Method $ID "sendVKey" @(0)
    $ID = Invoke-Method $session "findById" @("wnd[0]/tbar[1]/btn[8]")
    Invoke-Method $ID "press"

    $Path = "C:\Dummy"
    $FilesToAttach = $Path + "\Files2Attach"

    $Files = Get-ChildItem $FilesToAttach

    ForEach($File In $Files) {

      $ID = Invoke-Method $session "findById" @("wnd[0]/titl/shellcont/shell")
      Invoke-Method $ID "pressContextButton" @("%GOS_TOOLBOX")

      [AutoIt.AutoItX]::WinActivate("Test Report") > $Null

      $Job = Start-Job -ArgumentList $File.FullName, $PSScriptRoot, `
        ([environment]::CurrentDirectory="$PSScriptRoot") -ScriptBlock {

        Param($FileName, $PSScriptRoot)

        [Void] [System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\AutoIt\AutoItX3.Assembly.dll")  

        If ([AutoIt.AutoItX]::WinWait("Import file") -ne 1) {
          Return
        }
        [AutoIt.AutoItX]::WinActivate("Import file")

        #-These shortcuts are dependent from the OS language!-----------
        [AutoIt.AutoItX]::Send("!n$FileName!f")

        If ([AutoIt.AutoItX]::WinWait("SAP GUI Security", "", 5) -ne 1) {
          Return
        }
        If ([AutoIt.AutoItX]::WinExists("SAP GUI Security") -eq 1) {
          [AutoIt.AutoItX]::Send("!a")
        }
 
      }

      $ID = Invoke-Method $session "findById" @("wnd[0]/titl/shellcont/shell")
      Invoke-Method $ID "selectContextMenuItem" @("%GOS_PCATTA_CREA")

      #-Now the native modal dialog box is open and served by the job---

      $Job | Remove-Job

      $ID = Invoke-Method $Session "findById" @("wnd[0]/sbar/pane[0]")
      $StatusBar = Get-Property $ID "text"
      If ($StatusBar -eq "Document created") {
        Write-Host $File.FullName "created successfully" -ForegroundColor Green
      } Else {
        Write-Host $File.FullName "not created" -ForegroundColor Red
      }

    }

  }

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

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

 

This example show how easy it is to use parallel processes with PowerShell in the context of SAP GUI Scripting. On this way it is also easy to handle native modal OS dialogs.

Enjoy it.

Cheers
Stefan

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply