Share via


Windows PowerShell Script Library for MDT

At the Microsoft Management Summit 2011 in March I presented session BF21, Accelerated Scripting with the MDT Framework.  The session focused on using the VBScript libraries built-in to MDT to reduce the amount of time to write custom scripts and using a common framework and best practices for more lasting scripts. Some of the advantages of using the MDT framework are a common logging format and location, shared environment variables, and a whole library of helper functions for common deployment scripting activities.

The MDT 2010 scripting framework is VBScript-only so that it will function in all supported operating system versions, including WinPE.  Windows PowerShell requires the .NET Framework, which unfortunately is not currently in WinPE.  There are still many scenarios in which Windows PowerShell could be quite advantageous when integrated with the MDT scripting framework.  You can execute powershell.exe with a .ps1 script from a task sequence, but you then lose all of the advantages offered by the MDT scripting framework.

So I started work on a Windows PowerShell library in the same vein as the ZTIUtility.vbs script library in MDT.  The goal was to recreate some of the functions, objects and properties in the same spirit, not just explicitly rewriting the VBScript in Windows PowerShell.  Due to limited resources I had to prioritize the scope: first focusing on common logging, then the shared environment variables, and finally building the library of helper functions.

(NOTE: I had hoped to release this right after MMS, possibly with some additional revision to the script, but unfortunately that was not possible.  So I'm posting this now as-is.  It is still a work-in-progress; but I hope by making this available others can benefit from it and/or build upon it.)

The method that I used to integrate the library with a custom Windows PowerShell script is by simply dot-sourcing the library at the top of the custom script.  Other methods (e.g., Windows PowerShell module) may achieve better results.

# // ...Script Header...

# // Load Library
$LocalPath = split-path -parent $MyInvocation.MyCommand.Definition
. $LocalPath\Z-Utility.ps1

# // Another brilliant PoSh script goes here!


Z-Utility.ps1 v1.0 Alpha Features

  • Common logging
    • Standard log format for easy viewing in Trace32
    • Location is the task sequence log path (_SMSTSLogPath) if it exists, otherwise the current TEMP directory
    • Handles standard log entry types: Info, Warning, Error, Verbose
    • Strings with passwords are masked (except when DEBUG = TRUE)
  • Common environment
    • If running from a task sequence, convert the task sequence variables to Windows PowerShell variables. For example, $Architecture = X64, $IsLaptop = True, $Make = Dell Inc., $SerialNumber = G3YGTF2, etc.
  • Helper functions
    • RunWithHeartbeat: Execute a command with the provided arguments, log the full command, log a message at a defined "heartbeat" interval, and log and return the exit code.

The library will function via a Windows PowerShell script executed directly or via a task sequence, but currently it works best when called from a task sequence (to leverage the existing log path and environment variables).  To execute the script from within a task sequence, create a Run Command Line task with the following example Command line:

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%ScriptRoot%\Custom\CustomScript.ps1"

Attached to this post are two files, the Z-Utility.ps1 (v1.0 Alpha) script library, and CustomScript.ps1, which simply demonstrates the current functionality of the script library.

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.

Z-Utility_v1-0a.zip