从Task Scheduler运行简单的PowerShell脚本时,我想将输出重定向到文件。
关于此主题here的话题很长,但尚不清楚它们是否最终找到了最合适的解决方案。我很想知道Stack Overflow上是否有人也解决了这个问题,以及他们是如何做到的。
这是对我有用的命令。我不喜欢在脚本中重定向输出的想法,因为这将使其难以手动运行。
powershell -windowstyle minimized -c "powershell -c .\myscript.ps1 -verbose >> \\server\myscript.log 2>&1"
我使用脚本功能来帮助解决这个问题。只需将其包含在您的代码中,它将所有(可能)屏幕内容输出到日志文件。
Start-Transcript -path $LogFile -append
<body of script>
Stop-Transcript
以下内容适用于Windows 7:
powershell -command c:\temp\pscript.ps1 2>&1 > c:/temp/apickles.log
在win7 Task Scheduler gui中:
program = "Powershell"
arguments = "-command c:\temp\pscript.ps1 2>&1 > c:/temp/apickles.log"
请注意,“-file”无效,因为文件名之后的所有参数都被解释为文件的参数。请注意,“ 2>&1”也将错误输出重定向到日志文件。更高版本的powershell这样做的方式略有不同。
我该怎么做:创建一个函数来调用脚本并重定向该函数的输出,如下所示:
。ps1:
function test{
#your simple script commands
ls c:\temp -Filter *.JPG
ls z:\ #non existent dir
}
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
我知道这个问题已经被回答了两次,但是以上所有答案的组合确实帮助了我...
我的问题是,作为包装程序供应商的一部分,我需要通过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
Stop-Transscript
日志文件将与脚本位于同一目录。
病区上的Powershell 3允许重定向各个流(输出,冗长,错误)。但是,任务计划程序不了解它们。它是执行重定向的功能。
@ 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
此详细信息发送到屏幕并记录到文件中花了一点时间弄清楚这个,所以这个线程似乎是最合适的放置位置
something your doing -Verbose 4>&1|Tee-Object "C:\logs\verb.log" -Append