I've searched for a good answer to this, and found some things that got me close. What I ended up with is the following.
$taskName="SomeTask"
$serverName="yourserver"
$status = (schtasks.exe /query /tn "$taskName" /s $serverName /v /fo CSV | ConvertFrom-Csv | Select-Object -Property "Status").Status
If you want all of the items from the task, you can do something similar to the following...
$task= schtasks.exe /query /tn "$taskName" /s $serverName /v /fo CSV | ConvertFrom-Csv | Select-Object
Then you can select whichever property you want to work with...
$task.Status
$task."Last Result"
Remember to use quotes around the properties with spaces in them!
To clarify, my response is using Powershell when Get-ScheduledTask is not available, which it wouldn't be if running Windows 7, or Windows Server 2008. While one answer does provide a way to get the status, I feel like this method is easier to understand and work with. Even more so if someone might be interested in getting other properties of the scheduled task without having to parse which column the property happens to be in. In short, my answer is the solution that I was looking for, so I thought I would share it for other like minded individuals.
Tried this. It works good only for task without
space
in name. When there are spaces in task name, this does not work. Not yet tried, but It may work if I try to adjusttokens
values by counting spaces in output. – Sandeep – 2018-07-06T12:18:56.010@Sandeep Hmm. Tricky. There are some possible workaround in Windows Batch file count numbers of tokens - Stack Overflow
– DavidPostill – 2018-07-06T12:27:34.010Thank you, this works. Simply adjust the number of tokens if your scheduled task has spaces, e.g.
for /f "usebackq skip=3 tokens=7" %a in (\
schtasks.exe /query /tn "Refresh Dashboard Cache"`) do @echo %a` – wp78de – 2019-04-09T22:38:48.613@wp78de: no need to count tokens
– Stephan – 2019-08-24T11:49:05.570