Quantcast
Channel: SCN : Document List - Scripting Languages
Viewing latest article 4
Browse Latest Browse All 100

How To Use PowerShell Script Inside Java

$
0
0

Hello community,

 

I wrote here about the using of SAP GUI Scripting inside Java. Also I wrote here, here, here and here about the using of PowerShell inside ABAP. Here now a combination of both. I use JaCoB (Java COM Bridge) with SAPIEN Technologies ActiveX PowerShell library. On this way it is possible to use PowerShell seamless and easily inside Java.

 

//-Begin----------------------------------------------------------------

//-

//- Example program how to use PowerShell inside Java

//-

//- Author: Stefan Schnell

//-

//----------------------------------------------------------------------

 

  //-Package------------------------------------------------------------

    package de.stschnell;

 

  //-Import-------------------------------------------------------------

    import com.jacob.activeX.ActiveXComponent;

    import com.jacob.com.ComThread;

    import com.jacob.com.Variant;

 

  //-Class--------------------------------------------------------------

    public class GetPSVersion {

 

    //-Constants--------------------------------------------------------

      public static final int OUTPUT_CONSOLE = 0;

      public static final int OUTPUT_WINDOW = 1;

      public static final int OUTPUT_BUFFER = 2;

 

    //-Main-------------------------------------------------------------

        public static void main(String[] args) {

 

        ComThread.InitSTA();

 

        ActiveXComponent PS = new ActiveXComponent("SAPIEN.ActiveXPoSHV3");

        try {

 

              if (PS.invoke("Init", false).changeType(Variant.VariantInt).getInt() != 0) {

            throw new RuntimeException();

          }

 

          if (PS.getProperty("IsPowerShellInstalled").changeType(Variant.VariantInt).getInt() == 0) {

            throw new RuntimeException();

          }

 

          PS.setProperty("OutputMode", OUTPUT_BUFFER);

          PS.invoke("ClearOutput");

    

          String PSCommand = "Get-Host;$PSVersionTable.PSVersion";

    

          PS.invoke("Execute", PSCommand);

          String Output = PS.getProperty("OutputString").toString();

 

          System.out.println(Output);

  

        }

 

        catch (Exception e) {

          System.out.println("An exception has occurred");

        }

 

        finally {

          ComThread.Release();

          System.exit(0);

        }

 

      }         

      

    }

 

//-End------------------------------------------------------------------

 

002.jpg

 

With regard to the Eclipse IDE this may be an interesting aspect - but I must admit a little bit exotic.

 

Enjoy it.

 

Cheers

Stefan


Viewing latest article 4
Browse Latest Browse All 100

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>