首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java使用函数调用Powershell脚本,但未将Write-Host返回给java

Java使用函数调用Powershell脚本,但未将Write-Host返回给java
EN

Stack Overflow用户
提问于 2021-04-09 18:33:10
回答 2查看 90关注 0票数 1

我正在使用Java调用powershell脚本。powershell脚本是使用函数构建的,该函数会将值写入控制台。我需要在java中捕获这些值。我的poweshell脚本如下所示

代码语言:javascript
复制
 $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
}

当通过java调用上面的代码时,我没有得到while循环中的值,如下所示。它只会在powershell脚本执行后结束。

代码语言:javascript
复制
 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");
       }

如果有人能帮我解决这个问题,那就太好了。

EN

回答 2

Stack Overflow用户

发布于 2021-04-09 18:55:43

  • 你不需要cmd.exe。您可以直接运行powershell.exe.
  • Your PowerShell脚本将输出发送到Out-Null,因此显然不会向标准output.
  • powershell.exe写入任何内容接受可用于运行脚本的

参数。

代码语言:javascript
复制
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);
    }
}

注意,您必须调用方法waitFor(),以便您的java代码将等待,直到PowerShell脚本终止。

请记住,ProcessBuilder不会模拟命令提示符。在ProcessBuilder构造函数中,您需要将传递的命令拆分为一个单词列表。

当然,如果您只想打印PowerShell脚本输出,那么只需调用ProcessBuilder类的方法redirectIO()即可。然后上面的代码变成:

代码语言:javascript
复制
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);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-04-09 18:41:54

您可以在从proc获取输入流之前使用proc.waitFor();

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67019474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档