本文介绍了如何从TaskScheduler运行的脚本重定向Powershell输出并覆盖默认的80个字符的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Powershell脚本,由任务调度程序调用.任务的定义还使用>>将输出重定向到日志文件. PowerShell脚本调用一个C#控制台程序,该程序会生成宽度超过80个字符的输出.但是,日志文件中的结果输出将C#程序的所有输出包装为80个字符.请注意,仅包装了C#程序的输出.脚本的所有其他输出(由Write-Host生成)均未包装.另外请注意,仅当任务调度程序运行脚本时,才会发生这种包装.

这是脚本调用链:

  • 任务定义调用一个.cmd脚本(RunWidthTest.cmd)

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

  • RunReports.ps1(摘要):

    写主机"=========================================== ====================================================================

    .\ ReportRunner.exe --ValueDate $ valueDate |超默认值

请注意,需要将输出管道传送到Out-Default,以避免发生另一个Powershell错误,该错误是由于可执行文件的输出试图在Powershell不知道的情况下尝试写入与Powershell脚本相同的流( Write-Host:OS句柄的位置与FileStream所期望的位置不同.请勿同时使用句柄在一个FileStream中,并在Win32代码中或在另一个FileStream中.这可能会导致数据丢失.)

因此,在这种情况下,Write-Host行的输出字符串未包装,但是ReportRunner.exe生成的任何输出都包装为80个字符,并且仅在使用任务计划程序运行时才会发生!如果您更改重定向发生的位置,即如果您在定义中重定向任务(任务的命令行而不是脚本内),则会发生相同的行为

关于为什么会发生这种情况以及如何覆盖此宽度限制的任何线索.

谢谢.

解决方案

您尝试过

| Out-File C:\Log\output.log -width 120 # or whatever size  you need

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 simultaneouslyin 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.

解决方案

Did you try

| Out-File C:\Log\output.log -width 120 # or whatever size  you need

这篇关于如何从TaskScheduler运行的脚本重定向Powershell输出并覆盖默认的80个字符的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 08:06