0

I am writing a PowerShell script that has to invoke a legacy Perl Script and display the output of this long running Perl script in real time to PowerShell as I would normally view in a Windows command prompt or interactive Putty SSH session. My requirement is to write the output of the Perl script to a log file in real time and display the results to the PowerShell in real time.

My problem is that once the Perl script finishes running, all of the results are then returned to PowerShell or my log files.

Here is a simplified sample of my PowerShell code.

#Test.ps1
$env:Path = "C:\Dev\perl583\bin;" + $env:Path
$WORKING_PATH="C:\Dev\scripts"
perl $WORKING_PATH\LegacyScript.pl *>> C:\Dev\results\realtime_output.txt
exit $LASTEXITCODE

Here is a sample Perl Script that simulates a long running operation invoked by the PowerShell script:

#LegacyScript.pl
for( $a = 1; $a < 6; $a = $a + 1 ){
  print "New value of a: $a\n";
  sleep(10)
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Kase420
  • 43
  • 5
  • This allows you to make a transcript - you should then be able to call the script directly without redirecting output: https://stackoverflow.com/questions/1215260/how-to-redirect-the-output-of-a-powershell-to-a-file-during-its-execution – bytepusher Aug 12 '17 at 23:03
  • Thanks. Even with the transcript, it still seems like output is not being dumped until after the perl script's execution is done. – Kase420 Aug 13 '17 at 00:11
  • Ah I misunderstood your problem then, I thought you needed it in both ( "or my log files" -> "and my log files" ). I don't know enough about power shell to help you then - but I don't see what this has to do with perl, really. – bytepusher Aug 13 '17 at 00:44
  • Actually, after a day of researching I think I found the answer to my own question courtesy of PerlMonks.org . It is perl in the way it buffers the output (line vs. block). Once I set "$| = 1;" in my Perl script, I got the expected results from both the PowerShell itself and PowerShell's file redirection. Here is the site for those that may run into this problem integrating legacy scripts as in my case. http://www.perlmonks.org/?node_id=305801 – Kase420 Aug 13 '17 at 01:37
  • I stand corrected, well done :) – bytepusher Aug 13 '17 at 11:40

1 Answers1

3

After a day of researching I think I found the answer to my own question courtesy of PerlMonks.org . It is perl in the way it buffers the output (line vs. block). Once I set "$| = 1;" in my Perl script, I got the expected results from both the PowerShell itself and PowerShell's file redirection. Here is the site for those that may run into this problem integrating legacy scripts as in my case.

http://www.perlmonks.org/?node_id=305801 "By setting $| as Anonymous Monk indicated, you force the output to be flushed after each write." - Direct Quote from this Website that solved my problem.

Kase420
  • 43
  • 5
  • The spin off to my earlier question now that I got Perl from Windows incrementally outputting during the Perl scripts execution is that I hit the same problem from Linux. Currently, I am trying to do an Invoke-SshCommand from a WIndows Server to run my perl script on a Linux machine. Like before even after updating the Perl script, PowerShell is not getting any output until after the Perl script finishes and the Invoke-SshCommand cmdlet returns. If anyone with experience with SSH.Net has gotten through this problem, the help would be appreciated. Thanks! – Kase420 Aug 13 '17 at 11:42