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)
}
5
  • This allows you to make a transcript - you should then be able to call the script directly without redirecting output: stackoverflow.com/questions/1215260/… Commented Aug 12, 2017 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. Commented Aug 13, 2017 at 0: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. Commented Aug 13, 2017 at 0: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. perlmonks.org/?node_id=305801 Commented Aug 13, 2017 at 1:37
  • I stand corrected, well done :) Commented Aug 13, 2017 at 11:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.