Question:
I’m trying to get a list of all scheduled tasks on a server within a certian path that don’t have a LastTaskResult
of ‘0’. Just displaying all failures.
Simple bit of code I thought but it’s not returning as expected, my code is:
1 2 3 4 |
$var = Get-ScheduledTask | Get-ScheduledTaskInfo | select * | where {$_.TaskPath -eq "\"} $var = $var | select * | where {$_.LastTaskResult -ne 0} $var.count |
If I remove the second line I can see that there is one value returned but the count is returning nothing (not even 0)
If I run the same but using -eq then it gives me a count of 2 which is correct, anyone come across this issue before?
I’ve had a look around but I can;t see anything.
(Just a note, I’m very new to Powershell)
Answer:
You are correct, Count
is not always reliable for every situation. Especially not in the older versions of PowerShell.
Please use the following CmdLet:
1 2 |
$var | Measure-Object |
This can be enhanced to:
1 2 3 4 |
if (($var | Measure-Object).Count -ne 0) { 'We found stuff' } |
More information on this CmdLet can be found when typing:
1 2 |
Get-Help Measure-Command -ShowWindow |
As in the answer of @Lieven, you can also force your result to be an
Array
:
1 2 3 |
@($var).count $var = @(Get-Process | Select-Object -First 0); $var.count |