2016-11-20 62 views
0

我编写了一个执行PowerShell命令的Java程序。这里是我的代码:在Java程序中执行Vmware PowerShell命令

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class PowerShellCommand { 
    public static void main(String[] args) throws IOException { 
    String command = "powershell.exe your command"; 
    // Getting the version 
    String command = "powershell.exe $PSVersionTable.PSVersion"; 
    // Executing the command 
    Process powerShellProcess = Runtime.getRuntime().exec(command); 
    // Getting the results 
    powerShellProcess.getOutputStream().close(); 
    String line; 
    System.out.println("Standard Output:"); 
    BufferedReader stdout = new BufferedReader(new InputStreamReader(
     powerShellProcess.getInputStream())); 
    while ((line = stdout.readLine()) != null) { 
     System.out.println(line); 
    } 
    stdout.close(); 
    System.out.println("Standard Error:"); 
    BufferedReader stderr = new BufferedReader(new InputStreamReader(
     powerShellProcess.getErrorStream())); 
    while ((line = stderr.readLine()) != null) { 
     System.out.println(line); 
    } 
    stderr.close(); 
    System.out.println("Done"); 
    } 
} 

我想要做的是:而不是在本地的PowerShell我想使代码其中在VMware上运行Windows Server PowerShell的执行命令执行命令?我应该如何修改代码才能这样做?

回答

0

有无PowerShell的invoke在远程主机上的命令:

String server = "remotehost"; 
String command = "powershell.exe -Command \"&{Invoke-Command -Computer " + 
       server + " -ScriptBlock {$PSVersionTable.PSVersion}}\""; 

远程主机需要有PSRemoting启用这个工作。

+0

我做你已经告诉我但是这个错误弹出:连接到远程服务器192.168.2.3失败,出现以下错误消息:访问被拒绝。有关 的更多信息,请参阅about_Remote_Troubleshooting帮助主题。 + CategoryInfo:OpenError:(192.168.2.3:String)[],PSRemotingTransportException + FullyQualifiedErrorId:AccessDenied,PSSessionStateBroken -------------------------- ----------------------------------我做了大量的研究,但我找不到任何处理这个问题 –

+0

您是否“看到about_Remote_Troubleshooting帮助主题”? –