PowerShell 可以将普通文件夹转换为 ISO 文件。ISO 文件是二进制文件,可以被挂载并像只读 CD-ROM 驱动器一样运行。
过去,ISO 文件常用于挂载安装媒体。今天,您可以轻松地创建自己的 ISO 文件,并从自己的文件夹和文件中创建。这样,您可以创建一个简单的备份系统或者方便地与同事共享项目。由于 ISO 文件只是一个单独的文件,因此它们可以很容易地共享,并且由于 Windows 通过双击来挂载它们,并在 Windows Explorer 中显示它们作为 CD-ROM 驱动器,所以您可以立即使用数据而无需提取或解压任何内容。
与 VHD 映像文件相比,挂载 ISO 文件不需要管理员权限。任何人都可以挂载和使用 ISO 文件。
functionNew-IsoFile { param ( # path to local folder to store in # new ISO file (must exist) [Parameter(Mandatory)] [String] $SourceFilePath,
# name of new ISO image (arbitrary, # turns later into drive label) [String] $ImageName = 'MyCDROM',
# path to ISO file to be created [Parameter(Mandatory)] [String] $NewIsoFilePath,
# if specified, the source base folder is # included into the image file [switch] $IncludeRoot ) # use this COM object to create the ISO file: $fsi = New-Object-ComObject IMAPI2FS.MsftFileSystemImage
# use this helper object to write a COM stream to a file: # compile the helper code using these parameters: $cp = [CodeDom.Compiler.CompilerParameters]::new() $cp.CompilerOptions = '/unsafe' $cp.WarningLevel = 4 $cp.TreatWarningsAsErrors = $true $code = ' using System; using System.IO; using System.Runtime.InteropServices.ComTypes; namespace CustomConverter { public static class Helper { // writes a stream that came from COM to a filesystem file public static void WriteStreamToFile(object stream, string filePath) { // open output stream to new file IStream inputStream = stream as IStream; FileStream outputFileStream = File.OpenWrite(filePath); int bytesRead = 0; byte[] data; // read stream in chunks of 2048 bytes and write to filesystem stream: do { data = Read(inputStream, 2048, out bytesRead); outputFileStream.Write(data, 0, bytesRead); } while (bytesRead == 2048); outputFileStream.Flush(); outputFileStream.Close(); } // read bytes from stream: unsafe static private byte[] Read(IStream stream, int byteCount, out int readCount) { // create a new buffer to hold the read bytes: byte[] buffer = new byte[byteCount]; // provide a pointer to the location where the actually read bytes are reported: int bytesRead = 0; int* ptr = &bytesRead; // do the read: stream.Read(buffer, byteCount, (IntPtr)ptr); // return the read bytes by reference to the caller: readCount = bytesRead; // return the read bytes to the caller: return buffer; } } }'
# create CDROM, Joliet and UDF file systems $fsi.FileSystemsToCreate = 7 $fsi.VolumeName = $ImageName # allow larger-than-CRRom-Sizes $fsi.FreeMediaBlocks = -1
$msg = 'Creating ISO File - this can take a couple of minutes.' Write-Host$msg-ForegroundColor Green # define folder structure to be written to image: $fsi.Root.AddTreeWithNamedStreams($SourceFilePath,$IncludeRoot.IsPresent) # create image and provide a stream to read it: $resultimage = $fsi.CreateResultImage() $resultStream = $resultimage.ImageStream
# write stream to file [CustomConverter.Helper]::WriteStreamToFile($resultStream, $NewIsoFilePath)
Write-Host'DONE.'-ForegroundColor Green
}
一旦你运行了这段代码,你现在就有了一个名为 “New-IsoFile” 的新命令。从现有文件夹结构创建 ISO 文件现在变得非常简单 - 只需确保源文件路径存在即可:
Write-Progress-Activity'I am busy'-Status'Step A' Start-Sleep-Seconds2 Write-Progress-Activity'I am busy'-Status'Step B' Start-Sleep-Seconds2
如果您想在脚本仍在运行时关闭进度条,则需要使用“-Completed”开关参数:
1 2 3 4 5 6 7
Write-Progress-Activity'I am busy'-Status'Step A' Start-Sleep-Seconds2 Write-Progress-Activity'I am busy'-Status'Step B' Start-Sleep-Seconds2 Write-Progress-Completed-Activity'I am busy' Write-Host'Progress bar closed, script still running.' Start-Sleep-Seconds2
Write-Progress-Activity'I am busy'-Status'Step A' Start-Sleep-Seconds2 Write-Progress-Activity'I am busy'-Status'Step B' Start-Sleep-Seconds2 Write-Progress-Completed-Activity' ' Write-Host'Progress bar closed, script still running.' Start-Sleep-Seconds2
Write-Progress-Activity'I am busy'-Status'Step A' Start-Sleep-Seconds2 Write-Progress-Activity'I am busy'-Status'Step B' Start-Sleep-Seconds2 Write-Progress-Completed# due to the previously defined new default value, -Activity can now be omitted Write-Host'Progress bar closed, script still running.' Start-Sleep-Seconds2