Invoking powershell from Java
Anyone know of a good library to invoke powershell scripts from within Java? I'm currently spawning a seperate process (powershell.exe) and then parse the output, but it would really be nice if I can leverage Powershell's 'power' by getting objects back from a powershell call.
Edit:
Otherwise, anyone else doing such interop? What method do开发者_StackOverflow社区 you use?
After posting, I realized that this is not the answer to your question, but for anybody else who wants to call PowerShell from Java and is fine spinning up a separate process, here's how.
Assuming your Powershell script is in script.ps1, you can run it in a separate process using:
String cmd = "powershell C:\\path\\to\\your\\script\\script.ps1"
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
process.getOutputStream().close();
The downside to crossplatform tools is that you only have a common subset of tools for each platform - This is not possible through Java. You could do some JNI or use the method you have already tried
I've found Powershell Bridge (PSB). Still need to look into it though
精彩评论