{"id":1891,"date":"2013-12-23T15:19:58","date_gmt":"2013-12-23T15:19:58","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2013\/12\/23\/automatically-resuming-windows-powershell-workflow-jobs-at-logon\/"},"modified":"2019-02-18T13:05:26","modified_gmt":"2019-02-18T20:05:26","slug":"automatically-resuming-windows-powershell-workflow-jobs-at-logon","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/automatically-resuming-windows-powershell-workflow-jobs-at-logon\/","title":{"rendered":"Automatically resuming Windows PowerShell Workflow jobs at logon"},"content":{"rendered":"\n<p class=\"MsoNormal\" align=\"justify\"><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/jj134242.aspx\"><span style=\"color: windowtext\">PowerShell Workflow<\/span><\/a> helps IT Pros automate the long running tasks, or workflows, that can affect multiple managed computers or devices at the same time. Windows PowerShell Workflow is designed to be robust and recoverable. The features that are built into Windows PowerShell allow you to restart the target computers and, if the workflow is not yet complete, to reconnect to the target computers automatically and continue with the workflow commands. You can even restart the computer that is running a workflow, which allows you to run the workflow on the local computer, or include the local computer in the list of target computers. <span lang=\"EN\">As you might know, a PowerShell workflow job is suspended when the local machine restarts as part of the workflow job <\/span>execution, among other reasons, like user-initiated reboot. In<span lang=\"EN\"> this case, IT Pros need to resume the suspended jobs manually after machine restart. <\/span><\/p>\n<p style=\"text-align: justify\" class=\"MsoNormal\"><span lang=\"EN\">This blog post shows how one can automatically resume the suspended workflow jobs at user logon using <\/span><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/jj649816.aspx\"><span style=\"color: windowtext\" lang=\"EN\">PowerShell Scheduled Tasks cmdlets<\/span><\/a><span lang=\"EN\">.<\/span><\/p>\n<p class=\"MsoNormal\">For reference: In PowerShell Workflow, every user gets a unique workflow job store path based on <\/p>\n<ul>\n<li>Endpoint or console <\/li>\n<li>User context <\/li>\n<li>Authentication mechanism <\/li>\n<li>Elevated or non-elevated <\/li>\n<li>Interactive or non-interactive <\/li>\n<\/ul>\n<p style=\"text-align: justify\" class=\"MsoNormal\">Resuming workflow jobs works only when the store path is same in both the start and resume operations.<\/p>\n<p class=\"MsoNormal\">For example: You must connect to the workflow session to resume the workflow jobs created and suspended in a workflow session. <\/p>\n<p style=\"text-align: justify\" class=\"MsoNormal\">Workflow jobs started in an interactive PowerShell console are not visible in non-interactive PowerShell consoles. Because of this, scheduling a task to run at machine startup cannot find suspended jobs from a non-interactive console. Scheduled tasks cannot find the WF jobs to resume, unless you are logged on to an interactive session.<\/p>\n<p class=\"MsoNormal\">&#160;<\/p>\n<p class=\"MsoNormal\">Use the following example to resume the suspended workflow jobs automatically at user logon. <\/p>\n<p style=\"text-indent: -0.25in;margin-left: 0.25in\" class=\"MsoListParagraph\"><b>1.<span>&#160;&#160;&#160;&#160;&#160;&#160; <\/span><\/b><b>Define the workflow and create a startup script.<\/b><\/p>\n<p style=\"text-indent: -0.25in;margin-left: 0.25in\" class=\"MsoListParagraph\"><span style=\"color: #44546a\">&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>PS C:\\windows\\system32&gt; Get-Content c:\\AutoResumeWFJob\\Start-WFJob.ps1<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; Remove-Item C:\\AutoResumeWFJob\\beforeSuspend.txt -ErrorAction SilentlyContinue<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; Remove-Item C:\\AutoResumeWFJob\\afterResume.txt -ErrorAction SilentlyContinue<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; workflow test-restart <\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; {<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160;&#160; InlineScript {Get-Date | Out-File -FilePath C:\\AutoResumeWFJob\\beforeSuspend.txt}<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160;&#160; # Suspend using Restart-Computer activity or Suspend activity<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160;&#160; Restart-Computer -Wait<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160;&#160; InlineScript {Get-Date | Out-File -FilePath C:\\AutoResumeWFJob\\afterResume.txt}<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; }<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; $job = test-restart -asjob<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; Wait-job $job<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>PS C:\\windows\\system32&gt;&#160; <\/span><\/p>\n<p style=\"background: white;margin-left: 0.25in\" class=\"MsoNormal\"><span style=\"color: #44546a\">&#160;<\/span><\/p>\n<p style=\"text-indent: -0.25in;margin-left: 0.25in\" class=\"MsoListParagraph\"><b>2.<span>&#160;&#160;&#160;&#160;&#160;&#160; <\/span><\/b><b>Create the resume job script.<\/b><\/p>\n<p style=\"text-indent: -0.25in;margin-left: 0.25in\" class=\"MsoListParagraph\"><span style=\"color: #44546a\">&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>PS C:\\windows\\system32&gt; Get-Content c:\\AutoResumeWFJob\\Resume-WFJob.ps1<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; Import-Module \u2013Name PSWorkflow<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; $jobs = Get-Job -state Suspended<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; $resumedJobs = $jobs | resume-job -wait<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;&#160;&#160; $resumedJobs | wait-job<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>&#160;<\/span><\/p>\n<p style=\"background: #012456\" class=\"MsoNormal\"><span>PS C:\\windows\\system32&gt;&#160; <\/span><\/p>\n<p style=\"background: white;margin-left: 0.25in\" class=\"MsoNormal\"><span style=\"color: #44546a\">&#160;<\/span><\/p>\n<p style=\"text-indent: -0.25in;margin-left: 0.25in\" class=\"MsoListParagraph\"><b>3.<span>&#160;&#160;&#160;&#160;&#160;&#160; <\/span><\/b><b>Register a scheduled task to resume the workflow job at user logon.<\/b><\/p>\n<p style=\"background: white;margin-left: 0.25in\" class=\"MsoNormal\"><span style=\"color: #44546a\">&#160;<\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>$resumeActionscript<\/span><span> <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8216;-WindowStyle Normal -NoLogo -NoProfile -File &quot;c:\\AutoResumeWFJob\\Resume-WFJob.ps1&quot;&#8217;<\/span><\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>Get-ScheduledTask<\/span><span> <span style=\"color: navy\">-TaskName<\/span> <span style=\"color: blueviolet\">ResumeWFJobTask<\/span> <span style=\"color: navy\">\u2013EA<\/span> <span style=\"color: blueviolet\">SilentlyContinue<\/span> <span style=\"color: darkgray\">|<\/span> <span style=\"color: blue\">Unregister-ScheduledTask<\/span> <span style=\"color: navy\">-Confirm:<\/span><span style=\"color: orangered\">$false<\/span><\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>$act<\/span><span> <span style=\"color: darkgray\">=<\/span> <span style=\"color: blue\">New-ScheduledTaskAction<\/span> <span style=\"color: navy\">-Execute<\/span> <span style=\"color: darkred\">&quot;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe&quot;<\/span> <span style=\"color: navy\">-Argument<\/span> <span style=\"color: orangered\">$resumeActionscript<\/span><\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>$trig<\/span><span> <span style=\"color: darkgray\">=<\/span> <span style=\"color: blue\">New-ScheduledTaskTrigger<\/span> <span style=\"color: navy\">-AtLogOn<\/span> <span style=\"color: navy\">-RandomDelay<\/span> <span style=\"color: blueviolet\">00:00:55<\/span><\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>Register-ScheduledTask<\/span><span> <span style=\"color: navy\">-TaskName<\/span> <span style=\"color: blueviolet\">ResumeWFJobTask<\/span> <span style=\"color: navy\">-Action<\/span> <span style=\"color: orangered\">$act<\/span> <span style=\"color: navy\">-Trigger<\/span> <span style=\"color: orangered\">$trig<\/span> <span style=\"color: navy\">-RunLevel<\/span> <span style=\"color: blueviolet\">Highest<\/span><\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span style=\"color: #44546a\">&#160;<\/span><\/p>\n<p style=\"text-indent: -0.25in;margin-left: 0.25in\" class=\"MsoListParagraph\"><b>4.<span>&#160;&#160;&#160;&#160;&#160;&#160; <\/span><\/b><b>Start the workflow, which restarts the local machine.<\/b><span style=\"color: #44546a\">&#160;<\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe<\/span><span> <span style=\"color: navy\">-WindowStyle<\/span> <span style=\"color: blueviolet\">Normal<\/span> <span style=\"color: navy\">-NoLogo<\/span> <span style=\"color: navy\">-NoProfile<\/span> <span style=\"color: navy\">-NoExit<\/span> <span style=\"color: navy\">-File<\/span> <span style=\"color: darkred\">&quot;c:\\AutoResumeWFJob\\Start-WFJob.ps1&quot;<\/span><\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span style=\"color: #44546a\">&#160;<\/span><\/p>\n<p style=\"text-indent: -0.25in;margin-left: 0.25in\" class=\"MsoListParagraph\"><b>5.<span>&#160;&#160;&#160;&#160;&#160;&#160; <\/span><\/b><b>Verify the suspended workflow was resumed after logon, by looking for afterResume.txt.<\/b><\/p>\n<p style=\"text-indent: -0.25in;margin-left: 0.25in\" class=\"MsoListParagraph\"><span style=\"color: #44546a\">&#160;<\/span><span># Wait for the registered scheduled task to be finished after logon<\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span># I waited for 60 sec<\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>Sleep<\/span><span> <span style=\"color: purple\">60<\/span><\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>dir<\/span><span> <span style=\"color: blueviolet\">c:\\autoresumewfjob\\afterResume.txt<\/span><\/span><span>&#160;<\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>Get-Content<\/span><span> <span style=\"color: blueviolet\">c:\\autoresumewfjob\\afterResume.txt<\/span><\/span><span>&#160;<\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span># Check the workflow job in completed state<\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>Import-Module PSWorkflow<\/span><\/p>\n<p style=\"background: white\" class=\"MsoNormal\"><span>Get-Job<\/span><\/p>\n<p class=\"MsoNormal\">&#160;<\/p>\n<p style=\"text-align: justify\" class=\"MsoNormal\">As you see in this example, automatically resuming suspended workflow jobs works by scheduling a task to run at user logon. <\/p>\n<p class=\"MsoNormal\">&#160;<\/p>\n<p class=\"MsoNormal\">Manikyam Bavandla [MSFT]<\/p>\n<p class=\"MsoNormal\">Windows PowerShell Development<\/p>\n<p class=\"MsoNormal\"><span style=\"color: #44546a\">&#160;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell Workflow helps IT Pros automate the long running tasks, or workflows, that can affect multiple managed computers or devices at the same time. Windows PowerShell Workflow is designed to be robust and recoverable. The features that are built into Windows PowerShell allow you to restart the target computers and, if the workflow is not [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>PowerShell Workflow helps IT Pros automate the long running tasks, or workflows, that can affect multiple managed computers or devices at the same time. Windows PowerShell Workflow is designed to be robust and recoverable. The features that are built into Windows PowerShell allow you to restart the target computers and, if the workflow is not [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/1891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=1891"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/1891\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=1891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=1891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=1891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}