Windows PowerShell is a mix of command-line shell and scripting language. You find more Information about PowerShell here and here. With the free COM library ActiveXPosh.dll from SAPIEN you can also use PowerShell inside ABAP. In this blog I will show how to do that.

Here an example how to get all services and its state.

"-Begin----------------------------------------------------------------- Program zPSTest. "-Constants----------------------------------------------------------- Constants: OUTPUT_CONSOLE Type i Value 0, OUTPUT_WINDOW Type i Value 1, OUTPUT_BUFFER Type i Value 2 . "-Variables----------------------------------------------------------- Data: PS Type OLE2_OBJECT, Result Type i, strResult Type String, tabResult Type Table Of String, cmd Type String . "-Main---------------------------------------------------------------- Create Object PS 'SAPIEN.ActiveXPoSHV3'. Check sy-subrc = 0 And PS-Handle  0 Or PS-Type = 'OLE2'. Call Method Of PS 'Init' = Result Exporting #1 = 0. If Result 0. Free Object PS. Exit. EndIf. Call Method Of PS 'IsPowerShellInstalled' = Result. If Result = 0. Free Object PS. Exit. EndIf. Set Property Of PS 'OutputMode' = OUTPUT_BUFFER. cmd = `Get-WmiObject -class Win32_Service | `. cmd = cmd && `Format-Table -property Name,State`. Call Method Of PS 'Execute' Exporting #1 = cmd. Call Method Of PS 'OutputString' = strResult. Split strResult At cl_abap_char_utilities=>cr_lf Into Table tabResult. Loop At tabResult Into strResult. Write: / strResult. EndLoop. Free Object PS. "-End-------------------------------------------------------------------

You can use with PowerShell all dotNET and Windows standard libraries. On this way you can extend your possibilities.


Here a class to use PowerShell in ABAP easily:

