2011-11-16 62 views
12

我想從java調用我的powershell腳本。它可以做到。我試着用下面的代碼,但是流不關閉。從Java調用Powershell腳本

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

public class TestPowershell { 

    public static void main(String[] args) throws IOException 
    { 
     Runtime runtime = Runtime.getRuntime(); 
     Process proc = runtime.exec("powershell C:\\testscript.ps1"); 
     InputStream is = proc.getInputStream(); 
     InputStreamReader isr = new InputStreamReader(is); 
     BufferedReader reader = new BufferedReader(isr); 
     String line; 
     while ((line = reader.readLine()) != null) 
     { 
      System.out.println(line); 
     } 
     reader.close(); 
     proc.getOutputStream().close(); 
    } 

} 

java是否調用執行創建遠程會話和執行cmdlet的powershell腳本?

我們是否支持在java中調用powershell腳本?

任何人都可以請你幫忙。

等待您的回覆。

感謝, rammj

+0

您是否獲得一個異常輕鬆地做到這一點?您應該在finally {}塊中使用close()方法。 –

+2

請先閱讀本文http://kylecartmell.com/?p=9 – artbristol

回答

8

啓動進程(runtime.exec())後,添加一行代碼以關閉進程的輸入流(其Java調用輸出流!!):

proc.getOutputStream().close(); 
4

是我們可以使用PowerShell腳本創建遠程會話並執行cmdlet。

保存以下電源shell腳本testscript.ps1

#Constant Variables 
$Office365AdminUsername="YOUR_USERNAME" 
$Office365AdminPassword="TOUR_PASSWORD" 

#Main 
Function Main { 
#Remove all existing Powershell sessions 
    Get-PSSession | Remove-PSSession 

#Encrypt password for transmission to Office365 
    $SecureOffice365Password = ConvertTo-SecureString -AsPlainText $Office365AdminPassword -Force 


#Build credentials object 
    $Office365Credentials = New-Object System.Management.Automation.PSCredential $Office365AdminUsername, $SecureOffice365Password 
Write-Host : "Credentials object created" 

#Create remote Powershell session 
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Office365credentials -Authentication Basic –AllowRedirection 
Write-Host : "Remote session established" 

#Check for errors 
if ($Session -eq $null){ 
    Write-Host : "Invalid creditials" 
}else{ 
    Write-Host : "Login success" 
    #Import the session 
     Import-PSSession $Session 
} 

#To check folder size 
Get-MailboxFolderStatistics "YOUR_USER_NAME" | Select Identity, FolderAndSubfolderSize 

exit 
} 

# Start script 
. Main 

Java代碼:

try { 
      String command = "powershell.exe \"C:\\testscript.ps1\""; 
      ExecuteWatchdog watchdog = new ExecuteWatchdog(20000); 
      Process powerShellProcess = Runtime.getRuntime().exec(command); 
      if (watchdog != null) { 
       watchdog.start(powerShellProcess); 
      } 
      BufferedReader stdInput = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream())); 
      String line; 
      System.out.println("Output :"); 
      while ((line = stdInput.readLine()) != null) { 
       System.out.println(line); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

如果你沒有得到輸出,試試這個:powerShellProcess.getErrorStream(),而不是powerShellProcess.getInputStream()。它會顯示錯誤。

4

現在你可以用jPowerShell

powerShell = PowerShell.openSession(); 

//Print results  
System.out.println(powerShell.executeScript("\"C:\\testscript.ps1\"").getCommandOutput()); 

powerShell.close(); 
+0

您不應該複製並粘貼多個問題的答案。 –