Java 使用JSch在powershell上执行脚本
我想执行powershell,然后在远程windows系统上执行一些命令,我正在使用JSCH()。我在windows计算机上使用Win32 OpenSSH 当我使用“exec”通道时,它会运行powershell并将其关闭,因此我无法在powershell中运行命令。另一方面,如果我运行“shell”通道,我会从命令提示符获得输出,例如“Microsoft Windows[Version 10.0.14393](c)2016 Microsoft Corporation。保留所有权利。”因此我希望执行powershell并获取powershell进程的流,以便我可以将脚本写入该流并读取输出。我通过查看示例得到的流是运行powershell本身的SSH连接流,而不是powershell内部/内部的流 我使用以下代码在powershell中执行命令Java 使用JSch在powershell上执行脚本,java,powershell,ssh,jsch,Java,Powershell,Ssh,Jsch,我想执行powershell,然后在远程windows系统上执行一些命令,我正在使用JSCH()。我在windows计算机上使用Win32 OpenSSH 当我使用“exec”通道时,它会运行powershell并将其关闭,因此我无法在powershell中运行命令。另一方面,如果我运行“shell”通道,我会从命令提示符获得输出,例如“Microsoft Windows[Version 10.0.14393](c)2016 Microsoft Corporation。保留所有权利。”因此我希望
private Streams executePowershell(Session session, String command) throws JSchException, IOException {
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
((ChannelExec) channel).setErrStream(System.err);
//4. Getting response as a stream
channel.connect();
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
System.out.println(out);
Streams streams = new Streams();
streams.setInputStream(in);
streams.setOutputStream(out);
streams.setChannel(channel);
return streams;
}
//command will be powershell to invoke the powershell itself
//command2 will be something such as dir that is the command to execute within powershell
public String executeCommand(String command, String command2) {
String output = null;
try {
Session session = getSession();
Streams streams = null;
streams = executePowershell(session, command);
OutputStream outputStream = streams.getOutputStream();
Util.writeToStream(outputStream, command2);
output = Util.readStream(streams.getInputStream()).toString();
streams.getChannel().disconnect();
session.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
请将您的问题包括在程序中的相关源代码中。具体来说,展示如何使用jsch调用powershell。好的,我已经添加了相关的源代码。我们需要。嘿,你能找到解决方案吗?