Unable to resume a workflow via task scheduler

不羁岁月 提交于 2019-11-29 14:48:28

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!