Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, April 26, 2019 12:06 PM
Hello,
I'm trying to resume my workflow automatically after reboot by means of the scheduled job according to this article:
I write the following code:
workflow test-restart {
Get-WmiObject -Class Win32_ComputerSystem | Out-File -FilePath C:\Reportscomp.txt
Restart-Computer -Wait
"hello" | Out-File -FilePath C:\Reportsos.txt
Unregister-ScheduledJob -Name Resumeworkflow
}
$AtStartup = New-JobTrigger -AtStartup
Register-ScheduledJob -Name ResumeWorkflow -Trigger $AtStartup -ScriptBlock {Import-Module PSWorkflow; Get-Job TestRestart -State Suspended | Resume-Job}
test-restart -JobName TestRestart
But only the part before the reboot is being done. Then when I check the jobs I see my job "TestRestart" in "suspended" mode rather than in "Completed" as It should be. What am I doing wrong? Could you please help me?
All replies (20)
Friday, April 26, 2019 12:23 PM
You have to restart the workflow after the computer restarts. Reread the article section on "Resuming a Workflow".
\(ツ)_/
Friday, April 26, 2019 2:10 PM
PS C:\>$AtStartup = New-JobTrigger -AtStartup
PS C:\>Register-ScheduledJob -Name ResumeWorkflow -Trigger $AtStartup -ScriptBlock {Import-Module PSWorkflow; Get-Job ComputerSetup -State Suspended | Resume-Job}
Id Name JobTriggers Command Enabled
--
1 ResumeWorkflowJob 1 Import-Module PSWorkflow; Get-Job CnS... True
# Run the workflow. It is suspended when the computer restarts.
PS C:\>New-ComputerSetup -JobName ComputerSetup
Id Name PSJobTypeName State HasMoreData Location Command
--
9 ComputerSetup PSWorkflowJob Suspended True localhost New-ComputerSetup
# Verify that the workflow was resumed and completed successfully
PS C:\>Import-Module PSWorkflow
PS C:\>Get-Job ComputerSetup
Id Name PSJobTypeName State HasMoreData Location Command
--
9 ComputerSetup PSWorkflowJob Completed True localhost New-ComputerSetup
# Delete the scheduled job
PS C:\>Unregister-ScheduledJob -Name ComputerSetup
The code above is from article. So I'm doing everything as it's written there. IN the section called "Automate resuming the workflow".
Monday, April 29, 2019 11:26 AM
Hello,
I try to resume my workflow via a scheduled task
workflow test-restart {
schtasks.exe /create /f /ru SYSTEM /v1 /tn Workflowresumer /sc ONstart /RL HIGHEST /tr "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe c:\resume_workflow.ps1"
Get-WmiObject -Class Win32_ComputerSystem | Out-File -FilePath C:\Reportscomp.txt
Restart-Computer -Wait
get-process | Out-File -FilePath C:\processes.txt
}
test-restart
The scheduled task launches the code like below following:
Import-Module PSWorkflow Get-Job -State Suspended | Resume-Job
The task completes successfully but the workflow doesn't resume. I reproduced the command get-job via psexec.exe under the system account I don't see any jobs. But under my user account I see a job. Why is it so? It's something to do with the system account I guess. Thank you.
Monday, April 29, 2019 3:22 PM
Hello,
Could you please help me out with how to proceed the script after reboot.
I tried several approaches but with zero success:
1) By means of the scheduled jobs and workflows like described here
scheduled job is created. also workflow job is created but scheduled job doesn't resume workflow job after reboot. workflow job is in suspended mode after reboot.
2) I tried to divide my script into several scripts. I created master script with the code:
if(test-path C:\server_deployment\script_state.txt)
{
C:\server_deployment\server_deployment_part2.ps1
}
Server_deployment_part1 script creates the file "script_state.txt" as a flag at the end of the script. Then I schedule the master script at startup of the server. It has to start the script "server_deployment_part2.ps1" which contains the code:
write-host "Hello world" -ForegroundColor Green
write-host "Press any key to close the window" -ForegroundColor Yellow
$x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
I expected the powershell window to appear but that doesn't happen although the scheduled task completed successfully. What's wrong with that? I need the powershell window to appear and implement the script interactively.
3) I tried to create a scheduled task to resume the workflow at startup. The scheduled task is created under system account
workflow test-restart {
schtasks.exe /create /f /ru SYSTEM /v1 /tn Workflowresumer /sc ONstart /RL HIGHEST /tr "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe c:\resume_workflow.ps1"
Get-WmiObject -Class Win32_ComputerSystem | Out-File -FilePath C:\Reportscomp.txt
Restart-Computer -Wait
get-process | Out-File -FilePath C:\processes.txt
resume_workflow.ps1 contains:
Import-Module PSWorkflow
Get-Job -State Suspended | Resume-Job
But it doesn't work. I started psexec under system account and tried to write the same commands as above and no jobs have been returned. That's why It doesn't work. But why I don't see suspended jobs?
Maybe you can enlighten me on other approach I haven't tried please?
Thanks.
Monday, April 29, 2019 3:38 PM
Please stop posting the same question multiple time. If someone has an answer they will post it.
I posted the link to the instructions for what you are doing. You must spend the time to learn how to do this and test each of the examples until you understand what is happening and why. We cannot do that for you.
\(ツ)_/
Monday, April 29, 2019 3:39 PM
why have you merged this theme with the previous one again? I don't agree with that because this theme isn't the same as the previous one. here I described that I tried several approaches and nothing worked for me. That's not a duplicate. I asked to maybe give me the other direction to search for.
Monday, April 29, 2019 3:42 PM
I did everything as it described in the instruction. Have you tried to do the same as in instruction prior to say it works? BY the way look here
https://devblogs.microsoft.com/scripting/powershell-workflows-restarting-the-computer/
The author is Microsoft Scripting guy and he says:
"The online documentation I have seen states that a Windows PowerShell scheduled job can be used to perform this task. I had zero success with that approach, so I switched to using a scheduled task."
Monday, April 29, 2019 3:44 PM
It is the same issue. You need to learn how to use the workflow to wait on a restart. Making tiny mods to the question will not get you an answer.
If you go through the link and learn how it is intended to work you will be able to use this correctly. Adding PsExec will not help or work.
There are two major things you need to address - restarting a remote system and restarting the local system.
\(ツ)_/
Monday, April 29, 2019 3:47 PM
I did everything as it described in the instruction. Have you tried to do the same as in instruction prior to say it works? BY the way look here
https://devblogs.microsoft.com/scripting/powershell-workflows-restarting-the-computer/
The author is Microsoft Scripting guy and he says:
"The online documentation I have seen states that a Windows PowerShell scheduled job can be used to perform this task. I had zero success with that approach, so I switched to using a scheduled task."
I have done this in the past. It works. You must be running as an admin and use a job although a task can work if set up as directed.
Also note that the workflow engine is not part of PowerShell it is a service that PowerShell generates an XML file with instructions. If any part of the workflow system is configured incorrectly then the restart of a workflow will not work.
If you are having an issue on a system then try a different system as a first test to determine the cause of your issue.
\(ツ)_/
Monday, April 29, 2019 3:54 PM
Here is another proven article that clearly explains the process and why it works:
https://devblogs.microsoft.com/scripting/powershell-workflows-restarting-the-computer/
\(ツ)_/
Monday, April 29, 2019 4:26 PM
It is the same article I pointed out above)) and in this article author says he haven't managed to get scheduled jobs to work and switched to scheduled tasks. The quote from above is from this article.
"The online documentation I have seen states that a Windows PowerShell scheduled job can be used to perform this task. I had zero success with that approach, so I switched to using a scheduled task."
Seems like you don't read what I'm writing at all. But anyway thanks.
Monday, April 29, 2019 4:44 PM
Some things you need to know about running jobs or tasks at startup You must create the job as an elevated admin account. Tasks/Jobs that run at startup may take quite a while to execute on a busy system or one that takes a while to fully load.
If you are creating output then the admin account must have correct permissions on the files and they must be on the local system.
I just ran this as a Scheduled Job and it worked as expected.
To test your code and the ability to resume run the reboot workflow and do a manual restart of the workflow. If this doesn't work then there is an issue with your system or with your code.
\(ツ)_/
Monday, April 29, 2019 4:51 PM
Thanks. I did manual restart of the workflow job by resume-job after reboot and it works. The code for scheduled job is well-known and described here:
So it's quite hard to make a mistake following a direct instruction. That's why it's a mystery for me why it doesn't work. But thanks again.
Also if that's not a secret could you share your working code with scheduled job? maybe I miss something and that can help me to get what exactly.
Monday, April 29, 2019 5:22 PM
Let's backup a bit. I am using Windows 10 and I do not need to resume a waiting job. On earlier systems this didn't exist and was added with WMF 5.1.
The code I use to test is as follows:
workflow restartComputer{
"[$([datetime]::Now)]Begin restart" | Out-File c:\temp\testwf.txt
Restart-Computer -Wait
"[$([datetime]::Now)]Return from restart" | Out-File c:\temp\testwf.txt -Append
}
restartComputer
After the restart the file is correctly updated with the two lines. The job exists for the workflow but is completed.
If you are using an old version of Windows then there may be issues with a restart-resume.
\(ツ)_/
Tuesday, April 30, 2019 8:12 AM
I'm using windows server 2012 where workflow jobs don't resume automatically. The code I'm using:
workflow test-restart {
Get-WmiObject -Class Win32_ComputerSystem | Out-File -FilePath C:\Reportscomp.txt
Restart-Computer -Wait
get-process | Out-File -FilePath C:\processes.txt
}
$AtStartup = New-JobTrigger -AtStartup
Register-ScheduledJob -Name ResumeWorkflow -Trigger $AtStartup -ScriptBlock {Import-Module PSWorkflow; Get-Job | Resume-Job}
test-restart -JobName resume-workflow
I'm trying to do using scheduled jobs as described here:
But after restart workflow job doesn't resume. Scheduled job doesn't work for some unknown reason and doesn't resume my workflow job. I see workflow job in "Suspended" mode after reboot. Also I see scheduled job. However, manually I can resume my workflow job. So the question is still open as I see it... Thank you.
Tuesday, April 30, 2019 8:17 AM
1. How many saved workflow jobs are stored on that system?
2. This is the scriptblock.
-ScriptBlock {Get-Job | Resume-Job}
You need to run the task elevated under the same account the created the workflow.
\(ツ)_/
Tuesday, April 30, 2019 8:54 AM
zero jobs. I delete them all prior to launch the workflow again.
Rewrote like that. Still nor result
workflow test-restart {
Get-WmiObject -Class Win32_ComputerSystem | Out-File -FilePath C:\Reportscomp.txt
Restart-Computer -Wait
get-process | Out-File -FilePath C:\processes.txt
}
$AtStartup = New-JobTrigger -AtStartup
$option = New-ScheduledJobOption -RunElevated
Register-ScheduledJob -Name ResumeWorkflow -Trigger $AtStartup -ScriptBlock {Import-Module PSWorkflow; Get-Job resume-workflow -State Suspended | Resume-Job} -ScheduledJobOption $option
test-restart -JobName resume-workflow
Workflow doesn't resume after restart.
Tuesday, April 30, 2019 9:04 AM
Since we cannot access your system there is really not much we can do to help you. I suggest contacting MS support for assistance.
\(ツ)_/
Tuesday, April 30, 2019 9:08 AM
Thank you very much for patience and help.
Tuesday, April 30, 2019 9:12 AM
Sorry I can't guess at the issue but it is likely something subtle that you are doing which has not become apparent. The methods documented are now more than 5 years old and are used in many places. The issue is not with PowerShell or Windows. It is either a coding error or some configuration issue.
\(ツ)_/