简体   繁体   English

PowerShell脚本卡在ReadLine()上

[英]PowerShell script is stuck on ReadLine()

I have a java application that executes a PowerShell script. 我有一个执行PowerShell脚本的Java应用程序。
In my java code I read from the the input stream (data that comes from the script) and write to the output stream (data that goes to the script). 在我的Java代码中,我从输入流中读取(来自脚本的数据),然后写入输出流中(到达脚本的数据)。 I also handles the error stream separately. 我还单独处理错误流。
In the PowerShell script I am using the following command to read from the standard input stream of the script: 在PowerShell脚本中,我使用以下命令从脚本的标准输入流中读取:

$Line = [Console]::ReadLine()

When the script reaches this point in the code it hangs on the ReadLine() till something is written to the stream, that's where the java application comes in. 当脚本到达代码中的这一点时,它将挂在ReadLine()上,直到将某些内容写入流中为止,这就是Java应用程序进入的地方。
In java I am writing the following to the output stream (This code is not the entire code but in general and only the important parts of it): 在Java中,我将以下内容写入输出流(此代码不是完整的代码,但通常只是其中的重要部分):

public void runMain()
{
   String[] params = "the command and arguments here";
   Process process = r.exec(params, null);
   outStream = new BufferedOutputStream(process.getOutputStream());
   inStream  = new BufferedInputStream(process.getInputStream());
   errStream = new BufferedInputStream(process.getErrorStream());

   startCmd     = "some text here\r\n";
   wtByte       = startCmd.getBytes();
   outStream.write(wtByte, 0, wtByte.length);
   outStream.flush();

   while (!isScriptTerminated && !wasError && !isScriptFinished)
   {
       if (errStream != null)
       {
           // handle error
       }

       if (inStream != null)
       {
           // read the data from the STDOUT of the script (our input)
           readBuffer = receive();
           ...
       }
       ...
   }
}

Has you can see I am writing to the outStream right at the beginning and then I am calling the flush() to actually flush all to the stream, but at this point the script is stuck and hangs on the ReadLine() and my java code reaches the receive() method and waiting for the script to response and write something so standard input, but in this case java code will hang forever because the script is stuck on the ReadLine() forever. 您是否可以看到我从一开始就在向outStream写入内容,然后调用flush()实际上将所有内容刷新到流中,但是此时脚本被卡住并挂在ReadLine()和我的Java代码上到达receive()方法并等待脚本响应并编写标准输入内容,但是在这种情况下,由于脚本永久卡在ReadLine()上,因此Java代码将永远挂起。 Then after a while I have timeout mechanism that kills the process and release the program. 然后过了一会儿我有了超时机制,该机制可以终止进程并释放程序。
In the experiments that I did I have duplicated the writing to the outStream at the beginning and it sometimes worked and sometimes not. 在我做的实验中,我在一开始就将文字复制到outStream上,有时有效,有时无效。 Meaning, the second flush wrote to the stream and script got it. 意思是,第二次刷新写入了流,脚本得到了它。
I must say that I am using the same code to run Perl, VB and Python scripts with no problem. 我必须说,我使用相同的代码来毫无问题地运行Perl,VB和Python脚本。
Moreover, this code is working on my machine (Windows 10) and not working on some other machines like Windows 7, Windows Server 2008. Maybe it has something to do with the PowerShell version? 此外,此代码在我的计算机(Windows 10)上运行,而在Windows 7,Windows Server 2008等其他计算机上不运行。也许与PowerShell版本有关?
The question is how come? 问题是怎么来的?
What can I do to overcome this problem? 我该怎么做才能克服这个问题?
I didn't succeeded to use Read-Host command in PowerShell and I got the same behavior, Is there another way to read in PowerShell script? 我没有在PowerShell中成功使用Read-Host命令,并且得到了相同的行为,是否有另一种方法可以在PowerShell脚本中进行读取?

It appears that there is a bug in PowerShell version 2.0, once I upgraded to version 3.0 it worked. 在我升级到3.0版后,PowerShell 2.0版似乎存在一个错误。
On my machine there is version 5.0, that's why it worked on my machine. 我的机器上有5.0版,这就是为什么它可以在我的机器上使用。 On the other machines that it didn't work there was version 2.0. 在其他无法正常运行的计算机上,有版本2.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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