How can I redirect PowerShell output when run from Task Scheduler?

前端 未结 9 967
我在风中等你
我在风中等你 2020-12-13 12:35

When running a simple PowerShell script from Task Scheduler, I would like to redirect the output to a file.

There is a long thread about this very topic here, yet i

9条回答
  •  渐次进展
    2020-12-13 12:58

    It would be maybe easier to use Start-Transcript in order to log directly to a file:

    # Get script name
    $ScriptFullPath = $MyInvocation.MyCommand.Path
    # Start logging stdout and stderr to file
    Start-Transcript -Path "$ScriptFullPath.log" -Append
    
    [Some PowerShell commands]
    
    # Stop logging to file
    Stop-Transcript
    

    The log file will be in the same directory as the script.

提交回复
热议问题