December 23rd, 2013

Automatically resuming Windows PowerShell Workflow jobs at logon

PowerShell Team
PowerShell Team

PowerShell Workflow helps IT Pros automate the long running tasks, or workflows, that can affect multiple managed computers or devices at the same time. Windows PowerShell Workflow is designed to be robust and recoverable. The features that are built into Windows PowerShell allow you to restart the target computers and, if the workflow is not yet complete, to reconnect to the target computers automatically and continue with the workflow commands. You can even restart the computer that is running a workflow, which allows you to run the workflow on the local computer, or include the local computer in the list of target computers. As you might know, a PowerShell workflow job is suspended when the local machine restarts as part of the workflow job execution, among other reasons, like user-initiated reboot. In this case, IT Pros need to resume the suspended jobs manually after machine restart.

This blog post shows how one can automatically resume the suspended workflow jobs at user logon using PowerShell Scheduled Tasks cmdlets.

For reference: In PowerShell Workflow, every user gets a unique workflow job store path based on

  • Endpoint or console
  • User context
  • Authentication mechanism
  • Elevated or non-elevated
  • Interactive or non-interactive

Resuming workflow jobs works only when the store path is same in both the start and resume operations.

For example: You must connect to the workflow session to resume the workflow jobs created and suspended in a workflow session.

Workflow jobs started in an interactive PowerShell console are not visible in non-interactive PowerShell consoles. Because of this, scheduling a task to run at machine startup cannot find suspended jobs from a non-interactive console. Scheduled tasks cannot find the WF jobs to resume, unless you are logged on to an interactive session.

 

Use the following example to resume the suspended workflow jobs automatically at user logon.

1.       Define the workflow and create a startup script.

 

PS C:\windows\system32> Get-Content c:\AutoResumeWFJob\Start-WFJob.ps1

 

    Remove-Item C:\AutoResumeWFJob\beforeSuspend.txt -ErrorAction SilentlyContinue

    Remove-Item C:\AutoResumeWFJob\afterResume.txt -ErrorAction SilentlyContinue

 

    workflow test-restart

    {

     InlineScript {Get-Date | Out-File -FilePath C:\AutoResumeWFJob\beforeSuspend.txt}

 

     # Suspend using Restart-Computer activity or Suspend activity

     Restart-Computer -Wait

 

     InlineScript {Get-Date | Out-File -FilePath C:\AutoResumeWFJob\afterResume.txt}

    }

 

    $job = test-restart -asjob

 

    Wait-job $job

 

PS C:\windows\system32> 

 

2.       Create the resume job script.

 

PS C:\windows\system32> Get-Content c:\AutoResumeWFJob\Resume-WFJob.ps1

 

    Import-Module –Name PSWorkflow

    $jobs = Get-Job -state Suspended

    $resumedJobs = $jobs | resume-job -wait

    $resumedJobs | wait-job

 

PS C:\windows\system32> 

 

3.       Register a scheduled task to resume the workflow job at user logon.

 

$resumeActionscript = ‘-WindowStyle Normal -NoLogo -NoProfile -File "c:\AutoResumeWFJob\Resume-WFJob.ps1"’

Get-ScheduledTask -TaskName ResumeWFJobTask –EA SilentlyContinue | Unregister-ScheduledTask -Confirm:$false

$act = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument $resumeActionscript

$trig = New-ScheduledTaskTrigger -AtLogOn -RandomDelay 00:00:55

Register-ScheduledTask -TaskName ResumeWFJobTask -Action $act -Trigger $trig -RunLevel Highest

 

4.       Start the workflow, which restarts the local machine. 

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Normal -NoLogo -NoProfile -NoExit -File "c:\AutoResumeWFJob\Start-WFJob.ps1"

 

5.       Verify the suspended workflow was resumed after logon, by looking for afterResume.txt.

 # Wait for the registered scheduled task to be finished after logon

# I waited for 60 sec

Sleep 60

dir c:\autoresumewfjob\afterResume.txt 

Get-Content c:\autoresumewfjob\afterResume.txt 

# Check the workflow job in completed state

Import-Module PSWorkflow

Get-Job

 

As you see in this example, automatically resuming suspended workflow jobs works by scheduling a task to run at user logon.

 

Manikyam Bavandla [MSFT]

Windows PowerShell Development

 

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.