Job Triggers and JobStatus Enum not working as expected

Did something change with the Job object that is sent on triggers?

I had a conditional, similar to the following, that suddenly stopped working: ($Job.Status -eq 'Completed')? 'True' : 'False')

I found out that it seems to have to do with the JobStatus enum becoming Deserialized.PowershellUniversal.JobStatus, if I instead use the integer value of the Enum, it works fine.

A test trigger script that causes the issue looks like this:

param($Job)

@"
    Job Status: $($Job.Status)
    Job Status TypeNames: $($Job.Status.psobject.Typenames)
    Job Status -eq 'Completed': $($Job.Status -eq 'Completed')
    Job Status -eq 2: $($Job.Status -eq 2)
    (Direct Cast to [PowershellUniversal.JobStatus]) Job Status -eq 'Completed': $([PowerShellUniversal.JobStatus]$Job.Status -eq 'Completed')
    Switch Job Status: $(switch ($Job.Status) {
        'Completed' { 'Branched on Completed' }
        Default {'Default Branch'}
    })
"@

Output is:

[information]     Job Status: 2
    Job Status TypeNames: Deserialized.PowerShellUniversal.JobStatus Deserialized.System.Enum Deserialized.System.ValueType Deserialized.System.Object
    Job Status -eq 'Completed': False
    Job Status -eq 2: True
    (Direct Cast to [PowershellUniversal.JobStatus]) Job Status -eq 'Completed': True
    Switch Job Status: Default Branch 

I did fix it by direct casting, but it seems to have happened after the update to 3.7 (maybe 3.7.7 specifically?)

Product: PowerShell Universal
Version: 3.7.7

This may have been because we were originally only serializing a single depth for the metadata for triggers. If I had to venture a guess, the JobStatus was being serialized to a string at the depth of 1 but now that we’ve increased the depth, it is not becoming an enum.

I have to admit, I’m a bit surprised you can’t compare with a string as that seems like a regular PowerShell thing. The reason this change was made was because some of the trigger meta data objects were more complex than we were serializing for.

1 Like

Some helpful Powershell gurus pointed out that serializing and deserializing converts enums into a plain object, so basically it is converted into the base type (integer), it just also contains metadata so that Powershell typenames are preserved.

Basically I should just use the enum in my equality to maintain readability and functionality

$Job.Status -eq [PowerShellUniversal.JobStatus]::Completed

1 Like