Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor
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-----------------------------------------------------------------
"-
"- Preparation:
"-
"- Define a table type with the name Z_TAB_STRING and
"- the data type string
"-
"- Disable the security settings in the SAP logon
"-
"-----------------------------------------------------------------------
CLASS ZACTIVEXPOSHV3 DEFINITION
PUBLIC
CREATE PUBLIC.

public section.

interfaces IF_HTTP_EXTENSION .

constants MC_OUTPUTCONSOLE type I value 0 ##NO_TEXT.
constants MC_OUTPUTWINDOW type I value 1 ##NO_TEXT.
constants MC_OUTPUTBUFFER type I value 2 ##NO_TEXT.
constants MC_TRUE type I value 1 ##NO_TEXT.
constants MC_FALSE type I value 0 ##NO_TEXT.

"! Loads the ActiveXPoshV3 library
"!
"! @parameter rv_result | 1 for success, otherwise 0
methods LoadLib
returning
value(RV_RESULT) type I.

"! 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 iv_expression | PowerShell command
"!
"! @parameter rv_result | Result of the command
methods Eval
importing
value(IV_EXPRESSION) type STRING
returning
value(RV_RESULT) type I.

"! Executes a PowerShell command or script
"! Output is directed according to the OutputMode property.
"! Variable assignments persist between calls.
"!
"! @parameter iv_command | PowerShell command or script
methods Execute
importing
value(IV_COMMAND) type STRING.

"! Evaluates a PowerShell expression and returns its value as string
"!
"! @parameter iv_expression | PowerShell command
"!
"! @parameter rv_result | Value as string
methods GetValue
importing
value(IV_EXPRESSION) type STRING
returning
value(RV_RESULT) type STRING.

"! Initial call to instantiate a PowerShell engine
"! Required for any of the methods of this object to succeed.
"!
"! @parameter iv_load_profiles | Determines if your PowerShell profiles, if they exist, are executed
"!
"! @parameter rv_result | Returns 0 if successful, otherwise &lt;&gt; 0
methods Init
importing
value(IV_LOAD_PROFILES) type I
returning
value(RV_RESULT) type I.

"! Checks if PowerShell is installed
"!
"! @parameter rv_result | Returns 1 if PowerShell is installed, otherwise 0
methods GetIsPowerShellInstalled
returning
value(RV_RESULT) type I.

"! Gets the current output mode
"!
"! @parameter rv_result | 0 = OutputConsole, 1 = OutputWindow, 2 = OutputBuffer
methods GetOutputMode
returning
value(RV_RESULT) type I.

"! Sets the current output mode
"!
"! @parameter iv_mode | 0 = OutputConsole, 1 = OutputWindow, 2 = OutputBuffer
methods SetOutputMode
importing
value(IV_MODE) type I.

