Public/MyWindowsImage/Mount-MyWindowsImage.ps1
<#
.SYNOPSIS Mounts a WIM file .DESCRIPTION Mounts a WIM file automatically selecting the Path and the Index .LINK https://osd.osdeploy.com/module/functions/dism/mount-mywindowsimage .NOTES 19.11.21 David Segura @SeguraOSD #> function Mount-MyWindowsImage { [CmdletBinding()] param ( #Specifies the location of the WIM or VHD file containing the Windows image you want to mount. [Parameter( Position = 0, Mandatory = $true, ValueFromPipelineByPropertyName )] [string[]]$ImagePath, #Index of the WIM to Mount [Parameter( ValueFromPipelineByPropertyName )] [UInt32]$Index = 1, #Mount the WIM as Read Only [Parameter( ValueFromPipelineByPropertyName )] [switch]$ReadOnly, #Opens the Path in Windows Explorer [Parameter( ValueFromPipelineByPropertyName )] [switch]$Explorer ) begin { #=================================================================================================== # Require Admin Rights #=================================================================================================== if ((Get-OSDGather -Property IsAdmin) -eq $false) { Write-Warning "$($MyInvocation.MyCommand) requires Admin Rights ELEVATED" Break } #=================================================================================================== } process { foreach ($Input in $ImagePath) { #=================================================================================================== # ImagePath #=================================================================================================== Write-Verbose "ImagePath: $Input" Write-Verbose "Index: $Index" #=================================================================================================== # Validate File #=================================================================================================== if (-not (Test-Path $Input -ErrorAction SilentlyContinue)) { Write-Warning "Unable to locate WindowsImage at $Input" Break } #=================================================================================================== # Get-Item #=================================================================================================== $WindowsImageOSD = Get-Item $Input if ($WindowsImageOSD.Extension -ne '.wim') { Write-Warning "WindowsImage does not have a .wim extension" Break } if ($WindowsImageOSD.IsReadOnly -eq $true) { Write-Warning "WindowsImage is Read Only" Break } #=================================================================================================== # Set Mount Path #=================================================================================================== $OSDMountPath = $env:Temp + '\OSD' + (Get-Random) if (! (Test-Path $OSDMountPath)) { New-Item $OSDMountPath -ItemType Directory -Force | Out-Null } $Path = (Get-Item $OSDMountPath).FullName #=================================================================================================== # Mount-WindowsImage #=================================================================================================== if ($ReadOnly.IsPresent) { Mount-WindowsImage -Path $Path -ImagePath $Input -Index $Index -ReadOnly | Out-Null } else { Mount-WindowsImage -Path $Path -ImagePath $Input -Index $Index | Out-Null } #=================================================================================================== # Explorer #=================================================================================================== if ($Explorer.IsPresent) {explorer $Path} #=================================================================================================== # Return for PassThru #=================================================================================================== Return Get-WindowsImage -Mounted | Where-Object {$_.Path -eq $Path} } } end {} } |