从Java调用Powershell脚本

时间:2011-11-16 06:31:10

标签: 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

3 个答案:

答案 0 :(得分:8)

启动进程(runtime.exec())后,添加一行以关闭进程的输入流(JAVA调用输出流!!):

 proc.getOutputStream().close();

答案 1 :(得分:4)

是的,我们可以使用powershell脚本创建远程会话和执行cmdlet。

将以下Power 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()。它会显示错误。

答案 2 :(得分:4)

现在您可以使用jPowerShell

轻松完成
-