-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
127 lines (111 loc) · 3.73 KB
/
build.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
param(
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]$File
)
function Invoke-Cmd {
<#
.SYNOPSIS
Execute a cmd file.
.DESCRIPTION
Execute a cmd file and import ENV variables.
.EXAMPLE
Invoke-Cmd -File file.cmd
.PARAMETER File
Mandatory file to execute.
#>
[cmdletbinding()]
param(
[string]$File,
[string]$parameters
)
$tmpFile = [IO.Path]::GetTempFileName()
cmd.exe /c "`"$File`" `"$parameters`" && set > `"$tmpFile`""
Get-Content $tmpFile | ForEach-Object {
if ($_ -match "^(.*?)=(.*)$") {
Set-Content "env:\$($matches[1])" $matches[2]
}
}
Remove-Item $tmpFile
# Verbose ENV
Get-ChildItem env:\
}
function Invoke-FileBrowser {
<#
.SYNOPSIS
Open file dialog.
.DESCRIPTION
Open a windows explorer file selector.
.EXAMPLE
Invoke-FileBrowser
#>
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
Multiselect = $false # Multiple files can be chosen
Filter = 'Image (*.iso)|*.iso' # Specified file types
}
[void]$FileBrowser.ShowDialog()
If ($FileBrowser.FileNames -like "*\*") {
# Do something
$FileBrowser.FileName #Lists selected files (optional)
} else {
Write-Host "Cancelled by user"
}
}
function New-ISO {
Param(
[Parameter(Mandatory = $true)]
[ValidateScript( {
If (-Not ($_ | Test-Path) ) {
Throw "File does not exist."
}
If (-Not ($_ | Test-Path -PathType Leaf) ) {
Throw "The Path argument must be a file. Folder paths are not allowed."
}
If ($_ -notmatch "(\.iso)") {
Throw "The file specified in the path argument must be an iso."
}
If (-Not (Get-DiskImage -ImagePath $_ | Get-Volume).DriveLetter ) {
Mount-DiskImage -ImagePath $_ -StorageType ISO -PassThru
}
Return $true
})]
[System.IO.FileInfo]$File
)
# install.wim test
$Drive = (Get-DiskImage -ImagePath $File | Get-Volume).DriveLetter
If (-Not "$Drive`:\sources\install.wim" | Test-Path ) {
Throw "Missing install.wim"
}
# Working path
# $Build = [System.IO.Path]::GetRandomFileName()
$Build = [System.IO.Path]::GetFileNameWithoutExtension($File)
# Copy image contents
& Robocopy.exe "$drive`:\" ".\$Build\" /ETA /MIR /R:0 /W:0
# Customise
& imagex.exe /info ".\$Build\sources\install.wim" > ".\$Build\BUILD.txt"
& Robocopy.exe "`$OEM$" ".\$Build\sources\`$OEM$" /ETA /MIR /R:1 /W:1
# TODO
# Invoke-FileBrowser as an option
# autounattend
Copy-Item ".\autounattend.xml" ".\$Build\autounattend.xml"
$tools = 'C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\oscdimg'
$oscdimg = "$tools\oscdimg.exe"
$etfsboot = "$tools\etfsboot.com"
$efisys = "$tools\efisys.bin"
$Arguments = '2#p0,e,b"{0}"#pEF,e,b"{1}"' -f $etfsboot, $efisys
Start-Process $oscdimg -args @("-bootdata:$Arguments", '-u2', '-udfver102', ".\$Build", ".\$Build.autounattend.iso") -wait -nonewwindow
# TODO Test for success then clean-up
#Remove-Item ".\$Build" -Force
# Clean up
Dismount-DiskImage -ImagePath $File
}
# Invoke ADK
Invoke-Cmd -File "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat"
# Build OS
If ( $File ) {
# Get the full path to avoid issues with .\
$FilePath = Resolve-Path -Path $File
New-ISO -File "$FilePath"
} Else {
New-ISO -File $(Invoke-FileBrowser)
}