none
Powershell Workflows and Restart-Computer, doesn't wait as expected. RRS feed

  • General discussion

  • I've been experimenting with Powershell Workflows and come across behaviour I can't seem to figure out. When using Restart-Computer with the -Wait switch the documented behaviour should be that the workflow suspends and can be continued later with the Resume-Job cmdlet (after importing the PSModule Workflow).

    What actually seems to happen, at least on the Server 2012 evaluation VM I'm using, is that the workflow blindly continues without waiting resulting in incorrect behaviour.

    The behaviour can be seen by the following test script:

    workflow test-restartbug {
    Restart-Computer -Wait
    Get-Date | Set-Content -path "c:\scripts\reboot1.txt"
    Restart-Computer -Wait
    Get-Date | Set-Content -path "c:\scripts\reboot2.txt"
    Restart-Computer -Wait
    Get-Date | Set-Content -path "c:\scripts\reboot3.txt"
    Restart-Computer -Wait
    Get-Date | Set-Content -path "c:\scripts\reboot4.txt"
    Restart-Computer -Wait
    Get-Date | Set-Content -path "c:\scripts\reboot5.txt"
    Restart-Computer -Wait
    Get-Date | Set-Content -path "c:\scripts\reboot6.txt"
    Restart-Computer -Wait
    Get-Date | Set-Content -path "c:\scripts\reboot7.txt"
    Restart-Computer -Wait
    Get-Date | Set-Content -path "c:\scripts\reboot8.txt"
    }
    
    test-restartbug

    Which creates some (occasionally all 8) text files in one go, after just a single restart. If the workflow didn't get all the way through, then there is a suspended job, otherwise not.

    Am I missing something blindingly obvious, or is this a bug?

    Thursday, March 27, 2014 4:58 PM

All replies

  • Wait cannot be used with the local computer.  It is only valid with remoted restarts.

    Think about it.  How can a computer that is rebooting wait?


    ¯\_(ツ)_/¯

    Thursday, March 27, 2014 5:20 PM
  • In a normal script, you're absolutely correct. But the point of Workflows is surely that they're able to do things like be resumed after a reboot?

    The article at http://blogs.technet.com/b/heyscriptingguy/archive/2013/01/23/powershell-workflows-restarting-the-computer.aspx seems to suggest this should work as I expected, and I'd note that if you put a big enough delay after "Restart-Computer", with say "Start-Sleep -Seconds 100", to prevent the script from carrying on then you get the expected behaviour in that a PSWorkflowJob is created that can be resumed to allow the workflow to carry on from where it had reached and after each Resume-Job a new text file is created and the computer restarts again.

    Friday, March 28, 2014 9:24 AM
  • I've encountered the same issue on server 2012r2

    Add suspend-workflow instead of sleep after the restart should get around this issue
    Wednesday, December 3, 2014 5:30 PM