Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect Powershell output from a script run by TaskScheduler and override default width of 80 characters

I have a powershell script which is invoked by the task scheduler. The task's definition also redirects the output using >> to a log file. The PowerShell script invokes a C# console program which generates output which is longer than 80 characters in width. However, the resultant output in the log file is wrapping all the output from the C# program to 80- characters. Note that only the output from the C# program is wrapped. All other output from the script (generated by Write-Host) is not wrapped. Also note that this wrapping only occurs when the script is run by the task scheduler.

Here is the script calling chain:

  • The task definition invokes a .cmd script (RunWidthTest.cmd)

  • RunWidthTest.cmd: powershell -File .\RunReports.ps1 >> C:\Log\output.log

  • RunReports.ps1 (snippet):

    Write-Host "======================================================== Running reports for date file for $valueDate"

    .\ReportRunner.exe --ValueDate $valueDate | Out-Default

Note that piping the outout to Out-Default is required to avoid another powershell error that occurs due to the fact that an executable's output is attempting to write to the same stream as the powershell script without powershell being aware of it (Write-Host : The OS handle's position is not what FileStream expected. Do not use a handle simultaneously in one FileStream and in Win32 code or another FileStream. This may cause data loss.)

So in this case here, the string output by the Write-Host line is not wrapped, however, any output generated by ReportRunner.exe is wrapped to 80 characters and it only occurs when run with the task scheduler! The same behaviour occurs if you change the point at which the redirection occurs, ie if you redirect in the definition the task (the task's command line instead of inside the script)

Any clues as to why this would occur and how to override this width restriction.

Thanks.

like image 670
Plastikfan Avatar asked Dec 21 '10 12:12

Plastikfan


People also ask

How do I redirect the output of a PowerShell File?

Use the Out-File cmdlet, which sends command output to a text file. Typically, you use the Out-File cmdlet when you need to use its parameters, such as the Encoding , Force , Width , or NoClobber parameters. Use the Tee-Object cmdlet, which sends command output to a text file and then sends it to the pipeline.

How do I run a PowerShell script silently in Task Scheduler?

Create a shortcut that calls the PowerShell script and set the Run option to Minimized. This will prevent a window from flashing although you will still get a momentary blip of the script running on the Task Bar. Save this answer.

Can we schedule PowerShell script?

You can use the schedules. ps1 file to configure schedules via PowerShell script. To edit or view the configuration file, navigate to Settings \ Configurations and click schedules.


2 Answers

Did you try

| Out-File C:\Log\output.log -width 120 # or whatever size  you need
like image 62
bernd_k Avatar answered Oct 04 '22 06:10

bernd_k


Scripting Guy to the rescue!

Link: How can I expand the width of the Windows PowerShell Console

This worked for me:

# Change the PS host parameters, so that log file width is wider than 80 characters
$PSHost  = Get-Host
$PSWindow = $PSHost.UI.RawUI
$PSBuffer = $PSWindow.BufferSize
$PSBuffer.Height = 3000
$PSBuffer.Width = 150
$PSWindow.BufferSize = $PSBuffer
$PSBuffer = $PSWindow.WindowSize
$PSBuffer.Height = 50
$PSBuffer.Width = 150
$PSWindow.WindowSize = $PSBuffer
like image 28
Wouter Speybrouck Avatar answered Oct 04 '22 05:10

Wouter Speybrouck