简体   繁体   English

计划任务不显示在 Get-ScheduledTask 结果中

[英]scheduled tasks don't show up in Get-ScheduledTask result

I have defined some scheduled task using Windows Task Scheduler GUI under "" [default] path but when i run Get-ScheduledTask in powershell, it does not return them.我已经在“”[默认] 路径下使用 Windows 任务计划程序 GUI 定义了一些计划任务,但是当我在 powershell 中运行Get-ScheduledTask时,它不会返回它们。 why?为什么?

I have tried with Get-ScheduledTask -TaskName "MyTaskName" with one of my task name but it comes up with "No MSFT_ScheduledTask objects found with property 'TaskName' equal to 'MyTaskName'"我尝试使用Get-ScheduledTask -TaskName "MyTaskName"和我的任务名称之一,但它出现了 "没有找到属性 'TaskName' 等于 'MyTaskName' 的 MSFT_ScheduledTask 对象"

Actually I have tried https://library.octopusdeploy.com/step-template/actiontemplate-windows-scheduled-task-disable but it doesn't work so I have tried running script directly.实际上我已经尝试过https://library.octopusdeploy.com/step-template/actiontemplate-windows-scheduled-task-disable但它不起作用所以我尝试直接运行脚本。

UPDATE I have found the following script to get task list on http://www.fixitscripts.com/problems/getting-a-list-of-scheduled-tasks :更新我在http://www.fixitscripts.com/problems/getting-a-list-of-scheduled-tasks上找到了以下脚本来获取任务列表:

# PowerShell script to get scheduled tasks from local computer
$schedule = new-object -com("Schedule.Service")
$schedule.connect() 
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks  | Format-Table   Name , LastRunTime    # -AutoSize
IF($tasks.count -eq 0) {Write-Host “Schedule is Empty”}
Read-Host

UAC通用空调

The result is likely affected by UAC .结果可能受UAC影响。 To see everything try right clicking the PowerShell icon, select Run as Administrator and then run your Get-ScheduledTask command again and see if that changes anything.要查看所有内容,请尝试右键单击 PowerShell 图标,选择Run as Administrator ,然后再次运行Get-ScheduledTask命令,看看是否有任何变化。

Further reading: http://david-homer.blogspot.co.uk/2017/10/not-all-scheduled-tasks-show-up-when.html进一步阅读: http ://david-homer.blogspot.co.uk/2017/10/not-all-scheduled-tasks-show-up-when.html

Have you tried using a com object?您是否尝试过使用 com 对象? This code works for me:此代码对我有用:

# FOR A REMOTE MACHINE
$s = 'SERVER_NAME' # update this with server name
($TaskScheduler = New-Object -ComObject Schedule.Service).Connect($s)


# FOR LOCAL MACHINE
($TaskScheduler = New-Object -ComObject Schedule.Service).Connect()

#now we can query the schedules...
cls;$TaskScheduler.GetFolder('\').GetTasks(0) | Select Name, State, Enabled, LastRunTime, LastTaskResult | Out-GridView

This code will retrieve a particular task and enable it:此代码将检索特定任务并启用它:

$task = $TaskScheduler.GetFolder('\').GetTask("TASKNAME")
$task.Enabled = $true

When running Get-ScheduledTask from a user-level prompt, even if the user is an administrator, they will see only the tasks that they can read with user-level permissions.当从用户级提示运行Get-ScheduledTask时,即使用户是管理员,他们也只会看到他们可以使用用户级权限读取的任务。 Seeing them in the TaskSchd.Msc window indicates that program is running with different permissions.在 TaskSchd.Msc 窗口中看到它们表明该程序正在以不同的权限运行。

Therefore, running a PowerShell prompt as administrator solves the problem.因此,以管理员身份运行 PowerShell 提示符可以解决问题。

The same issue occurs when using SchTasks.exe from a command prompt.从命令提示符使用 SchTasks.exe 时会出现同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM