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


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.


Get-ScheduledTask


$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





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
Jul 3 at 6:21


Get-ScheduledTask


$PSVersionTable





I do not want to upgrade the Powershell Version. Isn't there a way to filter just the taskname?
– beginner
Jul 3 at 6:27




1 Answer
1



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.


Get-ScheduledTask



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 way around it, is to use schtask with correct switches.


schtask



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


Microsoft


schtasks /tn MicrosoftServiceNow /query /fo LIST



The output:


Folder: Microsoft
HostName: XXXXXACL06
TaskName: MicrosoftServiceNow
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:


/v


schtasks /tn MicrosoftServiceNow /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):


findstr


Select-String


sls



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:


powershell


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



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


ServiceNow



TaskName: MicrosoftServiceNow


TaskName: MicrosoftServiceNow



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.


findstr


/I


select-string





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
Jul 3 at 6:33





@beginner I see. I'll edit the answer and add this option.
– tukan
Jul 3 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
Jul 3 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
Jul 4 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
Jul 4 at 9:04



powershell


findstr


/I


findstr /?






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

iOS Top Alignment constraint based on screen (superview) height