// Execute command
JIComServer comStub = new JIComServer(JIProgId.valueOf("WScript.Shell"),
bonafides.getHost(), session);
IJIComObject unknown = comStub.createInstance();
IJIComObject comobject = unknown.queryInterface(IJIDispatch.IID);
IJIDispatch shell = (IJIDispatch) JIObjectFactory.narrowObject(comobject);
Object[] scriptArgs = new Object[]{
new JIString("%comspec% /c " + cmd)
};
// ref: http://stackoverflow.com/questions/6781340/how-to-call-a-remote-bat-file-using-jinterop
JIVariant results[] = shell.callMethodA("Exec", scriptArgs);
final IJIDispatch wbemObjectSet_dispatch =
(IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
JIVariant stdOutJIVariant = wbemObjectSet_dispatch.get("StdOut");
IJIDispatch stdOut =
(IJIDispatch) JIObjectFactory.narrowObject(stdOutJIVariant.getObjectAsComObject());
// Read all from stdOut
StringBuilder sb = new StringBuilder();
while (!((JIVariant) stdOut.get("AtEndOfStream")).getObjectAsBoolean()) {
sb.append(stdOut.callMethodA("ReadAll").getObjectAsString().getString());
}
return sb.toString();
}
catch (Exception e) {