不想为了创建 iso 文件装个软件,于是找到了用 powershell 脚本创建 iso 映像文件的方法:

来源:http://cncc.bingj.com/cache.aspx?q=powershell+create+iso+from+folder&d=4802516624410696&mkt=zh-CN&setlang=zh-CN&w=GzXUfc7VNhyH7tJKT-ziCJ2xrSQLnxD7

Create an ISO file with PowerShell!
I received a request at work to transfer 5+gb from the internal network, to a server in the DMZ.  Our DMZ networks are locked down, so I cannot copy the files directly.  I have the trial version of UltraISO that I’ve used for tasks like this in the past, but it’s limited to 300mb in the trial version.  Previously, we’ve used a “swing VMDK” that we’d mount on an internal VM, copy the data to it, take this VMDK and download it to my PC with something like WinSCP, then upload it to a DMZ datastore, mount it to the VM, etc.  This can get messy, especially if you don’t remember to remove and delete the “swing VMDK”.

I decided to do some research online to see if I could use something like 7-zip or another tool to create the ISO.  I happened upon a function that someone wrote for PowerShell to create an ISO via Microsoft’s Technet Script Center. I took the function, read through it to verify it wasn’t going to do anything malicious (which you should ALWAYS do before just blatantly running someone else’s code) and decided to try it.

Here’s the function itself:

function New-IsoFile
{  <# .Synopsis Creates a new .iso file .Description The New-IsoFile cmdlet creates a new .iso file containing content from chosen folders .Example New-IsoFile "c:\tools","c:Downloads\utils" 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. .Example New-IsoFile -FromClipboard -Verbose Before running this command, select and copy (Ctrl-C) files/folders in Explorer first. .Example 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" 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 .Notes NAME: New-IsoFile AUTHOR: Chris Wu LASTEDIT: 03/23/2016 14:46:50 #>  [CmdletBinding(DefaultParameterSetName='Source')]Param([parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true, ParameterSetName='Source')]$Source,  [parameter(Position=2)][string]$Path = "$env:temp\$((Get-Date).ToString('yyyyMMdd-HHmmss.ffff')).iso",  [ValidateScript({Test-Path -LiteralPath $_ -PathType Leaf})][string]$BootFile = $null,[ValidateSet('CDR','CDRW','DVDRAM','DVDPLUSR','DVDPLUSRW','DVDPLUSR_DUALLAYER','DVDDASHR','DVDDASHRW','DVDDASHR_DUALLAYER','DISK','DVDPLUSRW_DUALLAYER','BDR','BDRE')][string] $Media = 'DVDPLUSRW_DUALLAYER',[string]$Title = (Get-Date).ToString("yyyyMMdd-HHmmss.ffff"),  [switch]$Force,[parameter(ParameterSetName='Clipboard')][switch]$FromClipboard)Begin {  ($cp = new-object System.CodeDom.Compiler.CompilerParameters).CompilerOptions = '/unsafe'if (!('ISOFile' -as [type])) {  Add-Type -CompilerParameters $cp -TypeDefinition @'
public class ISOFile
{public unsafe static void Create(string Path, object Stream, int BlockSize, int TotalBlocks)  {  int bytes = 0;  byte[] buf = new byte[BlockSize];  var ptr = (System.IntPtr)(&bytes);  var o = System.IO.File.OpenWrite(Path);  var i = Stream as System.Runtime.InteropServices.ComTypes.IStream;  if (o != null) {while (TotalBlocks-- > 0) {  i.Read(buf, BlockSize, ptr); o.Write(buf, 0, bytes);  }  o.Flush(); o.Close();  }}
}
'@  }if ($BootFile) {if('BDR','BDRE' -contains $Media) { Write-Warning "Bootable image doesn't seem to work with media type $Media" }($Stream = New-Object -ComObject ADODB.Stream -Property @{Type=1}).Open()  # adFileTypeBinary$Stream.LoadFromFile((Get-Item -LiteralPath $BootFile).Fullname)($Boot = New-Object -ComObject IMAPI2FS.BootOptions).AssignBootImage($Stream)}$MediaType = @('UNKNOWN','CDROM','CDR','CDRW','DVDROM','DVDRAM','DVDPLUSR','DVDPLUSRW','DVDPLUSR_DUALLAYER','DVDDASHR','DVDDASHRW','DVDDASHR_DUALLAYER','DISK','DVDPLUSRW_DUALLAYER','HDDVDROM','HDDVDR','HDDVDRAM','BDROM','BDR','BDRE')Write-Verbose -Message "Selected media type is $Media with value $($MediaType.IndexOf($Media))"($Image = New-Object -com IMAPI2FS.MsftFileSystemImage -Property @{VolumeName=$Title}).ChooseImageDefaultsForMediaType($MediaType.IndexOf($Media))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 }}  Process {if($FromClipboard) {if($PSVersionTable.PSVersion.Major -lt 5) { Write-Error -Message 'The -FromClipboard parameter is only supported on PowerShell v5 or higher'; break }$Source = Get-Clipboard -Format FileDropList}foreach($item in $Source) {if($item -isnot [System.IO.FileInfo] -and $item -isnot [System.IO.DirectoryInfo]) {$item = Get-Item -LiteralPath $item}if($item) {Write-Verbose -Message "Adding item to the target image: $($item.FullName)"try { $Image.Root.AddTree($item.FullName, $true) } catch { Write-Error -Message ($_.Exception.Message.Trim() + ' Try a different media type.') }}}}End {  if ($Boot) { $Image.BootImageOptions=$Boot }  $Result = $Image.CreateResultImage()  [ISOFile]::Create($Target.FullName,$Result.ImageStream,$Result.BlockSize,$Result.TotalBlocks)Write-Verbose -Message "Target image ($($Target.FullName)) has been created"$Target}
}

