在任务计划程序中设置时,PowerShell脚本未运行

时间:2019-06-02 21:42:09

标签: powershell

我正在尝试通过powershell创建任务以删除一些早于6个小时的文件,如果我从powershell执行脚本没有任何问题,如果我尝试从任务计划程序执行则没有任何反应。.

在预定任务中呼叫Powershell.exe

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

设置此参数:

-NoProfile -ExecutionPolicy Bypass -Command -NonInteractive -File "C:\Scripts\DeleteFilesDiff3H.PS1"

任务计划程序不启动我的脚本可能是什么问题?

试图通过某些解决方案来解决类似问题,但没有成功

$Path = "E:\MyPath"
$now = Get-Date

Get-Childitem * |
Where-Object { $_.LastWriteTime -le $now.AddHours(-6) } |
Remove-Item -Recurse -Force

我收到以下消息:

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.

1 个答案:

答案 0 :(得分:4)

您不会使用已定义的变量$path,因此Get-ChildItem将永远不会在那里出现。将您的代码更新为以下代码,然后检查它是否适合您:

$Path = "E:\MyPath"
$now = Get-Date

Get-Childitem -path $Path |
Where-Object { $_.LastWriteTime -le $now.AddHours(-6) } |
Remove-Item -Recurse -Force