[英]PowerShell Script not running when set in the task scheduler
I'm attempting to create a task via powershell to delete some files older then 6 hours, if I execute the script from powershell there are no issues, if I try to execute from task scheduler nothing happens.. 我正在尝试通过Powershell创建任务以删除一些早于6个小时的文件,如果我从Powershell执行脚本没有任何问题,如果我尝试从任务计划程序执行则什么也没发生。
Call the Powershell.exe
in my schedulded task: 在我预定的任务中调用
Powershell.exe
:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Set this Parameters : 设置此参数:
-NoProfile -ExecutionPolicy Bypass -Command -NonInteractive -File "C:\Scripts\DeleteFilesDiff3H.PS1"
What could be the problem of the task scheduler not launching my script? 任务计划程序无法启动我的脚本可能是什么问题?
Tried to aply some solutions provide to similar issues without success 试图适当地提供一些解决方案来解决类似问题而没有成功
$Path = "E:\MyPath"
$now = Get-Date
Get-Childitem * |
Where-Object { $_.LastWriteTime -le $now.AddHours(-6) } |
Remove-Item -Recurse -Force
I got this messages: 我收到此消息:
Task Scheduler started "{38dcd44b-4210-473b-921e-3cc1442ff03b}" instance of the "\Delete Files 3H" task for user "my user".
Task Engine "S-1-5-21-159114655-2248028564-2417230598-213599:My User:Interactive:LUA[2]" received a message from Task Scheduler service requesting to launch task "\Delete Files 3H" .
Task Scheduler launched "{38dcd44b-4210-473b-921e-3cc1442ff03b}" instance of task "\Delete Files 3H" due to a time trigger condition.
Task Scheduler successfully completed task "\Delete Files 3H" , instance "{618e6f44-b523-4c56-ae0b-04d3552391cc}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" with return code 0.
You don't make use of the defined variable $path
so Get-ChildItem
will never look there. 您无需使用已定义的变量
$path
因此Get-ChildItem
将永远不会在那里出现。 Update your code to the following and check if this works for you: 将您的代码更新为以下代码,然后检查是否适合您:
$Path = "E:\MyPath"
$now = Get-Date
Get-Childitem -path $Path |
Where-Object { $_.LastWriteTime -le $now.AddHours(-6) } |
Remove-Item -Recurse -Force
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.