通过Java远程运行Powershell脚本

通过Java远程运行Powershell脚本

本文介绍了通过Java远程运行Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过Powershell本身运行以下powershell命令,

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

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

但是当我尝试通过Java,我收到一个错误。听起来像 Invoke-command不被认为是通过Java运行的程序。如果是这种情况,还有其他解决方案吗?

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

错误,

PS该错误与所提供的filePath无关,而是与invoke-command本身有关。

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

谢谢。

推荐答案

在编写 invoke-command 是Powershell命令时,因此必须调用Powershell tu来运行该命令,如下所示:

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

这篇关于通过Java远程运行Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:34