Created
February 22, 2024 17:01
-
-
Save phuonghuynh/cfefa53250bf322b793f217aca9d157f to your computer and use it in GitHub Desktop.
Powershell script to create iso file from a folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-IStreamToFile([__ComObject] $istream, [string] $fileName) | |
{ | |
# NOTE: We cannot use [System.Runtime.InteropServices.ComTypes.IStream], | |
# since PowerShell apparently cannot convert an IStream COM object to this | |
# Powershell type. (See http://stackoverflow.com/a/9037299/223837 for | |
# details.) | |
# It turns out that .NET/CLR _can_ do this conversion. | |
# | |
# That is the reason why method FileUtil.WriteIStreamToFile(), below, | |
# takes an object, and casts it to an IStream, instead of directly | |
# taking an IStream inputStream argument. | |
$typedef = @" | |
using System; | |
using System.IO; | |
using System.Runtime.InteropServices.ComTypes; | |
namespace My | |
{ | |
public static class FileUtil { | |
public static void WriteIStreamToFile(object i, string fileName) { | |
IStream inputStream = i as IStream; | |
FileStream outputFileStream = File.OpenWrite(fileName); | |
int bytesRead = 0; | |
int offset = 0; | |
byte[] data; | |
do { | |
data = Read(inputStream, 2048, out bytesRead); | |
outputFileStream.Write(data, 0, bytesRead); | |
offset += bytesRead; | |
} while (bytesRead == 2048); | |
outputFileStream.Flush(); | |
outputFileStream.Close(); | |
} | |
unsafe static private byte[] Read(IStream stream, int toRead, out int read) { | |
byte[] buffer = new byte[toRead]; | |
int bytesRead = 0; | |
int* ptr = &bytesRead; | |
stream.Read(buffer, toRead, (IntPtr)ptr); | |
read = bytesRead; | |
return buffer; | |
} | |
} | |
} | |
"@ | |
# $cp = New-Object CodeDom.Compiler.CompilerParameters | |
# $cp.CompilerOptions = "/unsafe" | |
# $cp.WarningLevel = 4 | |
# $cp.TreatWarningsAsErrors = $true | |
$PSV = $PSVersionTable.PSVersion.Major | |
if ($PSV -ge 6) { | |
$cp = "/unsafe" | |
Add-Type -CompilerOptions $cp -TypeDefinition $typedef | |
} | |
elseif ($PSV -le 5 -and $PSV -ge 3) { | |
$cp = New-Object CodeDom.Compiler.CompilerParameters | |
$cp.CompilerOptions = "/unsafe" | |
$cp.WarningLevel = 4 | |
$cp.TreatWarningsAsErrors = $true | |
Add-Type --CompilerParameters $cp -TypeDefinition $typedef | |
} | |
# Add-Type -CompilerParameters $cp -TypeDefinition | |
[My.FileUtil]::WriteIStreamToFile($istream, $fileName) | |
} | |
Function New-ISOFileFromFolder { | |
<# | |
.SYNOPSIS | |
Creates an ISO file from a filepath | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[String]$FilePath, | |
[Parameter(Mandatory=$true)] | |
[String]$Name, | |
[Parameter(Mandatory=$true)] | |
[String]$ResultFullFileName | |
) | |
write-host "Creating ISO $Name" -ForegroundColor Green | |
$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage | |
$dftd = New-Object -ComObject IMAPI2.MsftDiscFormat2Data | |
$Recorder = New-Object -ComObject IMAPI2.MsftDiscRecorder2 | |
$fsi.FileSystemsToCreate = 7 | |
$fsi.VolumeName = $Name | |
$fsi.FreeMediaBlocks = 1000000 #default 332800 | |
$fsi.Root.AddTreeWithNamedStreams($FilePath,$false) | |
$resultimage = $fsi.CreateResultImage() | |
$resultStream = $resultimage.ImageStream | |
Write-IStreamToFile $resultStream $ResultFullFileName | |
} | |
New-ISOFileFromFolder -FilePath {xxx} -Name {xxx} -ResultFullFileName {xxx} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment