可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 its not clear if they reached the most appropriate solution in the end. I'm interested if anyone on SO has also solved this problem, and how they did it.
回答1:
Here is the command that worked for me. I didn't like the idea of redirecting the output in the script, since it would make it difficult to run manually.
powershell -windowstyle minimized -c "powershell -c .\myscript.ps1 -verbose >> \\server\myscript.log 2>&1"
回答2:
I use the Transcript feature to help with this. Just include it in your code and it outputs all (would be) screen content to a log file.
Start-Transcript -path $LogFile -append
<body of script>
Stop-Transcript
回答3:
The following works for me on windows 7 :
powershell -command c:\temp\pscript.ps1 2>&1 > c:/temp/apickles.log
In the win7 Task Scheduler gui:
program = "Powershell"
arguments = "-command c:\temp\pscript.ps1 2>&1 > c:/temp/apickles.log"
Note that "-file" does not work because all parameters after the file name are interpreted as params to the file.
Note that "2>&1" redirects the error output to the log file as well. Later versions of powershell do this in a slightly different way.
回答4:
I whould do :
create a function to call your script and redirect output of this function like this :
.ps1 :
function test{
#your simple script commands
ls c:\temp -Filter *.JPG
ls z:\ #non existent dir
}
test *> c:\temp\log.txt
here is the log file :
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
You can control what do you want to output with the new V3 redirection operators :
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
回答5:
I realise this has been answered a couple times already but a combination of all the answers above really helped me out...
My problem was that I needed to execute a Powershell script over WinRM as part of a Packer provisioner and it kept failing due to rights issues. The way around this is to execute as a scheduled task but I needed to pass arguments to the script and get the output as well to confirm everything passed.
This snippet worked well for me:
# 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)
The full script can be found here:
https://gist.github.com/dev-rowbot/fa8b8dadf1b3731067a93065db3e1bba
回答6:
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
Stop-Transscript
Log file will be in the same directory as the script.
回答7:
Powershell 3 on wardsallows individual streams (out, verbose, error) to be redirected. However Task Scheduler does not understand them. Its the powershell that does the redirection.
@cmcginty's solution halfway works, since the powershell is invoking the powershell, it only supports only standard streams (error, out). If you want to use all strewams you need to use
Program or Script : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Argument: -windowstyle minimized –NonInteractive –NoProfile -c "powershell -c path_to_ps1.ps1 4>>verbose.log 5>>debug.log"
Start In: path_to_ps1
回答8:
this send verbose out put t the screen as well as log it to a file
took a while to figure this out so this thread seemed the most fitting place to put it
something your doing -Verbose 4>&1|Tee-Object "C:\logs\verb.log" -Append