Workflow - Resume Task not working after restart

Hello All,

I am stuck with a workflow that I am trying to get working but for some reason the scheduled task does not resume after computer restarts. When I do the command Get-Job, I can see that the job is suspended.

This is the script that i tried putting together. Basically, what I am trying to acheive is that when the computer restarts, another powershell script is executed.

## Workflow function
workflow restart {
    # Restart computer on workflow
    Restart-Computer -Wait
    # Action to perform at startup
    InlineScript { C:\temp\resume.ps1 }
    # Unregister schedule job after restart
    Unregister-ScheduledJob -Force -Name ResumeWorkflow

# Create Trigger
$Trigger = New-JobTrigger -AtStartUp

# Register the scheduled job
Register-ScheduledJob -Name ResumeWorkflow -Trigger $Trigger -ScriptBlock {[System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager = $true; Import-Module PSWorkflow; Get-Job -Name resumejob -State Suspended | Resume-Job -Wait}

# Call workflow function
restart -AsJob -JobName resumejob

Any help would be much appreciated. Thanks!

Take a look at this blog. The code snipplets are little jacked for me in my browser, but if you select the text you can see the code.

Automatically resuming Windows PowerShell Workflow jobs at logon | PowerShell Team (

1 Like

hi @rob-simmers ,

Thank you for your reply. Yes I have looked at this solution you posted as well. Unfortuantely that didnt work for me.

I think my problem is that the registered scheduled job doesnt run after restart. Though if I run the command within powershell manually after restart as desribed below, works okay

Get-Job -Name resumejob | Resume-Job -Wait

By specifiying this in the script block should do the same thing I would have thought i.e.

Register-ScheduledJob -Name ResumeWorkflow -Trigger $Trigger -ScriptBlock {[System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager = $true; Import-Module PSWorkflow; Get-Job -Name resumejob -State Suspended | Resume-Job -Wait}

The one you said runs fine doesn’t have -State suspended, where your second example does. Have you tried removing that?

Hi @krzydoug

Yeah i tried with both. No change in outcome unfortunately.

Okay I got it working only when the user logs back on, then the workflow resumes via scheduled task. Could not get it working if I changed the scheduled trigger from AtLogOn to AtStartUp. Changing trigger to AtStartUp does not seem to want the scheduled task to resume. When I checked the Task Scheduler, it just shows a status of ready. If anyone has any ideas, please do let me know how I can achieve this at startup instead.

Below are the scripts I am using.

# This is the workflow.ps1

# Pass in arguments from command line

# Write values to file for other script (eventsub.ps1) to retrieve these values and use them as variables
$scriptvariable1 | Out-File C:\temp\scriptvariable1.txt
$scriptvariable2 | Out-File C:\temp\scriptvariable2.txt

# Workflow function to restart and run next powershell script after restart. Next powershell script does the event subsription configuration
workflow restart {
  Restart-Computer -Wait
  InlineScript { C:\temp\eventsub.ps1 -All }

# Configure Schedule task 
$actionscript = '-Noninteractive -WindowStyle Normal -NoLogo -NoProfile -NoExit -Command "&"C:\temp\resumeworkflow.ps1""'
$pstart = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$act = New-ScheduledTaskAction -Execute $pstart -Argument $actionscript
#$trig = New-JobTrigger -AtLogOn -RandomDelay 00:00:30  # NOTE: Inorder to resume scheduled task, user has to logon to computer
$trig = New-JobTrigger -AtStartUp -RandomDelay 00:00:30

# Register the Scheduled Task
Register-ScheduledTask -TaskName ResumeWorkFlow -Action $act -Trigger $trig -RunLevel Highest

# Call workflow

# This is the resumeworkflow.ps1

# Import module to allow resume of scheduled task
Import-Module PSWorkflow

# Resume suspended job after restart
Get-Job | Resume-Job

# Unregister Schedule Task after schedule is run after restart
Get-ScheduledTask -TaskName ResumeWorkFlow | Unregister-ScheduledTask -Confirm:$false
# This is the final powershell script (eventsub.ps1) that should be run after restart to configure event subscription forwarding

$variable1 = Get-Content -Path C:\temp\scriptvariable1.txt
$variable2 = Get-Content -Path C:\temp\scriptvariable2.txt

# Start windows forwarder service
wecutil qc /q

# Allow winrm to listen on port 443 as well
echo y | cmd.exe /c winrm quickconfig -transport:https

# Import event subscription event subscription xml
$files = Get-ChildItem -Recurse -Path "C:\temp\xml_copy"

# Loop through xml directory and insert ssid for domain computers/controllers passed in from variables
foreach ($i in $files){
  $content = Get-Content $i.FullName
  $linetoreplace = '<AllowedSourceDomainComputers></AllowedSourceDomainComputers>'
  $replacementline = "<AllowedSourceDomainComputers>O:NSG:BAD:P${variable1}${variable2}S:</AllowedSourceDomainComputers>"
  $content.Replace($linetoreplace,$replacementline) | Set-Content $i.FullName

  cmd.exe /c wecutil cs $i.FullName