//-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------------------------------------------------------------------