ABAP 代码:
Program zPSTest.
Constants:
OUTPUT_CONSOLE Type i Value 0,
OUTPUT_WINDOW Type i Value 1,
OUTPUT_BUFFER Type i Value 2.
Data:
PS Type OLE2_OBJECT,
Result Type i,
strResult Type String,
tabResult Type Table Of String,
cmd Type String
.
Create Object PS 'SAPIEN.ActiveXPoSHV3'.
Check sy-subrc = 0 And PS-Handle <> 0 Or PS-Type = 'OLE2'.
WRITE:/ 'object created ok'.
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.
运行结果:
单步调试: