ps1 script using stream redirection works from cmd.exe but not from Task Scheduler
-
8. srpna 2011 5:40
Hello running 2008 R2
I have a basic script “CleanDisk.ps1” that deletes some files older then x days
# Clearing logs
$Path = "D:\mylogs\logs"
$Daysback = "-14"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete} | Remove-Item -recurse -force -verbose
Taking a tip from this post (could not really tell if 4>&1 was fully implemented or not)
I test the script from cmd.exe like so
powershell.exe -command "D:\Scripts\CleanDisk.ps1" 4>&1 > "D:\Scripts\logs\clean_logs.log"
This runs as expected and writes verbose info to the log
VERBOSE: Performing operation "Remove File" on Target "D:\logs\do_submit_08JUN11124843.log"
When I schedule this same exact script with Task Scheduler it does not work as expected. Task Scheduler says it completes without error but it does not delete the files or write to the log. Appears something this wrong with the stream or my user error. Still new to this.
In my setup Program/script:
powershell.exe
Add arguments (Optional):
-command "D:\BRM\Scripts\CleanDisk.ps1" 4>&1 > "D:\Scripts\logs\clean_logs.log"
When I schedule the script with just 2>&1 it works but I do not get any verbose information on the files that were deleted.
Help/Insight but appreciated.
Thanks
- Upravený agianni2 8. srpna 2011 5:53
Všechny reakce
-
8. srpna 2011 5:53
Looks like you are trying to capture the output to a text file. If that is your requirement, I prefer using write-log powershell function in your script which works both from command prompt and from scheduled task.
I feel this is the simple way.
Thanks,
Thanks,Sitaram Pamarthi
Blog : http://techibee.com
This posting is provided AS IS with no warranties or gurentees,and confers no rights
-
8. srpna 2011 6:11
I am hoping for an easier way. I am not sure how to use that function and how to implement it.
If it works from the cmd line I am hoping there is a way to make it work scheduled.
-
8. srpna 2011 9:40
Hi,
Try put this command to bat file and run from task scheduler this bat:powershell.exe -command "D:\Scripts\CleanDisk.ps1" 4>&1 > "D:\Scripts\logs\clean_logs.log"
Or try use Start-Transcript / Stop-Transcript cmmandlets.Start-Transcript D:\Scripts\logs\clean_logs.log
Get-ChildItem $Path -recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete} | Remove-Item -recurse -force -verbose
Stop-Transcript
Or try put writing to log in ps script like this:Get-ChildItem $Path -recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete} | %{Out-File -inputobject $_.FullName -Append D:\Scripts\logs\clean_logs.log; Remove-Item $_ -force }
-
8. srpna 2011 23:24
When scheduling with the -File parameter it works as expected...all output streams are written to the log
powershell.exe -NoProfile -File D:\Scripts\CleanDisk.ps1 > D:\Scripts\logs\clean_logs.log
- Označen jako odpověď agianni2 8. srpna 2011 23:25
-
19. června 2012 7:12
I try this, I call ps1 file using parameters, and I need log all output to file, but it doesn't work.
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -WindowStyle Hidden -NoLogo -File "deploy.TemplateRemoteDeployScriptPS.ps1" Desarrollo DESIIS01 "Web" True True instalador passwd "C:\Tests\LifeCycle.IISDeploy" "LifeCycle.Deploy.Test.PowerShell" >C:\temp\resultadosDeploy.txt 2>&1
it doesn`t work
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -WindowStyle Hidden -NoLogo -File "deploy.TemplateRemoteDeployScriptPS.ps1" Desarrollo DESIIS01 "Web" True True instalador passwd "C:\Tests\LifeCycle.IISDeploy" "LifeCycle.Deploy.Test.PowerShell" >C:\temp\resultadosDeploy.txt
www.kiquenet.com/profesional