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------------------------------------------------------------------
With regard to the Eclipse IDE this may be an interesting aspect - but I must admit a little bit exotic.
Enjoy it.
Cheers
Stefan