我能够通过 Powershell 本身运行以下 powershell 命令,
invoke-command -ComputerName "compName" -filepath "c:\script.ps1" -credential "admin"
但是当我尝试通过 Java 运行它时,出现错误。听起来“Invoke-command”不被识别为通过 Java 运行的程序。如果是这种情况,还有其他解决办法吗?
Process p = new ProcessBuilder()
.inheritIO()
.command("invoke-command", "-computername", "compName",
"-filepath", "C:\\script.ps1").start();
错误,
Cannot run program "invoke-command": CreateProcess error=2, The system cannot find the file specified
附注该错误与提供的 filePath 无关,而是与调用命令本身相关。
谢谢。
最佳答案
正如您所写的invoke-command
是一个Powershell命令,因此您必须调用Powershell来运行命令,如下所示:
Process p = new ProcessBuilder()
.inheritIO()
.command("powershell", "invoke-command", "-computername", "compName",
"-filepath", "C:\\script.ps1").start();
关于java - 通过Java远程运行Powershell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40351357/