Skip to content

Commit acbe878

Browse files
Create New-IsoFile.ps1
1 parent 39be319 commit acbe878

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

New-IsoFile.ps1

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<#
2+
.Synopsis Creates a new .iso file .Description The New-IsoFile cmdlet creates a new .iso file containing content from chosen folders
3+
.Example
4+
New-IsoFile "c:\tools","c:Downloads\utils"
5+
6+
This command creates a .iso file in $env:temp folder (default location) that contains c:\tools and c:\downloads\utils folders. The folders themselves are included at the root of the .iso image.
7+
8+
.Example
9+
New-IsoFile -FromClipboard -Verbose
10+
11+
Before running this command, select and copy (Ctrl-C) files/folders in Explorer first.
12+
13+
.Example
14+
dir c:\WinPE | New-IsoFile -Path c:\temp\WinPE.iso -BootFile "${env:ProgramFiles(x86)}\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\efisys.bin" -Media DVDPLUSR -Title "WinPE"
15+
16+
This command creates a bootable .iso file containing the content from c:\WinPE folder, but the folder itself isn't included. Boot file etfsboot.com can be found in Windows ADK. Refer to IMAPI_MEDIA_PHYSICAL_TYPE enumeration for possible media types: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366217(v=vs.85).aspx
17+
18+
.Notes
19+
NAME: New-IsoFile
20+
AUTHOR: Chris Wu
21+
LASTEDIT: 03/23/2016 14:46:50
22+
#>
23+
{
24+
[CmdletBinding(DefaultParameterSetName = 'Source')]
25+
Param(
26+
[parameter(Position = 1, Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Source')]
27+
$Source,
28+
[parameter(Position = 2)]
29+
[string]$Path = "$env:temp\$((Get-Date).ToString('yyyyMMdd-HHmmss.ffff')).iso",
30+
[ValidateScript({ Test-Path -LiteralPath $_ -PathType Leaf })]
31+
[string]$BootFile = $null,
32+
[ValidateSet('CDR', 'CDRW', 'DVDRAM', 'DVDPLUSR', 'DVDPLUSRW', 'DVDPLUSR_DUALLAYER', 'DVDDASHR', 'DVDDASHRW', 'DVDDASHR_DUALLAYER', 'DISK', 'DVDPLUSRW_DUALLAYER', 'BDR', 'BDRE')]
33+
[string] $Media = 'DVDPLUSRW_DUALLAYER',
34+
[string]$Title = (Get-Date).ToString("yyyyMMdd-HHmmss.ffff"),
35+
[switch]$Force,
36+
[parameter(ParameterSetName = 'Clipboard')]
37+
[switch]$FromClipboard
38+
)
39+
40+
Begin {
41+
($cp = New-Object System.CodeDom.Compiler.CompilerParameters).CompilerOptions = '/unsafe'
42+
if (!('ISOFile' -as [type])) {
43+
Add-Type -CompilerParameters $cp -TypeDefinition @'
44+
public class ISOFile
45+
{
46+
public unsafe static void Create(string Path, object Stream, int BlockSize, int TotalBlocks)
47+
{
48+
int bytes = 0;
49+
byte[] buf = new byte[BlockSize];
50+
var ptr = (System.IntPtr)(&bytes);
51+
var o = System.IO.File.OpenWrite(Path);
52+
var i = Stream as System.Runtime.InteropServices.ComTypes.IStream;
53+
54+
if (o != null) {
55+
while (TotalBlocks-- > 0) {
56+
i.Read(buf, BlockSize, ptr); o.Write(buf, 0, bytes);
57+
}
58+
o.Flush(); o.Close();
59+
}
60+
}
61+
}
62+
'@
63+
}
64+
65+
if ($BootFile) {
66+
if('BDR','BDRE' -contains $Media) { Write-Warning "Bootable image doesn't seem to work with media type $Media" }
67+
($Stream = New-Object -ComObject ADODB.Stream -Property @{Type=1}).Open() # adFileTypeBinary
68+
$Stream.LoadFromFile((Get-Item -LiteralPath $BootFile).Fullname)
69+
($Boot = New-Object -ComObject IMAPI2FS.BootOptions).AssignBootImage($Stream)
70+
}
71+
72+
$MediaType = @('UNKNOWN','CDROM','CDR','CDRW','DVDROM','DVDRAM','DVDPLUSR','DVDPLUSRW','DVDPLUSR_DUALLAYER','DVDDASHR','DVDDASHRW','DVDDASHR_DUALLAYER','DISK','DVDPLUSRW_DUALLAYER','HDDVDROM','HDDVDR','HDDVDRAM','BDROM','BDR','BDRE')
73+
74+
Write-Verbose -Message "Selected media type is $Media with value $($MediaType.IndexOf($Media))"
75+
($Image = New-Object -com IMAPI2FS.MsftFileSystemImage -Property @{VolumeName=$Title}).ChooseImageDefaultsForMediaType($MediaType.IndexOf($Media))
76+
77+
if (!($Target = New-Item -Path $Path -ItemType File -Force:$Force -ErrorAction SilentlyContinue)) { Write-Error -Message "Cannot create file $Path. Use -Force parameter to overwrite if the target file already exists."; break }
78+
}
79+
80+
Process {
81+
if($FromClipboard) {
82+
if($PSVersionTable.PSVersion.Major -lt 5) { Write-Error -Message 'The -FromClipboard parameter is only supported on PowerShell v5 or higher'; break }
83+
$Source = Get-Clipboard -Format FileDropList
84+
}
85+
86+
foreach($item in $Source) {
87+
if($item -isnot [System.IO.FileInfo] -and $item -isnot [System.IO.DirectoryInfo]) {
88+
$item = Get-Item -LiteralPath $item
89+
}
90+
91+
if($item) {
92+
Write-Verbose -Message "Adding item to the target image: $($item.FullName)"
93+
try { $Image.Root.AddTree($item.FullName, $true) } catch { Write-Error -Message ($_.Exception.Message.Trim() + ' Try a different media type.') }
94+
}
95+
}
96+
}
97+
98+
End {
99+
if ($Boot) { $Image.BootImageOptions=$Boot }
100+
$Result = $Image.CreateResultImage()
101+
[ISOFile]::Create($Target.FullName,$Result.ImageStream,$Result.BlockSize,$Result.TotalBlocks)
102+
Write-Verbose -Message "Target image ($($Target.FullName)) has been created"
103+
$Target
104+
}
105+
}

0 commit comments

Comments
 (0)