Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
984 views
in Technique[技术] by (71.8m points)

powershell - 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:usersweijgerssdesktopfoo.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:usersweijgerssdesktopzqqfff.txt
$resumedJobs = $jobs | resume-job -wait
$resumedJobs | wait-job

# Task scheduler action: C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -WindowStyle Normal -NoLogo -NoProfile -Command "&'c:usersweijgerssdesktop
esume.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:UsersweijgerssAppDataLocalmicrosoftwindowsPowerShellWFPSdefaultS-1-5-21-3519956147-933941082-741972881-500_EL (a normal powershell window uses this folder) C:UsersweijgerssAppDataLocalmicrosoftwindowsPowerShellWFPSdefaultS-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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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%AppDataRoaming"
set "LOCALAPPDATA=%USERPROFILE%AppDataLocal"
"C:WindowsSystem32WindowsPowerShellv1.0powershell.exe" -NonInteractive -WindowStyle Normal -NoLogo -NoProfile -Command "&'c:patho
esume-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:patho
esume-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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...