In a previous post I wrote about my Image Factory PowerShell script for MDT. This post is about a script derived from that which I setup to test my images and my deployment task sequences. Since this script is only an edit of the previous one I don’t intend to release it “officially” on the TechNet Gallery or the PowerShell Gallery. To explain succinctly what this script does, it turns Task Sequences into Virtual Machines. If you have any questions or comments, please leave them below.
-Mike
Features and Requirements
The script is designed to run on a device with MDT installed.
The device must also have Hyper-V management tools installed.
The MDT shares can be local or on a remote device.
The Hyper-V host can be local or on a remote device.
The script has been tested on Hyper-V installations on Windows 10, Windows Server 2016, and Windows Server 2012 R2 and MDT installations on Windows 10 and Windows Server 2016. When run, the script will:
Create and configure a Hyper-V Virtual Machine for image deployment.
Boot it from the MDT LiteTouch boot media.
Run the specified Task Sequence.
When the TS completes, the Virtual Machine will be configured for general usage.
Move on to the next specified task sequence.
Do steps 1-5 for all specified TS.
Optionally create a log file and email it to an address of your choice.
The script should be run with the -remote switch when the Hyper-V host is a remote device. The script should be run with the -compat switch when the Hyper-V host a remote server running Windows Server 2012 R2.
Configuration
I’ve changed the configuration to be done via command line switches, instead of having to edit the script itself. Here’s a list of all the switches and example configurations
Command Line Switch
Description
Example
-Deploy
Location of the deployment share, it can be a local or UNC path.
\\server\deploymentshare$ OR C:\DeploymentShare
-Vh
Name of the Hyper-V host. Can be a local or remote device.
VS01
-Vhd
The path relative to the Hyper-V server of where to put the VHD file for the VM(s) that will be generated.
C:\Hyper-V\VHD
-Boot
The path relative to the Hyper-V server of where the ISO file is to boot from.
C:\iso\LiteTouchPE_x64.iso
-Vnic
Name of the virtual switch that the virtual machine should use to communicate with the network. If the name of the switch contains a space encapsulate with single or double quotes.
vSwitch-Ext
-Ts
The comma-separated list of task sequence ID’s to build.
W10-1809,WS19-DC
-Compat
Use this switch if the Hyper-V server is Windows Server 2012 R2 and the script is running on Windows 10 or Windows Server 2016/2019. This loads the older version of the Hyper-V module, so it can manage WS2012 R2 Hyper-V VMs.
N/A
-Remote
Use this switch if the Hyper-V server is a remote device. Do not use this switch if the script is running on the same device as Hyper-V.
N/A
-L
Location to store the optional log file. The name of the log file is automatically generated.
The password required for the user being used for SMTP authentication.
P@ssw0rd
-UseSsl
Configures the utility to connect to the SMTP server using SSL.
N/A
This script can be run in a variety of configurations. You could run it on a Windows 10 device with MDT and Hyper-V Management tools installed or a single MDT deployment share on a file server and a remote Hyper-V host running Windows 10, Windows Server 2016 or Windows Server 2012 R2. The script makes changes (shown below) to your MDT customsettings.ini file - after making a backup, of course! These changes are necessary so that the process runs completely automated. Depending on your environment, you may need to make additional changes. Also most important: running this script will it will tie up your deployment share until it’s completed. Below are the settings you’ll need to add to your Bootstrap.ini to auto login to your Build Share. I recommend make these changes for auto-login, regenerate the boot images, copying the LiteTouch.iso to a safe place, then changing the settings back to how they were. This maintains the security of your deployment share, whilst giving you the necessary automated ISO to boot from.
The script adds the following lines to the end of your customsettings.ini. It sets the current task sequence to run, tells the WinPE deployment environment to skip asking for a Task Sequence and skip asking for a computer name. The computer name and Virtual Machine name is set to the Task Sequence ID.
# -------------------------------------------# Script: Image-Factory-Deploy.ps1# Version: 2.4# Author: Mike Galvin# Date: 18/08/2017# -------------------------------------------[CmdletBinding()]Param([parameter(Mandatory=$true)][alias("deploy")]$mdtdeploypath,[parameter(Mandatory=$true)][alias("ts")]$tsid,[parameter(Mandatory=$true)][alias("vh")]$vmhost,[parameter(Mandatory=$true)][alias("vhd")]$vhdpath,[parameter(Mandatory=$true)][alias("boot")]$bootmedia,[parameter(Mandatory=$true)][alias("vnic")]$vmnic,[alias("l")]$logpath,[alias("sendto")]$mailto,[alias("from")]$mailfrom,[alias("smtp")]$smtpserver,[alias("user")]$smtpuser,[alias("pwd")]$smtppwd,[switch]$usessl,[switch]$compat,[switch]$remote)## If logging is configured, start logIf($LogPath){$LogFile="image-factory.log"$Log="$LogPath\$LogFile"$LogT=Test-Path-Path$Log## If the log file already exists, clear itIf($LogT){Clear-Content-Path$Log}Add-Content-Path$Log-Value""Add-Content-Path$Log-Value"$(Get-Date-formatg) Log started"Add-Content-Path$Log-Value""}## If compat is configured, load the older Hyper-V PS moduleIf($compat){If($LogPath){Add-Content-Path$Log-Value"$(Get-Date-formatg) Importing Hyper-V 1.1 PowerShell Module"}Write-Host"$(Get-Date-formatg) Importing Hyper-V 1.1 PowerShell Module"Import-Module$env:windir\System32\WindowsPowerShell\v1.0\Modules\Hyper-V\1.1\Hyper-V.psd1}## Import MDT PS moduleIf($LogPath){Add-Content-Path$Log-Value"$(Get-Date-formatg) Importing MDT PowerShell Module"}Write-Host"$(Get-Date-formatg) Importing MDT PowerShell Module"$mdt="$env:programfiles\Microsoft Deployment Toolkit\bin\MicrosoftDeploymentToolkit.psd1"Import-Module$mdtForEach($idin$tsid){## Setup MDT custom settings for VM auto deployIf($LogPath){Add-Content-Path$Log-Value"$(Get-Date-formatg) Backing up current MDT CustomSettings.ini"}Write-Host"$(Get-Date-formatg) Backing up current MDT CustomSettings.ini"Copy-Item$mdtdeploypath\Control\CustomSettings.ini$mdtdeploypath\Control\CustomSettings-backup.iniStart-Sleep-s5If($LogPath){Add-Content-Path$Log-Value"$(Get-Date-formatg) Setting up MDT CustomSettings.ini for Task Sequence ID: $id"}Write-Host"$(Get-Date-formatg) Setting MDT CustomSettings.ini for Task Sequence ID: $id"Add-Content$mdtdeploypath\Control\CustomSettings.ini"TaskSequenceID=$id"Add-Content$mdtdeploypath\Control\CustomSettings.ini"SkipTaskSequence=YES"Add-Content$mdtdeploypath\Control\CustomSettings.ini"SkipComputerName=YES"## Create VM$vmname=$idIf($LogPath){Add-Content-Path$Log-Value"$(Get-Date-formatg) Creating VM: $vmname on $vmhost"Add-Content-Path$Log-Value"$(Get-Date-formatg) Adding VHD: $vhdpath\$vmname.vhdx"Add-Content-Path$Log-Value"$(Get-Date-formatg) Adding Virtual NIC: $vmnic"}Write-Host"$(Get-Date-formatg) Creating VM: $vmname on $vmhost"Write-Host"$(Get-Date-formatg) Adding VHD: $vhdpath\$vmname.vhdx"Write-Host"$(Get-Date-formatg) Adding Virtual NIC: $vmnic"New-VM-name$vmname-MemoryStartupBytes4096MB-BootDeviceCD -Generation1-NewVHDPath$vhdpath\$vmname.vhdx-NewVHDSizeBytes130048MB-SwitchName$vmnic-ComputerName$vmhostIf($LogPath){Add-Content-Path$Log-Value"$(Get-Date-formatg) Configuring VM Processor Count"Add-Content-Path$Log-Value"$(Get-Date-formatg) Configuring VM Static Memory"Add-Content-Path$Log-Value"$(Get-Date-formatg) Configuring VM to boot from $bootmedia"}Write-Host"$(Get-Date-formatg) Configuring VM Processor Count"Write-Host"$(Get-Date-formatg) Configuring VM Static Memory"Write-Host"$(Get-Date-formatg) Configuring VM to boot from $bootmedia"Set-VM$vmname-ProcessorCount2-StaticMemory-ComputerName$vmhostSet-VMDvdDrive-VMName$vmname-ControllerNumber1-ControllerLocation0-Path$bootmedia-ComputerName$vmhostIf($LogPath){Add-Content-Path$Log-Value"$(Get-Date-formatg) Starting $vmname on $vmhost with $id"}Write-Host"$(Get-Date-formatg) Starting $vmname on $vmhost with $id"Start-VM$vmname-ComputerName$vmhost## Wait for VM to stopIf($LogPath){Add-Content-Path$Log-Value"$(Get-Date-formatg) Waiting for $vmname to build $id"}Write-Host"$(Get-Date-formatg) Waiting for $vmname to build $id"While((Get-VM-Name$vmname-ComputerName$vmhost).state-ne'Off'){Start-Sleep-s5}## Change config backSet-VMDvdDrive-VMName$vmname-ControllerNumber1-ControllerLocation0-Path$null-ComputerName$vmhostSet-VM-Name$VMName-DynamicMemory-MemoryStartupBytes1GB-MemoryMinimumBytes100MB-MemoryMaximumBytes4GB-ComputerName$vmhost## Restore MDT custom settingsIf($LogPath){Add-Content-Path$Log-Value"$(Get-Date-formatg) Restoring MDT CustomSettings.ini from backup"}Write-Host"$(Get-Date-formatg) Restoring MDT CustomSettings.ini from backup"Remove-Item$mdtdeploypath\Control\CustomSettings.iniMove-Item$mdtdeploypath\Control\CustomSettings-backup.ini$mdtdeploypath\Control\CustomSettings.iniStart-Sleep-s5}## If log was configured stop the logIf($LogPath){Add-Content-Path$Log-Value""Add-Content-Path$Log-Value"$(Get-Date-formatg) Log finished"Add-Content-Path$Log-Value""## If email was configured, set the variables for the email subject and bodyIf($smtpserver){$mailsubject="Lab: Image Factory Deploy Log"$mailbody=Get-Content-Path$log|Out-String## If an email password was configured, create a variable with the username and passwordIf($smtppwd){$smtpcreds=New-ObjectSystem.Management.Automation.PSCredential-ArgumentList$smtpuser,$($smtppwd|ConvertTo-SecureString-AsPlainText-Force)## If ssl was configured, send the email with sslIf($usessl){Send-MailMessage-To$mailto-From$mailfrom-Subject$mailsubject-Body$mailbody-SmtpServer$smtpserver-UseSsl-Credential$smtpcreds}## If ssl wasn't configured, send the email without sslElse{Send-MailMessage-To$mailto-From$mailfrom-Subject$mailsubject-Body$mailbody-SmtpServer$smtpserver-Credential$smtpcreds}}## If an email username and password were not configured, send the email without authenticationElse{Send-MailMessage-To$mailto-From$mailfrom-Subject$mailsubject-Body$mailbody-SmtpServer$smtpserver}}}## End