1

I'm trying to use Process and ProcessBuilder to execute a ps1 file through powershell. I'm struggling with the entire thing and cant even get the powershell.exe to run. Ive tried :

 // Wont Run

 // Defined specific path for powershell - trying to simply run the exe file
 ProcessBuilder pb = new ProcessBuilder("C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"); 
 Process p = pb.start();

I've look at Running Powershell from Java and Run a powershell on a remote machine using JAVA trying to implement methods from these examples but I dont understand why mine wont even run an instance of powershell.

I do eventually want to incorporate running ps1 files through here but I need to walk before I can run.

Also, I have checked Task Manager and it IS running as a process.

Could anyone point me in the right direction as to why this wont even run ?

Thanks in advance

2
  • 1
    If PowerShell is showing up in Task Manager, then it sounds like you have successfully launched it. Try doing something like this and see what happens: ProcessBuilder pb = new ProcessBuilder("C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -Command \"New-Item c:\imadethis.txt -type file\""); Commented May 2, 2014 at 15:38
  • I managed to successfully launch it by complete fluke, I opted to use process builder, and the actual path of the ps1 file needed to be in quotations, which mine wasn't. I also used 'p.getOutputStream().close();' as the problem seemed to be that powershell first reads all input from it's input stream before continuing. I wanted to run it as a background process so it wasnt essential that powershell was displayed. Tried your solution which also worked so great for anyone else looking at this ! Commented May 2, 2014 at 15:49

0

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.