With that, I was able to create a variable for my source data, and use get-childitem to get that location and pipe that to creating the ISO. See below:

$source_dir = "Z:\Install\App123"
get-childitem "$source_dir" | New-ISOFile -path e:\iso\app123.iso

Even though the data was only about 5.4gb, there was over 120k files.  When I ran the script, I saw my PS window was “thinking” and when I glanced at my e:\iso folder, it had created the ISO file…  I saw the file was at 0k, so I said to myself, “This is going to error out!”
But I couldn’t have been more wrong!  It exported a file at 5.4gb!  I uploaded the new ISO to an ISO folder on a datastore accessible by the DMZ hosts, mounted it to my VM and BOOM, all my data was there and I was able to copy it!

I hope someone else finds this as useful as I did!

Ben Liebowitz, VCP, vExpert
NJ VMUG Leader

以上。

下面放几张示意图:

Powershell 脚本创建 iso 映像文件相关推荐

  1. 如何创建新的虚拟机并安装Linux系统(一步到位,附ISO映像文件)

    安装之前需提前准备好VMware和iso镜像文件,以下是博主提供的资源,有需要的小伙伴可以自取: rhel8.3镜像及VMware安装包 链接(永久有效):百度网盘 请输入提取码 提取码:vf29 - ...

  2. win10无法装载iso文件_win 10如何装载和弹出ISO映像文件

    要在 Windows 中装载(挂载) ISO 映像需要通过第三方虚拟光驱软件来实现,从 Windows 8 开始微软已经原生支持了该功能.当然,用户无需安装额外软件也可在 Windows 10 中直接 ...

  3. linux创建蓝光映像光盘,11.13 mkisofs指令:创建光盘映像文件

    11.13  mkisofs指令:创建光盘映像文件 [语    法]mkisofs [选项] [参数] [功能介绍]mkisofs指令被用来创建ISO9660.JOLIET和HFS类型的文件系统映像. ...

  4. 如何在Mac上。ISO映像文件刻录到DVD

    ISO是普通的CD或DVD光盘映像格式基于ISO-9660标准.从原始光盘ISO映像文件包含一个精确复制的数据.它包括光盘上的文件系统的信息,如目录结构,文件属性和引导代码,以及保存的数据.如果你想知 ...

  5. CentOS 7 从本地 ISO 映像文件安装 Gnome GUI

    由于虚拟机从本地 ISO 映像文件安装 CentOS 7 默认是没有Gnome GUI 图像界面的,以下是在CentOS 7 命令行界面从本地 ISO 映像文件安装 Gnome GUI 的步骤. 1. ...

  6. 虚拟机启动时,提示找不到ISO映像文件

    博主不定期更新[保研/推免.C/C++.5G移动通信.Linux.生活随笔]系列文章,喜欢的朋友[点赞+关注]支持一下吧! 记录一个问题,每次启动虚拟机时,出现下图提示: 出现原因应该是我在安装完虚拟 ...

  7. 您选择的文件不是有效的iso映像文件,请重新选择

    安装windows系统的时候无非就是参考类似于下面的这些博文 通用PE u盘装Ghost Win10系统教程http://www.tongyongpe.com/win10ghost.html 用U盘装 ...

  8. VMware或Hyper-V 工具ISO映像文件位置

    VMware或Hyper-V 工具ISO映像文件位置 对于Hyper-V: 在" Hyper-V集成组件ISO映像的完整路径"字段中,输入Hyper-V集成组件ISO映像的位置.需 ...

  9. linux dd目录生成iso文件,Linux下dd + mkisofs 制作可启动 img/iso 映像文件

    总的来说,制作镜像文件有三种方法,cp, cat, dd 和其它专用工具.cp ,cat 和 dd都可以从设备复制文件来创建镜像.而 dd 命令更为强大,可以通过指定块大小,块多少来直接创建镜像. I ...

