2

I am able to run the below powershell command through Powershell itself,

invoke-command -ComputerName "compName" -filepath "c:\script.ps1" -credential "admin"

but when I try running that through Java, I get an error. Sounds like "Invoke-command" is not recognized as a program to be run though Java. If this is the case, is there any other solution?

Process p = new ProcessBuilder()
                .inheritIO()
                .command("invoke-command", "-computername", "compName",
                        "-filepath", "C:\\script.ps1").start();

The error,

Cannot run program "invoke-command": CreateProcess error=2, The system cannot find the file specified

P.S. the error is not related to the filePath provided rather it is around the invoke-command itself.

Thank you.

1
  • invoke-command is not a Windows command, it's a PowerShell command, so you have to run it through the PowerShell command-line, i.e. powershell.exe -Command your-command-here. Commented Oct 31, 2016 at 22:23

1 Answer 1

4

As you wrote invoke-command is a Powershell command, thus you have to call Powershell tu run the command like so:

Process p = new ProcessBuilder()
                .inheritIO()
                .command("powershell", "invoke-command", "-computername", "compName",
                        "-filepath", "C:\\script.ps1").start();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @dudel! I just figured it out as well. Stupid of me the error message was very clear. Keeping the code here in case it might be useful for others. Thanks again.

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.