-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathNew-ISOFile.ps1
318 lines (242 loc) · 9.98 KB
/
New-ISOFile.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<#
.SYNOPSIS
Create an ISO file from a source folder.
.DESCRIPTION
Create an ISO file from a source folder.
Optionally speicify a boot image and media type.
Based on original function by Chris Wu.
https://gallery.technet.microsoft.com/scriptcenter/New-ISOFile-function-a8deeffd (link appears to be no longer valid.)
Changes:
- Updated to work with PowerShell 7
- Added a bit more error handling and verbose output.
- Features removed to simplify code:
* Clipboard support.
* Pipeline input.
.PARAMETER source
The source folder to add to the ISO.
.PARAMETER destinationIso
The ISO file to create.
.PARAMETER bootFile
Optional. Boot file to add to the ISO.
.PARAMETER media
Optional. The media type of the resulting ISO (BDR, CDR etc). Defaults to DVDPLUSRW_DUALLAYER.
.PARAMETER title
Optional. Title of the ISO file. Defaults to "untitled".
.PARAMETER force
Optional. Force overwrite of an existing ISO file.
.INPUTS
None.
.OUTPUTS
None.
.EXAMPLE
New-ISOFile -source c:\forIso\ -destinationIso C:\ISOs\testiso.iso
Simple example. Create testiso.iso with the contents from c:\forIso
.EXAMPLE
New-ISOFile -source f:\ -destinationIso C:\ISOs\windowsServer2019Custom.iso -bootFile F:\efi\microsoft\boot\efisys.bin -title "Windows2019"
Example building Windows media. Add the contents of f:\ to windowsServer2019Custom.iso. Use efisys.bin to make the disc bootable.
.LINK
source: https://github.com/brianbaldock/New-ISOFile
.NOTES
01 Alistair McNair Initial version.
02 Brian Baldock Fixed some type errors and defined using class.
#>
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="Low")]
Param
(
[parameter(Mandatory=$true,ValueFromPipeline=$false)]
[string]$source,
[parameter(Mandatory=$true,ValueFromPipeline=$false)]
[string]$destinationIso,
[parameter(Mandatory=$false,ValueFromPipeline=$false)]
[string]$bootFile = $null,
[Parameter(Mandatory=$false,ValueFromPipeline=$false)]
[ValidateSet("CDR","CDRW","DVDRAM","DVDPLUSR","DVDPLUSRW","DVDPLUSR_DUALLAYER","DVDDASHR","DVDDASHRW","DVDDASHR_DUALLAYER","DISK","DVDPLUSRW_DUALLAYER","BDR","BDRE")]
[string]$media = "DVDPLUSRW_DUALLAYER",
[Parameter(Mandatory=$false,ValueFromPipeline=$false)]
[string]$title = "untitled",
[Parameter(Mandatory=$false,ValueFromPipeline=$false)]
[int]$filesystem = [FsiFileSystems]::FsiFileSystemISO9660 + [FsiFileSystems]::FsiFileSystemJoliet,
[Parameter(Mandatory=$false,ValueFromPipeline=$false)]
[switch]$force
)
begin {
Write-Verbose ("Function start.")
} # begin
process {
Write-Verbose ("Processing nested system " + $vmName)
## Set type definition
Write-Verbose ("Adding ISOFile type.")
$typeDefinition = @'
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
public class ISOFile {
public static void Create(string path, object comStream, int blockSize, int totalBlocks) {
byte[] buffer = new byte[blockSize];
using (var fileStream = System.IO.File.OpenWrite(path)) {
var stream = comStream as System.Runtime.InteropServices.ComTypes.IStream;
if (stream != null) {
IntPtr readBytesPointer = Marshal.AllocHGlobal(sizeof(int));
try {
for (int i = 0; i < totalBlocks; i++) {
stream.Read(buffer, blockSize, readBytesPointer);
int readBytes = Marshal.ReadInt32(readBytesPointer);
fileStream.Write(buffer, 0, readBytes);
}
} finally {
Marshal.FreeHGlobal(readBytesPointer);
}
}
fileStream.Flush();
}
}
}
'@
## Create type ISOFile, if not already created. Different actions depending on PowerShell version
if (!('ISOFile' -as [type])) {
## Add-Type works a little differently depending on PowerShell version.
## https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type
switch ($PSVersionTable.PSVersion.Major) {
## 7 and (hopefully) later versions
{$_ -ge 7} {
Write-Verbose ("Adding type for PowerShell 7 or later.")
Add-Type -CompilerOptions "/unsafe" -TypeDefinition $typeDefinition
} # PowerShell 7
## 5, and only 5. We aren't interested in previous versions.
5 {
Write-Verbose ("Adding type for PowerShell 5.")
$compOpts = New-Object System.CodeDom.Compiler.CompilerParameters
$compOpts.CompilerOptions = "/unsafe"
Add-Type -CompilerParameters $compOpts -TypeDefinition $typeDefinition
} # PowerShell 5
default {
## If it's not 7 or later, and it's not 5, then we aren't doing it.
throw ("Unsupported PowerShell version.")
} # default
} # switch
} # if
## Add boot file to image
if ($bootFile) {
Write-Verbose ("Optional boot file " + $bootFile + " has been specified.")
## Display warning if Blu Ray media is used with a boot file.
## Not sure why this doesn't work.
if(@('BDR','BDRE') -contains $media) {
Write-Warning ("Selected boot image may not work with BDR/BDRE media types.")
} # if
if (!(Test-Path -Path $bootFile)) {
throw ($bootFile + " is not valid.")
} # if
## Set stream type to binary and load in boot file
Write-Verbose ("Loading boot file.")
try {
$stream = New-Object -ComObject ADODB.Stream -Property @{Type=1} -ErrorAction Stop
$stream.Open()
$stream.LoadFromFile((Get-Item -LiteralPath $bootFile).Fullname)
Write-Verbose ("Boot file loaded.")
} # try
catch {
throw ("Failed to open boot file. " + $_.exception.message)
} # catch
## Apply the boot image
Write-Verbose ("Applying boot image.")
try {
$boot = New-Object -ComObject IMAPI2FS.BootOptions -ErrorAction Stop
$boot.AssignBootImage($stream)
Write-Verbose ("Boot image applied.")
} # try
catch {
throw ("Failed to apply boot file. " + $_.exception.message)
} # catch
Write-Verbose ("Boot file applied.")
} # if
## Build array of media types
$mediaType = @(
"UNKNOWN",
"CDROM",
"CDR",
"CDRW",
"DVDROM",
"DVDRAM",
"DVDPLUSR",
"DVDPLUSRW",
"DVDPLUSR_DUALLAYER",
"DVDDASHR",
"DVDDASHRW",
"DVDDASHR_DUALLAYER",
"DISK",
"DVDPLUSRW_DUALLAYER",
"HDDVDROM",
"HDDVDR",
"HDDVDRAM",
"BDROM",
"BDR",
"BDRE"
)
enum FsiFileSystems {
FsiFileSystemNone = 0
FsiFileSystemISO9660 = 1
FsiFileSystemJoliet = 2
FsiFileSystemUDF = 4
FsiFileSystemUnknown = 0x40000000
}
Write-Verbose ("Selected media type is " + $media + " with value " + $mediaType.IndexOf($media))
## Initialise image
Write-Verbose ("Initialising image object.")
try {
$image = New-Object -ComObject IMAPI2FS.MsftFileSystemImage -Property @{VolumeName=$title} -ErrorAction Stop
$image.ChooseImageDefaultsForMediaType($mediaType.IndexOf($media))
Write-Verbose ("initialised.")
} # try
catch {
throw ("Failed to initialise image. " + $_.exception.Message)
} # catch
## Create target ISO, throw if file exists and -force parameter is not used.
if ($PSCmdlet.ShouldProcess($destinationIso)) {
if (!($targetFile = New-Item -Path $destinationIso -ItemType File -Force:$Force -ErrorAction SilentlyContinue)) {
throw ("Cannot create file " + $destinationIso + ". Use -Force parameter to overwrite if the target file already exists.")
} # if
} # if
## Get source content from specified path
Write-Verbose ("Fetching items from source directory.")
try {
$sourceItems = Get-ChildItem -LiteralPath $source -ErrorAction Stop
Write-Verbose ("Got source items.")
} # try
catch {
throw ("Failed to get source items. " + $_.exception.message)
} # catch
## Add these to our image
Write-Verbose ("Adding items to image.")
foreach($sourceItem in $sourceItems) {
try {
$image.Root.AddTree($sourceItem.FullName, $true)
} # try
catch {
throw ("Failed to add " + $sourceItem.fullname + ". " + $_.exception.message)
} # catch
} # foreach
## Add boot file, if specified
if ($boot) {
Write-Verbose ("Adding boot image.")
$Image.BootImageOptions = $boot
}
## Write out ISO file
Write-Verbose ("Writing out ISO file to " + $targetFile)
try {
# modifiers needed for cloud-init https://cloudinit.readthedocs.io/en/latest/reference/datasources/nocloud.html#source-2-drive-with-labeled-filesystem
$Image.FileSystemsToCreate = $filesystem # ISO9660 + Joliet file system https://learn.microsoft.com/en-us/windows/win32/api/imapi2fs/ne-imapi2fs-fsifilesystems
$Image.ISO9660InterchangeLevel = 2 # ISO 9660 Level 2 permits longer file names https://learn.microsoft.com/en-us/windows/win32/imapi/disc-formats
$Image.GetDefaultFileSystemForImport($filesystem)
$result = $Image.CreateResultImage()
[ISOFile]::Create($targetFile.FullName,$result.ImageStream,$result.BlockSize,$result.TotalBlocks)
} # try
catch {
throw ("Failed to write ISO file. " + $_.exception.Message)
} # catch
Write-Verbose ("File complete.")
## Return file details
return $targetFile
} # process
end {
Write-Verbose ("Function complete.")
} # end