-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageAutomation.ps1
77 lines (60 loc) · 3.01 KB
/
ImageAutomation.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#########################################
# Author: Corey Deli #
# Page: https://coreydeli.com #
# Date: 4/4/2017 #
# Version: 1.0 #
#########################################
# Variables
## Location of the "CustomSettings.ini"
$customini = "\\Contoso-svr-MDT\TestBuild$\Control\"
## Possible tasks sequences. Use Task ID's
$tasks = "REFW10-X64-001"#,"NEXT","NEXT" < ADD ADDITIONAL AND REMOVE POUND SIGN FOR MULTIPLE ID's.
## Hyper-V Settings
$hvhost = "Contoso-HV.Contoso.pri"
$VHDLocal = "\\Contoso-HV\VmBuild"
$VHDRemote = "\\Contoso-HV\VmBuild"
$media = "\\Contoso-HV\VmBuild\TestTouch.iso"
## MDT Locations
$dplpath = "\\Contoso-svr-MDT\Deployment$"
$capturepath = "\\Contoso-svr-MDT\TestBuild$\Captures"
## Import Hyper-V 2012r2 Modules. Disable if on 2016
Import-Module Hyper-V -RequiredVersion 1.1
#Import-Module Hyper-V -RequiredVersion 2 # This is the module for Hyper-V 2016
Import-Module "C:\Program Files\Microsoft Deployment Toolkit\bin\MicrosoftDeploymentToolkit.psd1"
# Actual script running
ForEach ($id in $tasks) {
## Configure the CustomSettings.ini to skip the task sequence and just run.
Copy-Item $customini\CustomSettings.ini $customini\CustomSettings-backup.ini
Start-Sleep -s 5
Add-Content $customini\CustomSettings.ini "TaskSequenceID=$id"
Add-Content $customini\CustomSettings.ini "SkipTaskSequence=YES"
Add-Content $customini\CustomSettings.ini "SkipComputerName=YES"
(Get-Content $customini\CustomSettings.ini).replace('OSDComputerName=%OSDPrefix%-%TaskSequenceID%',';OSDComputerName=%OSDPrefix%-%TaskSequenceID%') |
Set-Content $customini\CustomSettings.ini
## Build the VM to create the image on.
$vmname = ("build-{0:yyyy-MM-dd-HH-mm}" -f (get-date))
New-VM -Name $vmname -MemoryStartupBytes 4096MB -BootDevice CD -Generation 1 -NewVHDPath $VHDLocal\$vmname.vhdx -NewVHDSizeBytes 130048MB `
-SwitchName "Microsoft Network Adapter Multiplexor Driver - Virtual Switch" -ComputerName $hvhost
Set-VM $vmname -ProcessorCount 2 -StaticMemory -ComputerName $hvhost
Set-VMDvdDrive -VMName $vmname -ControllerNumber 1 -ControllerLocation 0 -Path $media -ComputerName $hvhost
Start-VM $vmname -ComputerName $hvhost
## Wait for the VM to enter a "Stopped" State
while ((get-vm -name $vmname -ComputerName $hvhost).state -ne 'Off') { start-sleep -s 5 }
## Remove the VM and all VM files associated
Remove-VM $vmname -ComputerName $hvhost -Force
Remove-Item $VHDRemote\$vmname.vhdx
## Reset MDT Custom Settings
Remove-Item $customini\CustomSettings.ini -Force
Move-Item $customini\CustomSettings-backup.ini $customini\CustomSettings.ini
Start-Sleep -s 5
}
## Connect to the MDT Production drive
New-PsDrive -Name "DS002" -PSProvider MDTProvider -Root $dplpath
## Find the files.
$wims = Get-ChildItem $capturepath\*.wim
## Import the reference images into Production
ForEach ($file in $wims) {
Import-MDTOperatingSystem -Path "DS002:\Operating Systems" -SourceFile $file -DestinationFolder $file.name -Move
}
## Cleanup Stage
Remove-PSDrive -Name "DS002"