2011-11-16 23 views
12

PowerShell komut dosyasını java'dan çağırmak istiyorum. Bu yapılabilir. Aşağıdaki kodla denedim, ancak akış kapanmıyor.Java'dan Powershell komut dosyalarını davet edin.

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 Uzak oturumu oluşturmak ve cmdlets yürütmek gerçekleştiren bir powershell komut çağırmak mu?

Java'da powershell komut dosyalarını çağırmak için desteğimiz var mı?

Bunun için lütfen yardım edebilirsiniz. Yanıtlarınız için bekliyoruz.

sayesinde süreç (runtime.exec()) başlattıktan sonra rammj

+0

Bir istisna alıyorsunuz ile kolayca yapabilirsiniz? Son olarak {} bloğunda close() yöntemlerini kullanmalısınız. –

+2

İlk önce bunu okuyun http://kylecartmell.com/?p=9 – artbristol

cevap

8

, sürecin giriş akışı kapatmak için bir satır ekleyin (JAVA !! çıkış akışı çağırır):

proc.getOutputStream().close(); 
4

biz Evet uzak oturum oluşturabilir ve powershell komut dosyasını kullanarak cmdlet'leri çalıştırabilir.

aşağıdaki Güç kabuk komut dosyası

#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 Kod testscript.ps1 kaydet: powerShellProcess.getErrorStream() yerine powerShellProcess.getInputStream(): Eğer çıktıyı alamazsanız

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(); 
     } 

, bunu deneyin. Hataları gösterecektir.

4

Şimdi jPowerShell

powerShell = PowerShell.openSession(); 

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

powerShell.close(); 
+0

Birden fazla soruya cevapları kopyalayıp yapıştırmamalısınız. –

İlgili konular