无法通过任务计划程序恢复工作流 [英] Unable to resume a workflow via task scheduler

查看:25
本文介绍了无法通过任务计划程序恢复工作流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 powershell 窗口中,我运行以下工作流程:

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'"

工作流不会在启动时恢复,也不会在我通过任务计划程序手动触发时恢复.zqqfff.txt 的内容表示任务调度器激活的powershell 看不到工作流.当我运行 Get-Job 时,常规的 powershell 窗口可以看到工作流程.

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.

(普通 powershell 窗口和任务调度程序 powershell 实例都以同一用户身份运行.)

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

我使用 procmon 来查看发生了什么,从中我可以看出,当 powershell 通常与 taskscheduler 相比时,它正在查看不同的工作流持久性路径,即:

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(普通的powershell窗口使用这个文件夹)C:\Users\weijgerss\AppData\Local\microsoft\windows\PowerShell\WF\PS\default\S-1-5-21-3519956147-933941082-741972881-500_EL_NI(任务调度器激活的powershell实例使用这个文件夹)

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)

我完全被难住了.如何让任务调度程序激活的 powershell 实例看到与普通 powershell 窗口相同的工作流?

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

推荐答案

以下脚本为您提供了一个解决方案,可在系统启动时使用任务调度程序在重启/崩溃后自动恢复 powershell 工作流程:

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:(下面的第一行修复了问题中提到的 _NI 问题)

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:(解决 Windows 8/server 2012 任务调度程序错误)

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'"

为了把它们放在一起,使用以下 powershell 脚本来控制 resume-workflows.cmd 在系统启动时运行:

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")

享受吧!

(ILSpy、sysinternal 的 procmon、大量的 google 和少量的 Windbg 都有助于为您提供上述答案)

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

这篇关于无法通过任务计划程序恢复工作流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