在任务计划程序中运行简单的PowerShell脚本时,我希望将输出重定向到文件。
有一个关于这个话题的长篇讨论在这里,但不清楚他们最终是否达到了最合适的解决方案。我想知道Stack Overflow上是否有人也解决了这个问题,以及他们是如何做到的?
在任务计划程序中运行简单的PowerShell脚本时,我希望将输出重定向到文件。
有一个关于这个话题的长篇讨论在这里,但不清楚他们最终是否达到了最合适的解决方案。我想知道Stack Overflow上是否有人也解决了这个问题,以及他们是如何做到的?
我使用转录功能来帮助这个。只需将其包含在您的代码中,它就会将所有(可能是)屏幕内容输出到日志文件中。
Start-Transcript -path $LogFile -append
<Body of the script>
Stop-Transcript
这是对我有效的命令。我不喜欢在脚本中重定向输出,因为这样会使手动运行变得困难。
powershell -windowstyle minimized -c "powershell -c .\myscript.ps1 -verbose >> \\server\myscript.log 2>&1"
以下方法适用于我使用的Windows 7操作系统:
powershell -command c:\temp\pscript.ps1 2>&1 > c:/temp/apickles.log
在 Windows 7 的任务计划程序界面中: program = "Powershell"
arguments = "-command c:\temp\pscript.ps1 2>&1 > c:/temp/apickles.log"
请注意,"-file"不起作用,因为文件名后的所有参数都被解释为文件的参数。
请注意,"2>&1"也会将错误输出重定向到日志文件中。PowerShell的后续版本会以稍微不同的方式执行此操作。之前所有回答的结合真的帮了我很多...
我的问题是,我需要在 Packer provisioner 的一部分中通过 WinRM 执行 PowerShell 脚本,但由于权限问题而失败。绕过这个问题的方法是将其作为定时任务执行,但我需要传递参数到脚本并获取输出以确认一切都已经正确执行了。
这段代码片段对我非常有效:
# Generate a unique ID for this execution
$guid = [guid]::NewGuid()
$logFile = "C:\Windows\Temp\$guid.log"
$argument = "-NoProfile -ExecutionPolicy unrestricted -Command ""& {""$ScriptPath"" $ScriptArgs} 2>&1 > $logFile"""
$a = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $argument
Register-ScheduledTask -TaskName $TaskName -RunLevel Highest -User $username -Password $password -Action $a | Start-ScheduledTask
do {
Start-Sleep -Seconds 30
$task = Get-ScheduledTask -TaskName $TaskName
} while ($task.State -eq 4)
https://gist.github.com/dev-rowbot/fa8b8dadf1b3731067a93065db3e1bba
也许使用Start-Transcript会更容易,因为可以直接记录到文件中:
# 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
function test{
# Your simple script commands
ls c:\temp -Filter *.JPG
ls z:\ # Non-existent directory
}
test *> c:\temp\log.txt
这是日志文件:
Répertoire : C:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 07/06/2008 11:06 176275 HPIM1427.JPG
-a--- 07/06/2008 11:06 69091 HPIM1428.JPG
-a--- 07/06/2008 11:06 174661 HPIM1429.JPG
ls : Lecteur introuvable. Il n'existe aucun lecteur nommé « z ».
Au caractère C:\temp\test.ps1:14 : 1
+ ls z:\ #non existent dir
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (z:String) [Get-ChildItem], Driv
eNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetC
hildItemCommand
使用新的V3重定向运算符,您可以控制要输出什么:
Do-Something 3> warning.txt # Writes warning output to warning.txt
Do-Something 4>> verbose.txt # Appends verbose.txt with the verbose output
Do-Something 5>&1 # Writes debug output to the output stream
Do-Something *> out.txt # Redirects all streams (output, error, warning, verbose, and debug) to out.txt
我在Windows Server 2016上进行了测试,只需调用powershell
一次即可。这样,您就可以使用带有空格的文件夹名称:
Powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle minimized "scriptName.ps1 *>> '\\servername\sharename\folder name with space\logfilename.log'"
从PowerShell 3开始,可以重定向各个流(输出、详细和错误)。然而,任务计划程序不理解它们。是PowerShell进行重定向。
@cmcginty的解决方案部分有效,因为PowerShell正在调用PowerShell。它仅支持标准流(错误和输出)。如果要使用所有流,则需要使用:
程序或脚本:C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
参数:-windowstyle minimized –NonInteractive –NoProfile -c "powershell -c path_to_ps1.ps1 4>>verbose.log 5>>debug.log"
起始位置:path_to_ps1
-noninteractive -c "scriptpath.ps1 *>>logpath.txt"
为了每天保留一个文件,那么:
-noninteractive -c "scriptpath.ps1 *>>logpath-$((get-date).ToString('yyyy-MM-dd')).txt"
如果你需要带时间戳的日志文件,你可以删除第二个“>”(这表示将内容追加到已有文件的末尾),并扩展时间格式:
-noninteractive -c "scriptpath.ps1 *>logpath-$((get-date).ToString('yyyy-MM-dd_HH-mm-ss-fff')).txt"
something you're doing -Verbose 4>&1 | Tee-Object "C:\logs\verb.log" -Append