使用 SAP ABAP 编程语言调用 Windows10 操作系统的 Powershell 命名并打印出执行结果

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.

运行结果:

在这里插入图片描述

单步调试:

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/i042416/article/details/126264905