Advertisement
Guest User

New-IsoFile cmdlet

a guest
Dec 7th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .Synopsis Creates a new .iso file
  3.     .Description The New-IsoFile cmdlet creates a new .iso file containing content from chosen folders
  4.    
  5.     .Example
  6.  
  7.     ```powershell
  8.         New-IsoFile "c:\tools","c:Downloads\utils"
  9.     ```        
  10.     This command creates a .iso file in $env:temp folder (default location) that contains
  11.     c:\tools and c:\downloads\utils folders.
  12.     The folders themselves are included at the root of the .iso image.
  13.    
  14.     .Example
  15.  
  16.     ```powershell
  17.         New-IsoFile -FromClipboard -Verbose  
  18.     ```
  19.     Before running this command, select and copy (Ctrl-C) files/folders in Explorer first.
  20.  
  21.     .Example
  22.    
  23.     ```powershell
  24.         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"
  25.     ```    
  26.     This command creates a bootable .iso file containing the content from c:\WinPE folder,
  27.     but the folder itself isn't included. Boot file etfsboot.com can be found in Windows ADK.
  28.     Refer to IMAPI_MEDIA_PHYSICAL_TYPE enumeration for possible media types:
  29.         http://msdn.microsoft.com/en-us/library/windows/desktop/aa366217(v=vs.85).aspx
  30.  
  31.     .Notes NAME: New-IsoFile
  32.     .AUTHOR: Chris Wu LASTEDIT: 03/23/2016 14:46:50
  33. #>
  34.  
  35. function New-IsoFile {  
  36.  
  37.   [CmdletBinding(DefaultParameterSetName='Source')]Param(
  38.     [parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true, ParameterSetName='Source')]$Source,  
  39.     [parameter(Position=2)][string]$Path = "$env:temp\$((Get-Date).ToString('yyyyMMdd-HHmmss.ffff')).iso",  
  40.     [ValidateScript({Test-Path -LiteralPath $_ -PathType Leaf})][string]$BootFile = $null,
  41.     [ValidateSet('CDR','CDRW','DVDRAM','DVDPLUSR','DVDPLUSRW','DVDPLUSR_DUALLAYER','DVDDASHR','DVDDASHRW','DVDDASHR_DUALLAYER','DISK','DVDPLUSRW_DUALLAYER','BDR','BDRE')][string] $Media = 'DVDPLUSRW_DUALLAYER',
  42.     [string]$Title = (Get-Date).ToString("yyyyMMdd-HHmmss.ffff"),  
  43.     [switch]$Force,
  44.     [parameter(ParameterSetName='Clipboard')][switch]$FromClipboard
  45.   )
  46.  
  47.   Begin {  
  48.     ($cp = new-object System.CodeDom.Compiler.CompilerParameters).CompilerOptions = '/unsafe'
  49.     if (!('ISOFile' -as [type])) {  
  50.       Add-Type -CompilerParameters $cp -TypeDefinition @'
  51. public class ISOFile  
  52. {
  53.  public unsafe static void Create(string Path, object Stream, int BlockSize, int TotalBlocks)  
  54.  {  
  55.    int bytes = 0;  
  56.    byte[] buf = new byte[BlockSize];  
  57.    var ptr = (System.IntPtr)(&bytes);  
  58.    var o = System.IO.File.OpenWrite(Path);  
  59.    var i = Stream as System.Runtime.InteropServices.ComTypes.IStream;  
  60.  
  61.    if (o != null) {
  62.      while (TotalBlocks-- > 0) {  
  63.        i.Read(buf, BlockSize, ptr); o.Write(buf, 0, bytes);  
  64.      }  
  65.      o.Flush(); o.Close();  
  66.    }
  67.  }
  68. }  
  69. '@  
  70.     }
  71.    
  72.     if ($BootFile) {
  73.       if('BDR','BDRE' -contains $Media) { Write-Warning "Bootable image doesn't seem to work with media type $Media" }
  74.       ($Stream = New-Object -ComObject ADODB.Stream -Property @{Type=1}).Open()  # adFileTypeBinary
  75.       $Stream.LoadFromFile((Get-Item -LiteralPath $BootFile).Fullname)
  76.       ($Boot = New-Object -ComObject IMAPI2FS.BootOptions).AssignBootImage($Stream)
  77.     }
  78.  
  79.     $MediaType = @('UNKNOWN','CDROM','CDR','CDRW','DVDROM','DVDRAM','DVDPLUSR','DVDPLUSRW','DVDPLUSR_DUALLAYER','DVDDASHR','DVDDASHRW','DVDDASHR_DUALLAYER','DISK','DVDPLUSRW_DUALLAYER','HDDVDROM','HDDVDR','HDDVDRAM','BDROM','BDR','BDRE')
  80.  
  81.     Write-Verbose -Message "Selected media type is $Media with value $($MediaType.IndexOf($Media))"
  82.     ($Image = New-Object -com IMAPI2FS.MsftFileSystemImage -Property @{VolumeName=$Title}).ChooseImageDefaultsForMediaType($MediaType.IndexOf($Media))
  83.    
  84.     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 }
  85.   }  
  86.  
  87.   Process {
  88.     if($FromClipboard) {
  89.       if($PSVersionTable.PSVersion.Major -lt 5) { Write-Error -Message 'The -FromClipboard parameter is only supported on PowerShell v5 or higher'; break }
  90.       $Source = Get-Clipboard -Format FileDropList
  91.     }
  92.  
  93.     foreach($item in $Source) {
  94.       if($item -isnot [System.IO.FileInfo] -and $item -isnot [System.IO.DirectoryInfo]) {
  95.         $item = Get-Item -LiteralPath $item
  96.       }
  97.  
  98.       if($item) {
  99.         Write-Verbose -Message "Adding item to the target image: $($item.FullName)"
  100.         try { $Image.Root.AddTree($item.FullName, $true) } catch { Write-Error -Message ($_.Exception.Message.Trim() + ' Try a different media type.') }
  101.       }
  102.     }
  103.   }
  104.  
  105.   End {  
  106.     if ($Boot) { $Image.BootImageOptions=$Boot }  
  107.     $Result = $Image.CreateResultImage()  
  108.     [ISOFile]::Create($Target.FullName,$Result.ImageStream,$Result.BlockSize,$Result.TotalBlocks)
  109.     Write-Verbose -Message "Target image ($($Target.FullName)) has been created"
  110.     $Target
  111.   }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement