5

I am trying to find a scheduled task with having certain pattern in it's name, in my case the scheduled task should have this pattern "ServiceNow" in its name. For this I am using Get-ScheduledTask command, but this command fails in Windows Server 2008 R2.

$taskPattern = "ServiceNow"
$taskDetails = Get-ScheduledTask | Where-Object {$_.TaskName -match $taskPattern  }
$taskName = $taskDetails.TaskName

This script fails with error :

"Get-ScheduledTask : The term 'Get-ScheduledTask' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

Then I tried this approach which gives me the "(taskname Date&Time State)" but I don't know how to only extract the taskname from it's output.

$taskPattern = "ServiceNow" 
$taskExists = schtasks /query | select-string -patt $taskPattern 
$taskExists

Can anyone please tell me how to make it work in Windows Server 2008 R2 by only extracting the actual taskname? Thanks in Advance

2
  • Windows Server 2008 R2 does support PS Version 5. Please try to upgrade the powershell, maybe that allows you to run Get-ScheduledTask. You can check your currently installed version with $PSVersionTable.
    – Paxz
    Commented Jul 3, 2018 at 6:21
  • I do not want to upgrade the Powershell Version. Isn't there a way to filter just the taskname?
    – beginner
    Commented Jul 3, 2018 at 6:27

2 Answers 2

16

You can't get Get-ScheduledTask at windows 2008 R2 as it was introduced for Windows 2012 and nobody backported it. For more see the link at github.

The comment that describes the situation the best is:

Hi @dronkoff - This module is supported on PS 4.0. However, the problem isn't in the version of PowerShell. The issue is that the ScheduledTasks module, which this resource depends on, is not built into Windows Server 2008R2. It was first included in Windows Server 2012.

This is the case with many of the PowerShell modules: They are missing from older versions of Windows (Networking for example). In the odd occasion there is an easy work around. However in this case converting the resource over to use schtasks.exe is not possible without a complete rewrite and would probably result in something really unstable and buggy (not to mention the problems that would arise with different localized versions of schtasks). I really can't see this happening unfortunately (unless someone else from the community has some ideas on how to do this).

But you are correct, this should be mentioned that Windows Server 2008R2 is not currently supported.

The workaround

The way around it, is to use schtask with correct switches.

If you have it in the Microsoft folder as I have it in the example you just need to run:

schtasks /tn \Microsoft\ServiceNow /query /fo LIST

The output:

Folder: \Microsoft
HostName:      XXXXXACL06
TaskName:      \Microsoft\ServiceNow
Next Run Time: 7/4/2018 8:16:22 AM
Status:        Ready
Logon Mode:    Interactive only

If you want more details you can use the /v (verbose) switch:

schtasks /tn \Microsoft\ServiceNow /query /v > service_now_details.txt

Edit - to find a pattern

If you have only pattern to search you have to use some additional tool to search the string. The windows natively supports findstr (or Select-String (short sls) in powershell):

To find the task name you can use then:

schtasks /query /fo LIST | findstr "ServiceNow"

OR even with wild charter

schtasks /query /fo LIST | findstr "ServiceNo*"

OR the powershell way:

schtasks /query /fo LIST | sls 'ServiceNo*'

The output in all cases will be something like this (since my task is named exactly ServiceNow):

TaskName: \Microsoft\ServiceNow

Edit2 case sensitivity

If you are searching for case insensitive string then: for findstr you have to add /I to make it insensitive. The powershell's select-string (sls) is naturally insensitive.

5
  • The task name is not exactly ServiceNow. It's name conatins this pattern "ServiceNow". I want to find out full name of this task
    – beginner
    Commented Jul 3, 2018 at 6:33
  • 1
    @beginner I see. I'll edit the answer and add this option.
    – tukan
    Commented Jul 3, 2018 at 6:37
  • @beginner please don't forget to accept the answer when it answers your questions (upvote is nice to :) ) - stackoverflow.com/help/someone-answers
    – tukan
    Commented Jul 3, 2018 at 7:02
  • How to make this query for case-insensitive cases? Suppose the task name is "servicenow" and not "ServiceNow", then how can we check for any insensitive cases? It is giving error because of case-sensitivity
    – beginner
    Commented Jul 4, 2018 at 8:49
  • @beginner the powershell version is case insensitive (you actually have to add a switch to make it case sensitive). As for findstr you have to add /I to make it case-insenstivie (check findstr /? for all switches).
    – tukan
    Commented Jul 4, 2018 at 9:04
2

There's a great script on GitHub, Get-ScheduledTasks (note the S on the end of the service name). This works on older versions of Windows going back to Windows Server 2003 / Windows XP.

Thanks to the amazing Warren F (aka Rambling Cookie Monster) for creating & sharing this.

Not the answer you're looking for? Browse other questions tagged or ask your own question.