-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild-PackerImage.ps1
70 lines (60 loc) · 2.03 KB
/
Build-PackerImage.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
[CmdletBinding()]
param (
[Parameter()]
[switch]
$Capture
)
if ($Capture) {
if (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
throw 'Must run as admin to capture.'
}
}
Import-Module '.\Modules\vmdk.psm1'
$variableFile = '.\vars.pkrvars.hcl'
$packerArgs = @{
FilePath = 'packer.exe'
ArgumentList = "build -force --var-file=$($variableFile) ."
wait = $true
NoNewWindow = $true
}
Try {
# Unpack and Provision Image
Start-Process @packerArgs
}
Catch {
$_
}
if ($Capture) {
$OSFMOUNT_DIR = (Get-InstalledSoftware | Where-Object { $_.DisplayName -eq 'OSFMount' }).InstallLocation
if (!($OSFMOUNT_DIR)) {
throw 'OSFMount must be installed in order to capture image'
}
# Mount & Capture
$drive = (Get-ChildItem function:[d-z]: -n | Where-Object { !(Test-Path $_) })[0] # NOTE: OSFMount uses the first unused drive letter
$emojiIcon = [System.Convert]::toInt32('1F604', 16) # Yes I am creating an emoji icon just for this...
$imagePath = "$PSScriptRoot\CapturedImage.wim"
Try {
if (Test-Path $imagePath) {
Write-Output 'Removing old image...'
Remove-Item $imagePath -Force
}
$startTime = (Get-Date)
Mount-vmdk -Path '.\output\step-4\disk-cl3.vmdk' | Out-Null
Write-Output "Capturing image... ( Please be patient $([System.Char]::ConvertFromUtf32($emojiIcon)) this can take a while)"
New-WindowsImage -ImagePath $imagePath -CapturePath $drive -Name 'Windows10-Enterprise' -CompressionType 'Max' -Verify
$endTime = (Get-Date)
$elapsedTime = $endTime - $startTime
$elapsedMinutes = $elapsedTime.Minutes
$elapsedSeconds = $elapsedTime.Seconds
[string]::Format('==> Wait completed after {0} minutes {1} seconds', $elapsedMinutes, $elapsedSeconds)
Write-Host '==> Wim file was successfully captured and written to disk at ' -NoNewline
Write-Host "$imagePath" -ForegroundColor Cyan
}
Catch {
Write-Warning 'An error occurred:'
Write-Host $_
}
Finally {
Dismount-vmdk -Drive $drive
}
}