Hello community,
here is an interesting discussion how to automated logon to an SAP system via VBScript. But VBScript offers not really a function to detect the moment if the session is available. Here now an implementation how to do same with PowerShell. PowerShell offers with its interface to the Windows API an easy way to detect the availability of the new session.
Here now the code how to detect the availability of the session:
#-Begin-----------------------------------------------------------------
$Sig = @'
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
'@
#-Add FindWindow function---------------------------------------------
$Win32 = Add-Type -Namespace Win32 -Name Funcs -MemberDefinition $Sig -PassThru
#-Set the path to the SAP GUI directory-------------------------------
$SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
#-Set the SAP system ID or the IP address-----------------------------
$SID = "NSP"
#-Set the instance number of the SAP system---------------------------
$InstanceNo = "00"
#-Starts the SAP GUI--------------------------------------------------
$SAPGUI = $SAPGUIPath + "sapgui.exe"
& $SAPGUI $SID $InstanceNo
While ($Win32::FindWindow("SAP_FRONTEND_SESSION", "SAP") -eq 0) {
Start-Sleep -Milliseconds 250
}
Write-Host "Here now your script..."
#-End-------------------------------------------------------------------
Here now the code how to logon automatically:
#-Begin-----------------------------------------------------------------
#-Includes------------------------------------------------------------
."COM.ps1"
#-Signatures----------------------------------------------------------
$Sig = @'
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
'@
#-Add FindWindow function---------------------------------------------
$Win32 = Add-Type -Namespace Win32 -Name Funcs -MemberDefinition $Sig -PassThru
#-Set the path to the SAP GUI directory-------------------------------
$SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
#-Set the SAP system ID-----------------------------------------------
$SID = "localhost"
#-Set the instance number of the SAP system---------------------------
$InstanceNo = "00"
#-Start the SAP GUI---------------------------------------------------
$SAPGUI = $SAPGUIPath + "sapgui.exe"
& $SAPGUI $SID $InstanceNo
#-Wait until the session is available---------------------------------
While ($Win32::FindWindow("SAP_FRONTEND_SESSION", "SAP") -eq 0) {
Start-Sleep -Milliseconds 250
}
#-Logon to SAP GUI session--------------------------------------------
$SapGuiAuto = Get-Object("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-------------------------------------------------------------------
You can find the COM include file here.
Enjoy it.
Cheers
Stefan