cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Execute command from PowerShell

4,817

I have no experience with SAP, but I should create a PowerShell script. I've taken a look at the SAP community and found some examples like https://blogs.sap.com/2015/09/26/how-to-use-powershell-for-automated-login-to-sap/ which explain how to log on to the SAP GUI. In addition to that, I should be able to execute a command from CLI. The command is:

.\sapshcut -system=EHD -client=102 -user=myUserName -PW=myPass -maxgui -command="va03"

Is it possible to execute this command after the logon thru the PowerShell script?

Thanks

View Entire Topic
Stefan-Schnell
Active Contributor
0 Likes

prusacv

Hello Vladimir
welcome in the SAP Community.

You have the choice which method you want to use to start the SAP GUI for Windows. On the one hand you can use the approach with sapshcut in PowerShell ...

... or on the other hand you can use the post to which you have linked above. It is not necessary to use both.

Here an equivalent PowerShell script:

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

$Win32 = Add-Type -Namespace Win32 -Name Funcs -MemberDefinition $Sig -PassThru

& 'C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe' -system=UZT -client=099 -user=user -PW=secret -language=E -maxgui -command=SE16
    
While ($Win32::FindWindow("SAP_FRONTEND_SESSION", "Data Browser: Initial Screen") -eq 0) {
  Start-Sleep -Milliseconds 250
}

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

Best regards
Stefan

Hello Stefan,

first of all, thank you for your answer. Regarding SAP GUI, we are able to do it from CMD and PowerShell but our focus is "validation". I see now that I didn't point what was the core issue which we want to do so I will try to explain in more detail. I'm working on a small part of a bigger disaster recovery test process. We want to start SAP GUI, execute commands and do "validation". Under validation, we would like to have any proof that the script/command used for the automation test is working fine (e.g. SAP GUI started and successfully executed the command).

So my first idea was to use sapshcut.exe, start the SAP GUI and execute the command "va03". Because the print screen validation is not an option for us we were thinking that using PowerShell in interaction with SAP GUI is a good start point.

So we would like to have some proof that command like:

& 'C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe' -system=UZT -client=099 -user=user -PW=secret -language=E -maxgui -command=SE16

is executed successfully. Visually we know that the SAP GUI command is executed successfully but we need to prove it to the business department. When I mention "proof of success" we were thinking about executing the SAP GUI thru script and retrieve data from SAP. Based on your experience does our approach sound good or maybe there is another better approach (using/searching log file for data, or similar).

Best Regards

Vladimir

Stefan-Schnell
Active Contributor
0 Likes

prusacv

Hello Vladimir,
interesting requirement. Your way to prove that the SAP GUI for Windows starts successfully is in my opinion a very good approach. What might be some other steps to increase the certainty of this kind of statement?

  • Check if saplogon process is running.
Try {
  $saplogon = Get-Process -Name saplogon -ErrorAction SilentlyContinue;
  If($saplogon) {
    Write-Host "saplogon process is running";
  } Else {
    Write-Host "saplogon process is not running";
  }
} Catch {
  Write-Host "Error at saplogon process check";
}
  • When this little loop comes to an end you can be sure that a SAP GUI for Windows session is open.
While ($Win32::FindWindow("SAP_FRONTEND_SESSION", "Data Browser: Initial Screen") -eq 0) {
  Start-Sleep -Milliseconds 250;
}
  • Here the same loop with a timeout.
$Timeout = New-TimeSpan -Seconds 60;
$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew();
While ($Win32::FindWindow("SAP_FRONTEND_SESSION", "Data Browser: Initial Screen") -eq 0 -and $Stopwatch.Elapsed -le $Timeout) {
  Start-Sleep -Milliseconds 250;
  If($Stopwatch.Elapsed -ge $Timeout) {
    Write-Host "Timeout";
  }
}
  • Send a non-existent transaction code, e.g. like /nBAMBI, to the session and read the error message of the status bar. This would even give you the prove that the SAP GUI for Windows works.
$ID = Invoke-Method -object $session -methodName "findById" -methodParameter @("wnd[0]/tbar[0]/okcd");
Set-Property -object $ID -propertyName "text" -propertyValue @("/nBAMBI");
$ID = Invoke-Method -object $session -methodName "findById" -methodParameter @("wnd[0]");
Invoke-Method -object $ID -methodName "sendVKey" -methodParameter @(0);
$ID = Invoke-Method -object $session -methodName "findById" -methodParameter @("wnd[0]/sbar/pane[0]");
$StatusBarText = Get-Property -object $ID -propertyName "text";
If($StatusBarText -eq "Transaction BAMBI does not exist") {
  Write-Host "SAP GUI for Windows is up and running";
}

These approaches are a visual equivalent and you can say with high certainty that the functionality works. I have used similar approaches at the time in test automation.

Best regards
Stefan