question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PowerShell 7.1 -> 5.1 inconsistency with Get-ScheduledTask

See original GitHub issue

Steps to reproduce

Get-ScheduledTask

Expected behavior

I would expect the ‘state’ property to be the same across PowerShell 5.1 and PowerShell 7.

This would mean I am returned a state of (Disabled|Ready|Running).

Actual behavior

I am returned an integer, I assume this is an enum.

Environment data

PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 6.3.9600
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
jborean93commented, Jan 12, 2021

The issue here is most likely due the ScheduledTask module not being PowerShell Core compatible on that Windows build. In this case the module is loaded in an implicit remoting session that is connected to a Windows PowerShell process which is what is hosting the module. Because the ScheduledTask module isn’t loaded into the current session there is no special type data to convert the raw int value the MSFT_ScheduledTask returns.

You can see the State value for the CIM class that hosts this information is a signed integer ([int32]) so a raw WMI query will return an integer

$cimParams = @{
    ClassName = 'MSFT_ScheduledTask'
    Namespace = 'ROOT\Microsoft\Windows\TaskScheduler'
}
(Get-CimClass @cimParams).CimClassProperties |
    Where Name -eq State
Name               : State
Value              :
CimType            : SInt32
Flags              : Property, ReadOnly, NullValue
Qualifiers         : {read, ValueMap, Values}
ReferenceClassName :

Where the PowerShell magic comes in is that it has extended type data to make the data more readable

# Run this on Windows PowerShell
(Get-TypeData '*MSFT_ScheduledTask').Members.State
GetScriptBlock
--------------
[Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.StateEnum]($this.PSBase.CimInstanceProperties['State'…

This type data is automatically applied to any type that matches the type name (caveats do apply) so on Windows PowerShell the State property as exposed by PowerShell is automatically converted to an enum when it is accessed.

Going back to the point where the ScheduledTask module is run in an implicit remoting session. The module is now loaded in the Windows PowerShell session whereas your PS Core one does not have any type data. Therefore the State will continue to be the raw Int32 value. Newer Windows builds contain an updated ScheduledTask module which now loads on PS Core and therefore contain the type data to automatically expose the enum instead.

TLDR: The raw value from Get-ScheduledTask State is an Int32 but the module has extra type data so it’s converted to an enum when accessed. PS Core doesn’t load the module normally on that older Windows build (because it’s not compatible) so the type data is not present.

2reactions
jborean93commented, Jan 12, 2021

Just an FYI in the future the default bug report title is quite generic. It makes it easier to search for issues (especially historical ones) if the title reflects the actual bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scheduled tasks don't show up in Get-ScheduledTask result
To see everything try right clicking the PowerShell icon, select Run as Administrator and then run your Get-ScheduledTask command again and see ...
Read more >
Differences between Windows PowerShell 5.1 and ...
This article summarizes the differences and breaking changes from Windows PowerShell 5.1 and the current version of PowerShell that is based ...
Read more >
Scheduled Task with Powershell script Failing with PS ...
Hi all, I've noticed that a scheduled task running a powershell script doesn't work on version 5.1.19041.1682. But It does work with version ......
Read more >
Get-ScheduledTask -TaskName no longer works on my ...
Hoping someone knows how to make this work again. The command help for -Name is: -Name <Object> The name or name pattern of...
Read more >
In an elevated Powershell prompt, why does Get- ...
Type the command Get-ScheduledTask. The expected results are a list of all the scheduled tasks. My actual results are Get-ScheduledTask: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found