2016-11-20 10 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"); 
    } 
} 

は次のようになります。代わりに、私はコードがVMware上で動作しているWindows ServerのPowerShellでコマンドを実行したいローカル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ヘルプトピック」を参照してください。 –

関連する問題