-
-
Save ckrueger1979/efac6750bdcf0504acb13a5714c10396 to your computer and use it in GitHub Desktop.
New-IsoFile.ps1
This file contains 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
# | |
# New-IsoFile: simple PowerShell function to create iso file from a directory | |
# | |
# This is heavily simplified code inspired from: | |
# https://blog.apps.id.au/powershell-tools-create-an-iso/ | |
# http://blogs.msdn.com/b/opticalstorage/archive/2010/08/13/writing-optical-discs-using-imapi-2-in-powershell.aspx | |
# http://tools.start-automating.com/Install-ExportISOCommand/ | |
# http://stackoverflow.com/a/9802807/223837 | |
function New-IsoFile | |
{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true,Position=0)] | |
[string] $Path, | |
[Parameter(Mandatory=$true,Position=1)] | |
[string] $Destination, | |
[Parameter(Mandatory=$true)] | |
[string] $VolumeName, | |
[switch] $IncludeRoot, | |
[switch] $Force | |
) | |
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa364840.aspx | |
$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage | |
$fsi.FileSystemsToCreate = 4 # FsiFileSystemUDF | |
$fsi.FreeMediaBlocks = 0 | |
$fsi.VolumeName = $VolumeName | |
$fsi.Root.AddTree($Path, $IncludeRoot) | |
$istream = $fsi.CreateResultImage().ImageStream | |
$Options = if ($PSEdition -eq 'Core') { | |
@{ CompilerOptions = "/unsafe" } | |
} else { | |
$cp = New-Object CodeDom.Compiler.CompilerParameters | |
$cp.CompilerOptions = "/unsafe" | |
$cp.WarningLevel = 4 | |
$cp.TreatWarningsAsErrors = $true | |
@{ CompilerParameters = $cp } | |
} | |
Add-Type @Options -TypeDefinition @" | |
using System; | |
using System.IO; | |
using System.Runtime.InteropServices.ComTypes; | |
namespace IsoHelper { | |
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; | |
} | |
} | |
} | |
"@ | |
[IsoHelper.FileUtil]::WriteIStreamToFile($istream, $Destination) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment