简体   繁体   中英

Java calling Powershell script with function and not returning Write-Host to java

I am using Java to call powershell script. The powershell script is built with function and the function will write values to console. I need to capture those values in java. my poweshell script is as below

 $TokenCSV="M:\work\Powershell\TokenExtractedFromDB_Spec.csv"
$TokenXlPath="M:\work\Powershell\TokenListconverted.xlsx"
$Switch="Token"
Write-Host "Inside ConvertCSVtoEXL2 calling fuc  :"
$x=ConverToExlFile $TokenCSV $TokenXlPath $Switch

###Function
function ConverToExlFile
{
 Param ([string]$TokenCSV,
        [string]$TokenXlPath,
        [string]$Switch)
    Write-Output "Inside ConverToExlFile Function  :"| Out-Null

    
    for($row = 2;$row -lt 10;$row++)
    {
    Write-Output "Inside for loop :$row"| Out-Null
    }
    return
}

while calling above code through java im not getting values in while loop as given below. it just finishes once powershell script executes.

 Process proc = runtime.exec("cmd.exe /c powershell.exe  M:\\work\\Powershell\\V2\\ConvertCSVtoEXL2.ps1");
        System.out.println("2...");
        InputStream is = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader reader = new BufferedReader(isr);
        String line;
        System.out.println("3");
       while ((line = reader.readLine()) != null)
       {
            System.out.println(line);
            //System.out.println(reader.readLine());
            System.out.println("4");
       }

IT would be great if any one can help me with this.

  • You don't need cmd.exe . You can directly run powershell.exe .
  • Your PowerShell script is sending output to Out-Null so obviously nothing will be written to standard output.
  • powershell.exe accepts a -File parameter which you can use to run your script.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PrcBldTs {

    public static void main(String[] args) throws IOException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-File", "M:\\work\\Powershell\\V2\\ConvertCSVtoEXL2.ps1");
        Process p = pb.start();
        try (InputStreamReader isr = new InputStreamReader(p.getInputStream());
             BufferedReader br = new BufferedReader(isr)) {
            String line = br.readLine();
            while (line != null) {
                System.out.println(line);
                line = br.readLine();
            }
        }
        int exitStatus = p.waitFor();
        System.out.println("exit status = " + exitStatus);
    }
}

Note that you must call method waitFor() so that your java code will wait until the PowerShell script terminates.

Remember that ProcessBuilder does not emulate a Windows command prompt. In the ProcessBuilder constructor you need to split the command that you pass into a list of words.

Of-course, if all you want to do is print the PowerShell script output, you can simply call method redirectIO() of class ProcessBuilder . Then the above code becomes:

import java.io.IOException;

public class PrcBldTs {

    public static void main(String[] args) throws IOException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-File", "M:\\work\\Powershell\\V2\\ConvertCSVtoEXL2.ps1");
        pb.inheritIO();
        Process p = pb.start();
        int exitStatus = p.waitFor();
        System.out.println("exit status = " + exitStatus);
    }
}

you can use proc.waitFor(); before getting input stream from proc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM