2016-11-20 59 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幫助主題」? –