Thursday, 5 January 2017

How to use Windows PowerShell Script inside ABAP

Windows PowerShell is a mix of command-line shell and scripting language. You find more Information about PowerShell  here.

With the free COM library ActiveXPosh.dll from SAPIEN you can also use PowerShell inside ABAP.

Here an example how to get all services and its state.
"-Begin-----------------------------------------------------------------
  Program zPSTest.

    "-TypePools---------------------------------------------------------
      Type-Pools OLE2 .

    "-Constants--------------------------------------------------------
      Constants OUTPUT_CONSOLE Type i Value 0.
      Constants OUTPUT_WINDOW Type i Value 1.
      Constants OUTPUT_BUFFER Type i Value 2.

    "-Variables---------------------------------------------------------
      Data PS Type OLE2_OBJECT.
      Data Result Type i.
      Data strResult Type String.
      Data tabResult Type Table Of String.
      Data cmd Type String.

    "-Main--------------------------------------------------------------
      Create Object PS 'SAPIEN.ActiveXPoSHV3'.
      If sy-subrc  0 Or PS-Handle = 0 Or PS-Type  'OLE2'.
        Exit.
      EndIf.

      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.

How to use Windows PowerShell Script inside ABAP

No comments:

Post a Comment