Invoke Powershell scripts from Java

前端 未结 3 901
盖世英雄少女心
盖世英雄少女心 2020-12-08 11:56

I want to invoke my powershell script from java. Can it be done. I tried with the following code, but the stream is not closing.

import java.io.BufferedRead         


        
相关标签:
3条回答
  • 2020-12-08 12:22

    Now you can do it easily with jPowerShell

    powerShell = PowerShell.openSession();
    
    //Print results    
    System.out.println(powerShell.executeScript("\"C:\\testscript.ps1\"").getCommandOutput());
    
    powerShell.close();
    
    0 讨论(0)
  • 2020-12-08 12:35

    After starting the process ( runtime.exec() ), add a line to close the input stream of the process ( which JAVA calls output stream!!):

     proc.getOutputStream().close();
    
    0 讨论(0)
  • 2020-12-08 12:37

    Yes we can create remote session and execute cmdlets using powershell script.

    Save the following Power shell script to 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 Code :

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

    If you do not get output, try this: powerShellProcess.getErrorStream() instead powerShellProcess.getInputStream(). It will show the errors.

    0 讨论(0)
提交回复
热议问题