sim*_*ers 4 powershell powershell-workflow
在PowerShell窗口中,我运行以下工作流程:
workflow foo { Suspend-Workflow; "hello world" | Out-File c:\users\weijgerss\desktop\foo.txt }
Run Code Online (Sandbox Code Playgroud)
然后,为了恢复工作流程,我通过触发在启动时运行的任务调度程序进行了以下调度:
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'"
Run Code Online (Sandbox Code Playgroud)
工作流程既不会在启动时恢复,也不会通过任务计划程序手动触发它.zqqfff.txt的内容表明任务调度程序激活的PowerShell无法查看工作流.当我运行Get-Job时,常规的PowerShell窗口可以看到工作流程.
(正常的PowerShell窗口和任务调度程序powershell实例都以同一用户身份运行.)
我使用procmon看看发生了什么,我可以从中看到,当powershell正常与taskscheduler时,它正在查看不同的工作流持久性路径,即:
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实例使用此文件夹)
我完全难过了.如何让任务调度程序激活PowerShell实例以查看与普通PowerShell窗口相同的工作流程?
以下脚本为您提供了一个解决方案,可在系统启动时使用任务计划程序在重新启动/崩溃后自动恢复PowerShell工作流:
resume-workflows.ps1 :(下面的第一行修复了问题中提到的_NI问题)
[System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager = $true
Import-Module PSWorkflow
Get-Job -State Suspended | Resume-Job -Wait| Wait-Job
Run Code Online (Sandbox Code Playgroud)
resume-workflows.cmd :(解决windows 8/server 2012任务调度程序错误)
@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'"
Run Code Online (Sandbox Code Playgroud)
要将它们放在一起,请使用以下powershell脚本来简化resume-workflows.cmd以在系统启动时运行:
$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")
Run Code Online (Sandbox Code Playgroud)
请享用!
(ILSpy,sysinternal的procmon,大量的google和一些windbg都有助于为你提供上述答案)
| 归档时间: |
|
| 查看次数: |
4014 次 |
| 最近记录: |