Setting Environment Variables in a Task Sequence

Some tools require setting an environment variable when they are used.  For example, the User State Migration Tool has several that can be used for troubleshooting.  One is discussed in the Ask the Directory Services Team blog post here.  Unfortunately, the built-in MDT or ConfigMgr Task Sequence steps for capturing and restoring the user state don’t allow you to set environment variables.

Trying to set the environment variable using a script in a preceding step will not work.  If you set an environment variable in the script (e.g. using the SET command in a command shell script) it will only be set for that script.  The task sequencer parent process will not inherit the environment variable, so neither will subsequent steps.  Setting a System (or master) environment variable will have the same issue.  The task sequencer will not inherit the new master environment.   However, if you restart the computer after setting the System environment variable then task sequencer will inherit the updated System environment variables.

So the process for using an environment variable with something like USMT is to have steps before the steps that run the tool (the user state capture and restore steps in this case) that set the variable in the System environment and then restart the computer.  This is shown in the steps prefixed with Custom: in the sample MDT Lite Touch Task Sequence below.

image

Note that you can also place the step to set the variable before an existing earlier reboot in the same phase as well.  If you are using ConfigMgr OSD you should add the step(s) somewhere before any of the user state related steps (Request State Store, etc.).  Finally, if you are using an MDT-integrated ConfigMgr OSD task sequence you will need to add a Use Toolkit Package step after the reboot if you are adding an additional reboot for this.

There are a few ways to set System environment variables with a script.  The SETX command is built into Windows Vista and higher and a version is available in the Windows Server 2003 Resource Kit Utilities that can be used on Windows XP/Windows Server 2003.  The command syntax would be like this:

setx MIG_IGNORE_PROFILE_MISSING 1 /M

To have something a little more MDT-ish for this (with logging, etc.), I’ve attached an MDT script (MDTSetSystemEnvVar.wsf) for doing this.  Copy this to your MDT Scripts folder.

It can be used it two ways.  First, you can explicitly call out the environment variable name and value directly using command line switches.

cscript.exe "%SCRIPTROOT%\MDTSetSystemEnvVar.wsf" /envvar:MIG_IGNORE_PROFILE_MISSING /value:1

Finally, you can specify the name of a task sequence variable name.  The task sequence variable name and value will be used for setting the environment variable.

cscript.exe "%SCRIPTROOT%\MDTSetSystemEnvVar.wsf" /tsvar:MIG_IGNORE_PROFILE_MISSING

 

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use .

This post was contributed by Michael Murgolo, a Senior Consultant with Microsoft Services - U.S. East Region.

MDTSetSystemEnvVar.zip