Skip to content

Commit 77b2bbe

Browse files
Update New-IsoFile.ps1
1 parent acbe878 commit 77b2bbe

File tree

1 file changed

+91
-98
lines changed

1 file changed

+91
-98
lines changed

New-IsoFile.ps1

+91-98
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,98 @@
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-
#>
1+
function New-IsoFile
232
{
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-
}
3+
<#
4+
.Synopsis
5+
Creates a new .iso file
6+
.Description
7+
The New-IsoFile cmdlet creates a new .iso file containing content from chosen folders
8+
.Example
9+
New-IsoFile "c:\tools","c:Downloads\utils"
10+
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.
11+
.Example
12+
New-IsoFile -FromClipboard -Verbose
13+
Before running this command, select and copy (Ctrl-C) files/folders in Explorer first.
14+
.Example
15+
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"
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+
.Notes
18+
NAME: New-IsoFile
19+
AUTHOR: Chris Wu
20+
LASTEDIT: 03/23/2016 14:46:50
21+
#>
22+
23+
[CmdletBinding(DefaultParameterSetName='Source')]Param(
24+
[parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true, ParameterSetName='Source')]$Source,
25+
[parameter(Position=2)][string]$Path = "$env:temp\$((Get-Date).ToString('yyyyMMdd-HHmmss.ffff')).iso",
26+
[ValidateScript({Test-Path -LiteralPath $_ -PathType Leaf})][string]$BootFile = $null,
27+
[ValidateSet('CDR','CDRW','DVDRAM','DVDPLUSR','DVDPLUSRW','DVDPLUSR_DUALLAYER','DVDDASHR','DVDDASHRW','DVDDASHR_DUALLAYER','DISK','DVDPLUSRW_DUALLAYER','BDR','BDRE')][string] $Media = 'DVDPLUSRW_DUALLAYER',
28+
[string]$Title = (Get-Date).ToString("yyyyMMdd-HHmmss.ffff"),
29+
[switch]$Force,
30+
[parameter(ParameterSetName='Clipboard')][switch]$FromClipboard
31+
)
32+
33+
Begin {
34+
($cp = new-object System.CodeDom.Compiler.CompilerParameters).CompilerOptions = '/unsafe'
35+
if (!('ISOFile' -as [type])) {
36+
Add-Type -CompilerParameters $cp -TypeDefinition @'
37+
public class ISOFile
38+
{
39+
public unsafe static void Create(string Path, object Stream, int BlockSize, int TotalBlocks)
40+
{
41+
int bytes = 0;
42+
byte[] buf = new byte[BlockSize];
43+
var ptr = (System.IntPtr)(&bytes);
44+
var o = System.IO.File.OpenWrite(Path);
45+
var i = Stream as System.Runtime.InteropServices.ComTypes.IStream;
46+
47+
if (o != null) {
48+
while (TotalBlocks-- > 0) {
49+
i.Read(buf, BlockSize, ptr); o.Write(buf, 0, bytes);
50+
}
51+
o.Flush(); o.Close();
52+
}
53+
}
54+
}
55+
'@
6056
}
57+
58+
if ($BootFile) {
59+
if('BDR','BDRE' -contains $Media) { Write-Warning "Bootable image doesn't seem to work with media type $Media" }
60+
($Stream = New-Object -ComObject ADODB.Stream -Property @{Type=1}).Open() # adFileTypeBinary
61+
$Stream.LoadFromFile((Get-Item -LiteralPath $BootFile).Fullname)
62+
($Boot = New-Object -ComObject IMAPI2FS.BootOptions).AssignBootImage($Stream)
63+
}
64+
65+
$MediaType = @('UNKNOWN','CDROM','CDR','CDRW','DVDROM','DVDRAM','DVDPLUSR','DVDPLUSRW','DVDPLUSR_DUALLAYER','DVDDASHR','DVDDASHRW','DVDDASHR_DUALLAYER','DISK','DVDPLUSRW_DUALLAYER','HDDVDROM','HDDVDR','HDDVDRAM','BDROM','BDR','BDRE')
66+
67+
Write-Verbose -Message "Selected media type is $Media with value $($MediaType.IndexOf($Media))"
68+
($Image = New-Object -com IMAPI2FS.MsftFileSystemImage -Property @{VolumeName=$Title}).ChooseImageDefaultsForMediaType($MediaType.IndexOf($Media))
69+
70+
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 }
6171
}
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
72+
73+
Process {
74+
if($FromClipboard) {
75+
if($PSVersionTable.PSVersion.Major -lt 5) { Write-Error -Message 'The -FromClipboard parameter is only supported on PowerShell v5 or higher'; break }
76+
$Source = Get-Clipboard -Format FileDropList
77+
}
78+
79+
foreach($item in $Source) {
80+
if($item -isnot [System.IO.FileInfo] -and $item -isnot [System.IO.DirectoryInfo]) {
81+
$item = Get-Item -LiteralPath $item
8482
}
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-
}
83+
84+
if($item) {
85+
Write-Verbose -Message "Adding item to the target image: $($item.FullName)"
86+
try { $Image.Root.AddTree($item.FullName, $true) } catch { Write-Error -Message ($_.Exception.Message.Trim() + ' Try a different media type.') }
9587
}
9688
}
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-
}
10589
}
90+
91+
End {
92+
if ($Boot) { $Image.BootImageOptions=$Boot }
93+
$Result = $Image.CreateResultImage()
94+
[ISOFile]::Create($Target.FullName,$Result.ImageStream,$Result.BlockSize,$Result.TotalBlocks)
95+
Write-Verbose -Message "Target image ($($Target.FullName)) has been created"
96+
$Target
97+
}
98+
}

0 commit comments

Comments
 (0)