运行为计划任务的PowerShell代码因Program Files文件夹而失败

时间:2013-08-19 03:35:26

标签: powershell scheduled-tasks

我正在尝试直接在计划任务中运行以下PowerShell命令。

Powershell.exe -command Get-ChildItem -Path 'C:\Program Files (x86)\CA\ARCserve Backup\Reports' -Force | Where-Object {($_.Name -like 'AB*.xml') -and ($_.LastWriteTime -lt (get-date).AddDays(-7))} | Move-Item -Destination 'C:\Program Files(x86)\CA\ARCserve Backup\Reports\OldReports'

它失败,因为计划任务似乎没有以管理权限运行。

任务配置如下:

  • 运行用户是否已登录
  • 具有本地管理员权限的指定用户
  • 以最高权限运行
  • 操作 - 启动程序
  • 计划= powershell.exe
  • 参数= -command Get-ChildItem -Path 'C:\Program Files (x86)\CA\ARCserve Backup\Reports' -Force | Where-Object {($_.Name -like 'AB*.xml') -and ($_.LastWriteTime -lt (get-date).AddDays(-7))} | Move-Item -Destination 'C:\Program Files(x86)\CA\ARCserve Backup\Reports\OldReports'

文件夹的权限已更改为(经过身份验证的用户=完全控制)

如果我在同一个计划任务中运行以下命令,则表示已成功完成。

powershell.exe -command Get-ChildItem -Path 'C:\Reports' -Force | Where-Object {($_.Name -like 'AB*.xml') -and ($_.LastWriteTime -lt (get-date).AddDays(-7))} | Move-Item -Destination 'C:\Reports\OldReports'

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

对于此帐户,执行政策可能过于严格。 尝试通过在参数中添加-executionpolicy unrestricted来设置executionpolicy。 如果失败,将有助于查看调度程序的日志。

相关问题