Running Powershell script remotely through Java

天涯浪子 提交于 2019-12-02 05:46:57

问题


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:


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();


来源:https://stackoverflow.com/questions/40351357/running-powershell-script-remotely-through-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!