最新文章

  1. 虚拟化--046 利用web client查看存储
  2. 华为配置SSH登陆详细步骤
  3. 听说,京沪津的人都爱直接“看牌”买买买
  4. python处理excel-使用python将数据写入excel
  5. 设计模式之 抽象工厂 封装业务逻辑层和Dao层
  6. vue vuex 挂载_vue.js,javascript_Vuex的初始化失败,一直显示没有挂载到根组件上,奇怪了!,vue.js,javascript - phpStudy...
  7. C语言实现高斯-赛德尔迭代gauss seidel(附完整源码)
  8. [CTS2019]氪金手游
  9. php在空值时调用成员函数_当Vlookup函数匹配的结果是时间,或者空值时,显示不正常了...
  10. 工作中常用到的ES6语法
  11. 多行日志合并处理的内外存方法
  12. 01 前言/基础设施 - DevOps之路
  13. InnoDB存储引擎相关问题整理
  14. Zemax自学--2(Zemax软件总览)
  15. java程序一维数组能被5整除,JAVA鏈熸湯璇曢闆?鍚瓟妗? - 鐧惧害鏂囧簱
  16. 股票涨或跌为什么?看懂本质,才能顺势而为!
  17. 【RASA】NLU模块组件分析
  18. Jquery导出页面表格table的内容为Excel,PDF,DOC格式
  19. Java异常处理——日志打印
  20. springmvcHandlerMapping解析

热门文章

  1. linux it资产管理系统,开源IT资产管理软件(GIPI)
  2. 朗强科技HDMI网线延长器
  3. Isaac-gym(8):Tensor API
  4. RabbitMQ入门教程(四):工作队列(Work Queues)
  5. QQ数据库管理-----mysql
  6. linux 文件和打印机共享文件夹,能实现Windows和Linux系统之间文件和打印机共享的Linux服务是( )...
  7. 使用GridView实现仿微信发朋友圈添加图片,点击预览、删除图片
  8. Use Case框图
  9. UWA学堂|Unreal课程合集
  10. [unity3d]场景烘焙