"! Delivers the content of the output buffer as a single string
"!
"! @parameter rv_result | Output buffer as string
methods GetOutputString
returning
value(RV_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 rv_result | Width in characters
methods GetOutputWidth
returning
value(RV_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 iv_width | Width in characters
methods SetOutputWidth
importing
value(IV_WIDTH) type I.

"! Reads an include as string
"!
"! @parameter iv_incl_name | Name of the include
"!
"! @parameter rv_str_incl | Include as string
methods ReadInclAsString
importing
value(IV_INCL_NAME) type SOBJ_NAME
returning
value(RV_STR_INCL) type STRING.

"! Loads a file from the MIME repository
"! and copies it to the SAP GUI work directory
"!
"! @parameter iv_uri_file | URI path of the file in the MIME repository
methods LoadFileFromMime
importing
!IV_URI_FILE type CSEQUENCE.

"! Converts a string to a string table
"!
"! @parameter iv_string | String e.g. from GetOutputString
"!
"! @parameter rv_stringtable | String table
methods StringToTable
importing
value(IV_OUTPUTSTRING) type STRING
returning
value(RV_STRINGTABLE) type Z_TAB_STRING.

"! Converts a string table to a string
"!
"! @parameter it_string | String table
"!
"! @parameter rv_string | String
methods TableToString
importing
value(IT_STRING) type Z_TAB_STRING
returning
value(RV_STRING) type STRING.

PRIVATE SECTION.

METHODS IsActiveX
EXPORTING ev_result TYPE i.

DATA olib TYPE ole2_object.

ENDCLASS.



CLASS ZACTIVEXPOSHV3 IMPLEMENTATION.


METHOD if_http_extension~handle_request."-----------------------------

DATA:
lv_verb TYPE string,
lv_action TYPE string,
lv_input TYPE progname,
lv_data TYPE string,
lt_incl TYPE prognames
.

AUTHORITY-CHECK OBJECT 'S_DEVELOP'
ID 'ACTVT' FIELD '03'.
IF sy-subrc = 0.
CALL METHOD server->response->set_status(
code = 403
reason = 'Not Authorized'
).
RETURN.
ENDIF.

lv_verb = server->request->get_header_field( name = '~request_method' ).
lv_action = server->request->get_header_field( name = 'action' ).
lv_input = server->request->get_header_field( name = 'input' ).

IF lv_verb = 'GET' OR lv_action IS INITIAL OR lv_input IS INITIAL.
CALL METHOD server->response->set_status(
code = 400
reason = 'Bad Request'
).
RETURN.
ENDIF.

TRANSLATE lv_action TO UPPER CASE.
TRANSLATE lv_input TO UPPER CASE.

CASE lv_action.

WHEN 'READ_INCL_AS_STRING'.
lv_data = me->ReadInclAsString( iv_incl_name = lv_input ).

WHEN 'GET_INCL'.
SELECT name FROM trdir INTO TABLE lt_incl
WHERE name LIKE lv_input AND subc = 'I' AND appl = space.
lv_data = /ui2/cl_json=>serialize( data = lt_incl ).

WHEN OTHERS.
CALL METHOD server->response->set_status(
code = 400
reason = 'Bad Request'
).
RETURN.

ENDCASE.

IF lv_data IS NOT INITIAL.
CALL METHOD server->response->set_cdata( data = lv_data ).
CALL METHOD server->response->set_status(
code = 200
reason = 'Ok'
).
ELSE.
CALL METHOD server->response->set_status(
code = 204
reason = 'No Content'
).
ENDIF.

ENDMETHOD.


METHOD ClearOutput."--------------------------------------------------
CALL METHOD OF olib 'ClearOutput'.
ENDMETHOD.


METHOD Eval."---------------------------------------------------------
CALL METHOD OF olib 'Eval' = rv_result
EXPORTING #1 = iv_expression.
ENDMETHOD.


METHOD Execute."------------------------------------------------------
CALL METHOD OF olib 'Execute'
EXPORTING
#1 = iv_command.
ENDMETHOD.


METHOD Flush."--------------------------------------------------------
CALL METHOD cl_gui_cfw=>flush.
ENDMETHOD.


METHOD FreeLib."------------------------------------------------------
FREE OBJECT olib.
ENDMETHOD.


METHOD GetIsPowerShellInstalled."-------------------------------------
GET PROPERTY OF olib 'IsPowerShellInstalled' = rv_result.
ENDMETHOD.


METHOD GetOutputmode."------------------------------------------------
GET PROPERTY OF olib 'OutputMode' = rv_result.
ENDMETHOD.


METHOD GetOutputString."----------------------------------------------
GET PROPERTY OF olib 'OutputString' = rv_result.
ENDMETHOD.


METHOD GetOutputWidth."-----------------------------------------------
GET PROPERTY OF olib 'OutputWidth' = rv_result.
ENDMETHOD.


METHOD GetValue."-----------------------------------------------------
CALL METHOD OF olib 'GetValue' = rv_result
EXPORTING #1 = iv_expression.
ENDMETHOD.


METHOD Init."---------------------------------------------------------
CALL METHOD OF olib 'Init' = rv_result
EXPORTING #1 = iv_load_profiles.
ENDMETHOD.


METHOD IsActiveX."----------------------------------------------------

DATA hasactivex(32) TYPE c.

ev_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'.
ev_result = 1.

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 iv_uri_file 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 = iv_uri_file
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.


METHOD LoadLib."------------------------------------------------------

DATA rc TYPE i VALUE 0.

rv_result = 0.
CALL METHOD me->isactivex IMPORTING ev_result = rc.
CHECK rc = 1.
CREATE OBJECT olib 'SAPIEN.ActiveXPoSHV3'.
CHECK sy-subrc = 0 AND olib-handle > 0 AND olib-type = 'OLE2'.
rv_result = 1.

ENDMETHOD.


METHOD ReadInclAsString."---------------------------------------------

DATA:
lt_trdir TYPE trdir,
lt_incl TYPE TABLE OF string,
lv_inclline TYPE string,
lv_retincl TYPE string
.

"TRDIR is a view of table REPOSRC
SELECT SINGLE * FROM trdir INTO lt_trdir
WHERE name = iv_incl_name AND subc = 'I' AND appl = space.
CHECK sy-subrc = 0.
READ REPORT iv_incl_name 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.
CLEAR lv_inclline.
ENDLOOP.
rv_str_incl = lv_retincl.

ENDMETHOD.


METHOD SetOutputMode."------------------------------------------------
SET PROPERTY OF olib 'OutputMode' = iv_mode.
ENDMETHOD.


METHOD SetOutputWidth."-----------------------------------------------
SET PROPERTY OF olib 'OutputWidth' = iv_width.
ENDMETHOD.


METHOD TableToString."------------------------------------------------

DATA:
lv_string Type String,
lv_retString Type String
.

Loop At IT_STRING Into lv_string.
lv_retString = lv_retString && lv_string &&
cl_abap_char_utilities=>cr_lf.
Clear lv_string.
EndLoop.
rv_string = lv_retString.

ENDMETHOD.


METHOD StringToTable."------------------------------------------------

FIELD-SYMBOLS:
<lv_string> TYPE string
.

SPLIT iv_outputstring
AT cl_abap_char_utilities=>cr_lf
INTO TABLE rv_stringtable.
"-Delete empty lines------------------------------------------------
LOOP AT rv_stringtable ASSIGNING <lv_string>.
CHECK <lv_string> IS INITIAL.
DELETE rv_stringtable.
ENDLOOP.

ENDMETHOD.


ENDCLASS.
"-End-------------------------------------------------------------------

Here an example how to use this class:
"-Begin-----------------------------------------------------------------
REPORT zPSTest.

"-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( ) = lo_PS->mc_True.
lo_PS->setOutputMode( iv_Mode = lo_PS->mc_OutputBuffer ).
CHECK lo_PS->getIsPowershellInstalled( ) = lo_PS->mc_True AND
lo_PS->Init( iv_Load_Profiles = lo_PS->mc_False ) = 0.
lo_PS->ClearOutput( ).
lo_PS->Execute( iv_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-------------------------------------------------------------------
21 Comments