8

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. 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'"

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.

UPDATE I have found the following script to get task list on 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
TylerH
  • 20,799
  • 66
  • 75
  • 101
mehran
  • 1,314
  • 4
  • 19
  • 33

3 Answers3

14

UAC

The result is likely affected by 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.

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

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
David Homer
  • 396
  • 2
  • 8
  • When I try to run the Get-ScheduledTask from a terminal opened with "Run As Administrator" I alwas get the error `Get-ScheduledTask: Catastrophic failure `. I get the error in Powershell 1 and 7 – Charlweed Oct 14 '22 at 16:39
  • Sounds like an issue with one of the Scheduled Tasks themselves. Do you get the error when running the task scheduler user MMC? I'd take a look at the following folder. c:\windows\system32\Tasks You likely have a corrupt task file in this folder. – David Homer Mar 14 '23 at 14:01
4

Have you tried using a com object? 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
TechSpud
  • 3,418
  • 1
  • 27
  • 35
  • the first one is not working with '.' and neither without it. with '.' it gets the following error _Exception calling "GetFolder" with "1" argument(s): "This operation is supported only when you are connected to the server"_ – mehran Feb 15 '17 at 22:56
  • the second one is getting the following error: _Exception calling "GetTask" with "1" argument(s): "Access is denied._ – mehran Feb 15 '17 at 22:56
  • Sorry, not at a windows machine now. Try running in an elevated PowerShell session. Also, for a local task, the syntax needs to be slightly different - see my edit. – TechSpud Feb 15 '17 at 23:16
  • Again with the lastest changes, the same error _Exception calling "GetFolder" with "1" argument(s): "This operation is supported only when you are connected to the server._ – mehran Feb 16 '17 at 00:45
  • Apologies, @mehran. You still need to connect, which I didn't test. See my latest edit. – TechSpud Feb 16 '17 at 07:21
  • I only get one task with `.GetTasks(0)`, and there are many others. – Ferdinand.kraft Jul 19 '18 at 03:51
2

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. Seeing them in the TaskSchd.Msc window indicates that program is running with different permissions.

Therefore, running a PowerShell prompt as administrator solves the problem.

The same issue occurs when using SchTasks.exe from a command prompt.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Robert B
  • 21
  • 3