"-Begin----------------------------------------------------------------- Class zActiveXPoshV3 Definition Public Create Public. Public Section. "! Loads the ActiveXPoshV3 library "! "! @parameter Result | 1 for success, otherwise 0 Methods LoadLib Returning Value(Result) Type Integer. "! Frees the ActiveXPoshV3 library Methods FreeLib. "! Executes stored OLE activities Methods Flush. "! Clears the internal output buffer "! when the OutputMode property is set to mc_OutputBuffer Methods ClearOutput. "! Evaluates a PowerShell expression "! If the expression returns an object this function returns -1, "! otherwise 0. Output, if any, is not captured or redirected. "! "! @parameter Expression | PowerShell command "! @parameter Result | Result of the command Methods Eval Importing Expression Type String Returning Value(Result) Type i. "! Executes a PowerShell command or script "! Output is directed according to the OutputMode property. "! Variable assignments persist between calls. "! "! @parameter Command | PowerShell command or script Methods Execute Importing Command Type String. "! Evaluates a PowerShell expression "! "! @parameter Expression | PowerShell command "! @parameter Result | Value as string Methods GetValue Importing Expression Type String Returning Value(Result) Type String. "! Initial call to instantiate a PowerShell engine "! Required for any of the methods of this object to succeed. "! "! @parameter LoadProfiles | Determines if your PowerShell profiles, if they exist, are executed "! @parameter Result | Returns 0 if successful, otherwise  0 Methods Init Importing LoadProfiles Type i Returning Value(Result) Type i. "! Checks if PowerShell is installed "! "! @parameter Result | Returns 1 if PowerShell is installed, otherwise 0 Methods getIsPowerShellInstalled Returning Value(Result) Type i. "! Gets the current output mode "! "! @parameter Result | 0 = OutputConsole, 1 = OutputWindow, 2 = OutputBuffer Methods getOutputMode Returning Value(Result) Type i. "! Sets the current output mode "! "! @parameter Mode | 0 = OutputConsole, 1 = OutputWindow, 2 = OutputBuffer Methods setOutputMode Importing Mode Type i. "! Delivers the content of the output buffer as a single string "! "! @parameter Result | Output buffer as string Methods getOutputString Returning Value(Result) Type String. "! Gets the desired output width in characters "! PowerShell output often gets truncated, wrapped or adjusted "! corresponding to the width of a console window. Since there "! is not necessarily a console window available, the default "! is set to 80 characters width. "! "! @parameter Result | Width in characters Methods getOutputWidth Returning Value(Result) Type i. "! Sets the desired output width in characters "! PowerShell output often gets truncated, wrapped or adjusted "! corresponding to the width of a console window. Since there "! is not necessarily a console window available, the default "! is set to 80 characters width. "! "! @parameter Width | Width in characters Methods setOutputWidth Importing Width Type i. "! Reads an include as string "! "! @parameter InclName | Name of the include "! @parameter strIncl | Include as string Methods ReadInclAsString Importing InclName Type SOBJ_NAME Returning Value(strIncl) Type String. "! Loads a file from the MIME repository "! and copies it to the SAP GUI work directory "! "! @parameter URIFile | URI path of the file in the MIME repository Methods LoadFileFromMIME Importing URIFile Type csequence. Constants mc_OutputConsole Type i Value 0. Constants mc_OutputWindow Type i Value 1. Constants mc_OutputBuffer Type i Value 2. Private Section. Methods IsActiveX Exporting Result Type i. Data oLib Type OLE2_OBJECT. EndClass. Class zActiveXPoshV3 Implementation. Method LoadLib."---------------------------------------------------- Data rc Type i Value 0. Result = 0. Call Method Me->IsActiveX Importing Result = rc. Check rc = 1. Create Object oLib 'SAPIEN.ActiveXPoSHV3'. Check sy-subrc = 0 And oLib-Handle  0 And oLib-Type = 'OLE2'. Result = 1. EndMethod. Method FreeLib."---------------------------------------------------- Free Object oLib. EndMethod. Method Flush."------------------------------------------------------ Call Method CL_GUI_CFW=>Flush. EndMethod. Method IsActiveX."-------------------------------------------------- Data HasActiveX(32) Type c. Result = 0. Call Function 'GUI_HAS_OBJECTS' Exporting OBJECT_MODEL = 'ACTX' Importing Return = HasActiveX Exceptions INVALID_OBJECT_MODEL = 1 Others = 2. Check sy-subrc = 0 And HasActiveX = 'X'. Result = 1. EndMethod. Method ClearOutput."------------------------------------------------ Call Method Of oLib 'ClearOutput'. EndMethod. Method Eval."------------------------------------------------------- Call Method Of oLib 'Eval' = Result Exporting #1 = Expression. EndMethod. Method Execute."---------------------------------------------------- Call Method Of oLib 'Execute' Exporting #1 = Command. EndMethod. Method GetValue."--------------------------------------------------- Call Method Of oLib 'GetValue' = Result Exporting #1 = Expression. EndMethod. Method Init."------------------------------------------------------- Call Method Of oLib 'Init' = Result Exporting #1 = LoadProfiles. EndMethod. Method getIsPowerShellInstalled."----------------------------------- Get Property Of oLib 'IsPowerShellInstalled' = Result. EndMethod. Method getOutputMode."---------------------------------------------- Get Property Of oLib 'OutputMode' = Result. EndMethod. Method setOutputMode."---------------------------------------------- Set Property Of oLib 'OutputMode' = Mode. EndMethod. Method getOutputString."-------------------------------------------- Get Property Of oLib 'OutputString' = Result. EndMethod. Method getOutputWidth."--------------------------------------------- Get Property Of oLib 'OutputWidth' = Result. EndMethod. Method setOutputWidth."--------------------------------------------- Set Property Of oLib 'OutputWidth' = Width. EndMethod. Method ReadInclAsString."------------------------------------------- Data: lt_TADIR Type TADIR, lt_Incl Type Table Of string, lv_InclLine Type string, lv_retIncl Type String . Select Single * From TADIR Into lt_TADIR Where OBJ_NAME = InclName. Check sy-subrc = 0. Read Report InclName Into lt_Incl. Check sy-subrc = 0. Loop At lt_Incl Into lv_InclLine. lv_retIncl = lv_retIncl && lv_InclLine && cl_abap_char_utilities=>cr_lf. lv_InclLine = ''. EndLoop. strIncl = lv_retIncl. EndMethod. Method LoadFileFromMIME."------------------------------------------- Data: lr_mr_api Type Ref To if_mr_api, lv_FileData Type xstring, lv_WorkDir Type string, lv_FilePath Type string, lt_FileName Type Standard Table Of string, lv_FileName Type string, lt_dTab Type Table Of x255, lv_len Type i, lv_FileExists Type abap_bool . Split URIFile At '/' Into Table lt_FileName. Read Table lt_FileName Index lines( lt_FileName ) Into lv_FileName. Call Method cl_gui_frontend_services=>get_sapgui_workdir Changing sapworkdir = lv_workdir Exceptions Others = 1. lv_filepath = lv_workdir && '' && lv_FileName. Call Method cl_gui_frontend_services=>file_exist Exporting file = lv_filepath Receiving result = lv_FileExists Exceptions Others = 1. Check lv_FileExists = abap_false. If lr_mr_api Is Initial. lr_mr_api = cl_mime_repository_api=>if_mr_api~get_api( ). EndIf. Call Method lr_mr_api->get Exporting i_url = urifile Importing e_content = lv_filedata Exceptions Others = 1. Check sy-subrc = 0. Call Function 'SCMS_XSTRING_TO_BINARY' Exporting buffer = lv_FileData Importing output_length = lv_len Tables binary_tab = lt_dtab. Call Function 'GUI_DOWNLOAD' Exporting bin_filesize = lv_len filename = lv_filepath filetype = 'BIN' Tables data_tab = lt_dtab Exceptions Others = 1. EndMethod. EndClass. "-End-------------------------------------------------------------------

Here an example how to use this class:

"-Begin----------------------------------------------------------------- REPORT zPSTest. "-Constants----------------------------------------------------------- CONSTANTS True TYPE i VALUE 1. CONSTANTS False TYPE i VALUE 0. "-Variables----------------------------------------------------------- DATA: lo_PS TYPE REF TO zactivexposhv3, lv_Result TYPE string, lt_Result TYPE TABLE OF string . "-Main---------------------------------------------------------------- CREATE object lo_PS. CHECK lo_PS->LoadLib( ) = True. CHECK lo_PS->getIsPowershellInstalled( ) = True. CHECK lo_PS->Init( LoadProfiles = False ) = 0. lo_PS->setOutputMode( Mode = lo_PS->mc_OutputBuffer ). lo_PS->ClearOutput( ). lo_PS->Execute( Command = 'Write-Host "Hello World from PowerShell";' ). lv_Result = lo_PS->getOutputString( ). SPLIT lv_Result AT cl_abap_char_utilities=>cr_lf INTO TABLE lt_Result. LOOP AT lt_Result Into lv_Result. WRITE: / lv_Result. ENDLOOP. lo_PS->FreeLib( ). "-End-------------------------------------------------------------------



New NetWeaver Information at

Very Helpfull

User Rating: Be the first one !