Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to resume a workflow via task scheduler

In a powershell window I run the following workflow:

workflow foo { Suspend-Workflow; "hello world" | Out-File c:\users\weijgerss\desktop\foo.txt }

Then to resume the workflow, I have the following scheduled via task scheduler triggered to run at startup:

Import-Module PSWorkflow
$jobs = Get-Job -state Suspended
$jobs > c:\users\weijgerss\desktop\zqqfff.txt
$resumedJobs = $jobs | resume-job -wait
$resumedJobs | wait-job

# Task scheduler action: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Normal -NoLogo -NoProfile -Command "&'c:\users\weijgerss\desktop\resume.ps1'"

The workflow does not get resumed neither at startup, nor if I manually trigger it via Task Scheduler. The contents of zqqfff.txt indicates that the task scheduler activated powershell cannot see the workflow. A regular powershell window can see the workflow when I run Get-Job.

(Both the normal powershell window and the task scheduler powershell instance run as same user.)

I used procmon to see what's going on and I can see from this that when powershell normally vs taskscheduler it's looking at different workflow persistence paths, namely:

C:\Users\weijgerss\AppData\Local\microsoft\windows\PowerShell\WF\PS\default\S-1-5-21-3519956147-933941082-741972881-500_EL (a normal powershell window uses this folder) C:\Users\weijgerss\AppData\Local\microsoft\windows\PowerShell\WF\PS\default\S-1-5-21-3519956147-933941082-741972881-500_EL_NI (a task scheduler activated powershell instance uses this folder)

I'm totally stumped. How can I get a task scheduler activated powershell instance to see the same workflows as normal powershell window can?

like image 706
simonweijgers Avatar asked Jun 24 '15 20:06

simonweijgers


People also ask

How do I manually trigger a scheduled task?

Go to the Scheduled Tasks applet in Control Panel, right-click the task you want to start immediately, and select Run from the displayed context menu.

What is a PowerShell workflow?

Windows PowerShell Workflows A workflow is a sequence of programmed, connected steps that perform long-running tasks or require the coordination of multiple steps across multiple devices or managed nodes.

How do I edit scheduled tasks?

On the ColdFusion Administrator, click Server Settings > Scheduled Tasks, and click Schedule New Task.


1 Answers

The below scripts give you a solution that automatically resumes powershell workflows after a reboot/crash using task scheduler at system start up:

resume-workflows.ps1: (the first line below fixes the _NI issue mentioned in the question)

[System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager = $true
Import-Module PSWorkflow
Get-Job -State Suspended | Resume-Job -Wait| Wait-Job

resume-workflows.cmd: (works around a windows 8/server 2012 task scheduler bug)

@rem This is a workaround for task scheduler bug 
@rem See: http://support.microsoft.com/kb/2968540
set "USERPROFILE=%USERPROFILE%\..\%USERNAME%"
set "APPDATA=%USERPROFILE%\AppData\Roaming"
set "LOCALAPPDATA=%USERPROFILE%\AppData\Local"
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NonInteractive -WindowStyle Normal -NoLogo -NoProfile -Command "&'c:\path\to\resume-workflows.ps1'"

To put it all together use the following powershell script to shedule resume-workflows.cmd to run at system start up:

$trigger = New-ScheduledTaskTrigger -AtStartup
$action = New-ScheduledTaskAction -Execute "c:\path\to\resume-workflows.cmd" 
$currentuser = ([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
Register-ScheduledTask -TaskName "Resume $($currentuser.Replace('\', '-'))'s Powershell Workflows" `
    -Trigger $trigger -Action $action -RunLevel Highest -User $currentuser `
    -Password (Read-Host "Enter password for $currentuser")

Enjoy!

(ILSpy, sysinternal's procmon, plenty of google and a dash of windbg were all instrumental in bringing the above answer to you)

like image 82
simonweijgers Avatar answered Oct 07 '22 12:10

simonweijgers