2

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 you use?

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

he already is spawning a seperate process (powershell.exe) and then parses the output. He asked for specific java interaction between powershell and java.
1

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

Comments

0

I've found Powershell Bridge (PSB). Still need to look into it though

1 Comment

Prerequisites include "Exchange Powershell, or Exchange Management Shell2", which seems outside the scope of this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.