0

tl;dr Entering a new powershell session seems to create (or reveal) a copy PSWorkflowJob. Explain?


As seen in the elevated code below, I create a simple Workflow and run it as a job. While it is suspended, I enter into a new PS session and the PSWorkflowJob is there but it is not the same one. A second workflow job was created without me explicitly wanting it. In fact if I go on to open powershell from the start menu, I will see that same second job.

When I exit back into the original powershell, I can once again see the original job.

I understand that Get-Job only shows the jobs started by current session but in that case, it seems a clone of the PSWorkflowJob was created. A Remove-Job needs to be done for both to clear them, so there are definitely two.

Can anyone explain this behaviour? Is it an unintended effect, or does it have a useful purpose?

PS C:\> Workflow New-Workflow { Suspend-Workflow }
PS C:\> New-Workflow -AsJob
Id     Name            PSJobTypeName   State         HasMoreData     Location
--     ----            -------------   -----         -----------     --------
1      Job1            PSWorkflowJob   Running       True            localhost            

PS C:\> Get-Job

Id     Name            PSJobTypeName   State         HasMoreData     Location
--     ----            -------------   -----         -----------     --------
1      Job1            PSWorkflowJob   Suspended     True            localhost

PS C:\> powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\> Get-Job

Id     Name            PSJobTypeName   State         HasMoreData     Location
--     ----            -------------   -----         -----------     --------
2      Job1            PSWorkflowJob   Suspended     True            localhost

PS C:\> exit
PS C:\> Get-Job

Id     Name            PSJobTypeName   State         HasMoreData     Location
--     ----            -------------   -----         -----------     --------
1      Job1            PSWorkflowJob   Suspended     True            localhost

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.3031
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.3031
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Happens in Windows 10 and 11

My original aim was to Resume-Job a Workflow after a reboot by using a ScheduledTask. And I can do that, thanks to the example in the MS docs but I came across this puzzling behaviour

xenull
  • 83
  • 1
  • 6

0 Answers0