on ‎2021 Sep 03 7:46 AM
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
Request clarification before answering.
Hello Stefan,
in meantime, I researched more on-site and on the Internet and found all steps to successfully perform your code example. Now I'm able to execute transaction "BAMBI" and receive/read an error. So the last step for me will be to test this approach as a background task when nobody logged on. Anyway, thank you for your help.
Best regards,
Vladimir
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Stefan,
sorry for the delay in response, but I was on vacation. Once again thanks for clarifying the needs for my case.
Best regards,
Vladimir
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Stefan,
thanks for the code, I tried but get an error when tried to create a $session with the following code:
$SapGuiAuto = [microsoft.visualbasic.Interaction]::GetObject("SAPGUI")
$application = Invoke-Method $SapGuiAuto "GetScriptingEngine"
$connection = Get-Property $application "Children" @(0)
$session = Get-Property $connection "Children" @(0)
It failed on the first line with the error message:
"Cannot create ActiveX component"
I research community and Internet articles and found that Visual Basic assembly needs to be loaded but even after I add this additional line of code still same error message.
Do I need to install additional SAP components to be able to create a session?
If it matters I'm using SAP NetWeaver (SAP GUI), Release 760 Final Release, File Version 7600.1.11.1160 Build 2065455 Patch Level 11.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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?
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";
}
While ($Win32::FindWindow("SAP_FRONTEND_SESSION", "Data Browser: Initial Screen") -eq 0) {
Start-Sleep -Milliseconds 250;
}$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";
}
}$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
| User | Count |
|---|---|
| 15 | |
| 9 | |
| 6 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.