Powershell Workflow - Resume Suspended Job in Visible Window

TypeKG 0 Reputation points
2023-02-16T18:20:59.15+00:00

I've put together a powershell workflow script that runs on the local machine and calls some other scripts. The other scripts include write-output messages to let operators know what's happening on the system.

This is important because if an operator uses the computer while the workflow is running, things might not work properly. It's also helpful in debugging the scripts if something goes wrong.

Some of the scripts reboot the computer and suspend the workflow. I have a scheduled task to restart the suspended workflow after the computer logs back in.

The problem is that when this happens the workflow resumes running in the background and the write-output messages are no longer visible on screen.

Here's my code to schedule the workflow restart:

# Set up a job trigger to go off 15-20 seconds after login
$delayTime = New-TimeSpan -Start 00:0:15 -End 00:0:20
$AtLogOn = New-JobTrigger -AtLogOn -RandomDelay $delayTime

# Set up the task action to start Powershell and run this code
$resumeScript = 'Get-Job -State Suspended | Resume-Job; pause'
$action = New-ScheduledTaskAction -Execute powershell -Argument $resumeScript

# Actually registering the scheduled task with the above inputs
Register-ScheduledTask -TaskName ResumeSetup -Trigger $AtLogOn -Action $action -RunLevel Highest

How can I have Powershell resume a workflow in an open powershell window so the write-output messages are visible?

As a side note, the Scheduled Task does resume in the foreground, but the task is just running some powershell code that restarts the suspended workflow. The workflow doesn't actually run in the powershell window created by the scheduled task.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,776 Reputation points
    2023-02-17T12:45:02.4733333+00:00

    Hi,

    Thank you for posting your query.

    Kindly follow the steps provided below to resolve your query

    Describes the Suspend-Workflow activity, which suspends the workflow in which the activity appears.

    Long description

    The Suspend-Workflow activity temporarily stops workflow processing from within the workflow. Before suspending, Windows PowerShell Workflow takes a checkpoint so the workflow's state and data are preserved and the workflow can resume from the suspension point.

    To resume the workflow, the user running the workflow uses the Resume-Job cmdlet. You can't resume a workflow from within the workflow.

    Go to this link for your reference and additional troubleshooting procedures https://learn.microsoft.com/en-us/powershell/module/psworkflow/about/about_suspend-workflow

    If the answer is helpful kindly click "ACCEPT AS ANSWER" and up vote it.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.