MDT 2010 introduced a new feature called Finish Actions. This allowed you to set the variable FinishAction to either SHUTDOWN, RESTART (or REBOOT), or LOGOFF and the computer would be shutdown, restarted, or logged off after the task sequence (and subsequent cleanup) had completed. Unfortunately, this was only provided for Lite Touch deployments.
This same functionality was required in Configuration Manager OS deployments for a recent project of mine. So I created scripts that would allow the addition of a step to the task sequence that would consume the same variable and take the appropriate action. I wish I could claim credit for this idea, but I actually based this script off of scripts provided by Deployment Guy emeritus Richard Trusson . In this post, Richard created a script and HTA to implement a restart at the end of a BDD 2007 task sequence. To implement this required a two stage process. The task sequence step must call one script which then launches the second script that does the real work. The first script does not wait for the second script to complete before completing itself. That way the task sequence can continue and complete while the second script is still running.
The second script then waits for the task sequence to complete by watching for the task sequence process (TSManager.exe) to exit before doing its assigned task. For my implementation the first script, ZTI-SpawnProcess.wsf, will launch the command specified using the /cmd switch. The script to actually execute shutdown, restart, or logoff the is OSDFinishAction.vbs. This script takes two arguments, /FinishAction for specifying the action name and the optional /FinishActionDelay for specifying how many seconds to wait after TSManager.exe exits before executing the finish action.
To implement these in an MDT-integrated OSD task sequence, add the above scripts to the Scripts folder of the MDT Toolkit package. Set the value for the FinishAction variable by whatever method you choose (CustomSettings.ini, Set Task Sequence Variable step, etc.). Then add a Run Command Line step after what would be the last step in the successful execution of the task sequence. Specify the following command line (the /FinishActionDelay is optional but recommended to allow time for all cleanup to finish, adjust the time as needed):
cscript.exe "%deployroot%\scripts\ZTI-SpawnProcess.wsf" /cmd:"cscript.exe %deployroot%\scripts\OSDFinishAction.vbs /FinishAction:%FinishAction% /FinishActionDelay:60"
Note that using ZTI-SpawnProcess.wsf is not absolutely necessary to accomplish this. I use it so that I have logging of what command was launched in the BDD log. However, if the command you wish to run as the second script required quotes in its command line, using a script like ZTI-SpawnProcess.wsf may not actually work. This is due to the way VBScript strips out the quoting from argument values. You can use a command line like this when command line quotes are required for the “second” script.
cmd.exe /c start "" cscript.exe "%deployroot%\scripts\OSDFinishAction.vbs" /FinishAction:%FinishAction% /FinishActionDelay:60
This method uses the internal start command of the Command Shell as the “first” script to launch the “second” script without waiting for it to exit. Both methods will work for launching OSDFinishAction.vbs.
Finally, I’ve also included in the attachment a more general purpose “run an action after the task sequence” script, OSDRunActionAfterTaskSequence.vbs, that you can use to launch any command once the task sequence has ended. A sample command line for this script would be something like this:
cmd.exe /c start "" cscript.exe "%deployroot%\scripts\OSDRunActionAfterTaskSequence.vbs" /Action:"C:\Temp\CleanupScript.vbs" /Delay:60
Care must be taken when launching other scripts this way. For example if the script is included in the toolkit package, you will need to add a step before this Run Command Line to copy it to a location that will not be cleaned up when the task sequence exits.
One final note, both OSDFinishAction.vbs and OSDRunActionAfterTaskSequence.vbs log to the %Temp% folder (C:\Windows\Temp for the System account in a running Windows OS).
This post was contributed by Michael Murgolo, a Senior Consultant with Microsoft Services - U.S. East Region.