Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-ScheduledTask command does not work in Windows Server 2008 R2

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

like image 891
beginner Avatar asked Jul 03 '18 06:07

beginner


2 Answers

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.

like image 89
tukan Avatar answered Oct 19 '22 03:10

tukan


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.

like image 2
JohnLBevan Avatar answered Oct 19 '22 03:10

JohnLBevan