简体   繁体   English

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

[英]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.

相关问题 在任务计划程序上运行PowerShell脚本 - Running PowerShell Script on Task Scheduler Powershell 脚本未在任务计划程序上运行 - Powershell script not running on Task Scheduler 从任务计划程序运行 Powershell 脚本 - Running a Powershell script from Task Scheduler 在任务计划程序中运行PowerShell - Running PowerShell in Task Scheduler 当脚本所在的文件夹的名称带有空格时,从任务计划程序运行Powershell脚本 - Running Powershell script from task scheduler when the name of the folder where script is kept has spaces 在“运行是否登录用户”模式下,使用任务计划程序运行Powershell脚本会冻结 - Running Powershell script using Task Scheduler freezes when in “Run whether user is logged in or not” mode 当计算机锁定时,为什么任务计划程序未在透明的动态视图[网络共享]中运行powershell脚本? - Why task scheduler is not running powershell script in clearcase dynamic view [network share ]when the machine is locked? 在任务计划程序上运行时,用于删除引号的 powershell 脚本代码不会删除它们 - powershell script code to remove quotation marks doesn't remove them when running on task scheduler 从Windows Task Scheduler运行PowerShell脚本时调用函数时出错 - Error calling function when running PowerShell script from WIndows Task Scheduler 我的 PowerShell 任务调度程序脚本在运行时显示运行时异常错误 - My PowerShell task scheduler script is showing a runtime exception error when running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM