纯 PowerShell 脚本生成ISO镜像文件

原理跟脚本一样,调系统自带 IMAPI2,不用装东西。2-4GB 大概 13 分钟,看到”完成”就是好了,ISO 会生成在文件夹。

$src = 'F:\office\office2024'
$dst = 'F:\office\office2024.iso'
if (Test-Path $dst) { Remove-Item $dst -Force }

Add-Type -TypeDefinition @"
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
public static class IsoW {
    public static void Write(object comStream, string path) {
        IStream s = (IStream)comStream;
        byte[] buf = new byte[1048576];
        using (FileStream fs = File.Create(path)) {
            while (true) {
                IntPtr pr = Marshal.AllocHGlobal(sizeof(int));
                try {
                    s.Read(buf, buf.Length, pr);
                    int got = Marshal.ReadInt32(pr);
                    if (got <= 0) break;
                    fs.Write(buf, 0, got);
                } finally { Marshal.FreeHGlobal(pr); }
            }
        }
    }
}
"@

$img = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
$img.VolumeName = 'OFFICE2024'
$img.FileSystemsToCreate = 4
$img.FreeMediaBlocks = 10000000
$img.Root.AddTree($src, $false)
$res = $img.CreateResultImage()
[IsoW]::Write($res.ImageStream, $dst)
"完成: $dst  ({0:N1} MB)" -f ((Get-Item $dst).Length/1MB)

原创文章,作者:开心电脑网,如若转载,请注明出处。

赞 (0)
Windows 10/11硬盘系统分区空间大小建议
上一篇 2026年10月3日
三星SA系列显示器无信号故障解决方案[附三星显示器软件更新软件免费下载]
下一篇 2024年9月2日

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注