Running Powershell script remotely through Java(通过 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 运行它时,我得到一个错误.听起来调用命令"不被识别为通过 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();
错误,
无法运行程序invoke-command":CreateProcess error=2,系统找不到指定的文件
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.
谢谢.
推荐答案
正如你所写的 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 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过 Java 远程运行 Powershell 脚本


基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01