Quick Fix – MDT Environment Variables set in PowerShell are not flushed to Variables.dat

When setting variables in MDT Lite Touch, they need to be written to Variables.dat to be able to survive a reboot. This is of course very much needed when using a custom PowerShell frontend or script in the WinPE phase.

Bug in MDT 2013 Update 2

When setting variables in VBScript they are automatically flushed to Variables.dat, but not when setting them via PowerShell. For example if you have the IP Address (OSDADAPTER0IPADDRESSLIST) set to 192.168.1.200, and you change it via PowerShell, it get's updated in memory, but not in the Variables.dat file on disk.

Workarounds

There are two things you can do to fix the problem:

1. Update the Variables.dat file directly in your PowerShell script. The Variables.dat file is really just a XML file, so it's easy to update. Here is how the open source PowerShell Deployment for MDT does it:

function Save-PSDVariables
{
    $v = [xml]""
    Get-ChildItem TSEnv: | % {
        $element = $v.CreateElement("var")
        $element.SetAttribute("name", $_.Name) | Out-Null
        $element.AppendChild($v.createCDATASection($_.Value)) | Out-Null
        $v.DocumentElement.AppendChild($element) | Out-Null
    }
    $path = "$(Get-PSDLocaldataPath)Variables.dat"
    $v.Save($path)
    return $path
}

2. Call a VBscript that resets the value to whatever value your PowerShell script set, then just run that script after your PowerShell script. 

Here is an example (I named the script ZTIFlushVariables.wsf):


    


Logging

If you are curious what values the script writes back to the variables.dat file, you can open the ZTIFlushVariables.log file.

image

image

Example when a task sequence calls the script after a custom PowerShell frontend sets new values.

About the author

Johan Arwidmark

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

>