Creating Reference Images using the VMware platform – Image Factory script by Johnny Radeck

On this blog I previously posted PowerShell scripts to automatically generate reference images via the Hyper-V platform, often referred to as an Image Factory. Here is an example for the VMware platform, provided by my good friend Johnny Radeck. Thanks Johnny!

Note: This script can also be used as a base to create virtual machines for the hydration kits I have released, which are used for automating setup of entire lab environments. While the hydration kits work on any virtual platform that supports booting from an ISO file (meaning all of them), I only provided PowerShell scripts to create VMs for the Hyper-V platform. This script, with some modifications, can be used for that as well.

Automation is king 🙂

The credential script

If you need a credential script for a standalone host, here is an example:

New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @('root',('P@ssw0rd' | ConvertTo-SecureString -AsPlainText -Force)) | Export-Clixml -Path "C:\Setup\VMware Cred\esxcred.enc.xml"

The create virtual machines script

Below you find the script that creates the reference image VMs, tested on both VMware VSphere PowerCLI v5.5 and 6.0:

$Params = @{
    VMMemory = 3500MB #Desired memory configuration
    VMDiskSize = 40GB #Desired disk configuration
    Cpu = 2 #Numbers of CPU's   
    VMNetwork = 'LAN' #Name of the network    
    VMISO = 'E:\Build\ISO\LiteTouchPE_x64.iso' #Location of MDT ISO
    VMHost = 'Host01' #Name of the Host
    $CredFilePath = 'C:\Setup\VMware Cred\esxcred.enc.xml'
    $Credential = Import-Clixml -Path $CredFilePath
function New-RefImage-VMware {
        [ValidateSet('WIN10X64ENT', 'WIN81X64ENT', 'WIN7X64ENT', 'WIN2012R2', 'WIN2016')]
        #Only in VMware function
    Add-PSSnapin VMware.VimAutomation.Core
    Connect-VIServer -Server $VMHost -User $Credential.UserName -Password $Credential.GetNetworkCredential().Password
    New-VM -VMHost $VMHost -GuestId windows8_64Guest -Name $ImageName -MemoryMB ($VMMemory / 1MB) -NumCpu $Cpu -DiskGB ($VMDiskSize /1GB) -NetworkName $VMNetwork -Description "Ref image build VM"
    Get-VM $ImageName | Get-NetworkAdapter | Set-NetworkAdapter -MacAddress $MACAddress -Confirm:$False
    $cd = New-CDDrive -VM $ImageName -ISOPath $VMISO
    Set-CDDrive -CD $cd -StartConnected:$true -Confirm:$False
    Start-VM -VM $ImageName
    $VM = Get-VM -Name $ImageName
    $status = $vm.PowerState
    While($status -ne 'PoweredOff')
        write-host "The VM is still running"  
        Start-Sleep -s 20 
        $vm = Get-VM -Name $ImageName 
        $status = $vm.PowerState  
    Remove-VM -DeletePermanently -VM $ImageName -Confirm:$False
    Disconnect-VIServer -Server * -Force -Confirm:$False
    New-RefImage-VMware @Params -ImageName WIN10X64ENT -MACAddress '00:50:56:00:00:01' -Credential $Credential
    New-RefImage-VMware @Params -ImageName WIN81X64ENT -MACAddress '00:50:56:00:00:02'
    New-RefImage-VMware @Params -ImageName WIN7X64ENT -MACAddress '00:50:56:00:00:03'
    New-RefImage-VMware @Params -ImageName WIN2012R2 -MACAddress '00:50:56:00:00:04'

/ Johan

About the author

Johan Arwidmark

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments