3

I have an ISO image (in this case an MS Office ISO) that I'd like to mount.

I'd like to use Powershell, and specify the drive letter assignment at the time of mounting, such that I can use scripting commands on files on the mounted ISO (drive), after which time, I'd like to dismount the ISO.

How can this be done?

Background: I'd like to script installation of MS Office given an ISO image.

CJBS
  • 195
  • 1
  • 11

5 Answers5

4

The following Powershell commands will mount the specified ISO image to the specified drive letter. The mountvol command requires elevation, so run Powershell as an Administrator:

# ISO image - replace with path to ISO to be mounted
$isoImg = "D:\en_visio_professional_2019_x86_x64_dvd_3b951cef.iso"
# Drive letter - use desired drive letter
$driveLetter = "Y:"

# Mount the ISO, without having a drive letter auto-assigned
$diskImg = Mount-DiskImage -ImagePath $isoImg  -NoDriveLetter

# Get mounted ISO volume
$volInfo = $diskImg | Get-Volume

# Mount volume with specified drive letter (requires Administrator access)
mountvol $driveLetter $volInfo.UniqueId

#
#
# Do work (e.g. MS Office installation - omitted for brevity)
#
#

# Unmount drive
DisMount-DiskImage -ImagePath $isoImg  

Background: this was a useful reference: https://www.derekseaman.com/2010/04/change-volume-drive-letter-with.html

CJBS
  • 195
  • 1
  • 11
1

This is a working version (for me at least)

    $isoImg = "C:\OrdinaDBA\ISO\en_sql_server_2019_developer_x64_dvd_baea4195.iso"
    $driveLetter = "X:\" # note the added ending backslash: mount fails if its not there :(
    
    
    #Check if elevated
    [Security.Principal.WindowsPrincipal]$user = [Security.Principal.WindowsIdentity]::GetCurrent();
    $Admin = $user.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator);
    
    if ($Admin) 
    {
        Write-Host "Administrator rights detected, continuing install.";
        
        Write-Host "Mount the ISO, without having a drive letter auto-assigned";
        $diskImg = Mount-DiskImage -ImagePath $isoImg  -NoDriveLetter -PassThru;
    
        #Write-Host "Get mounted ISO volume";
        $volInfo = $diskImg | Get-Volume
    
        #Write-Host "Mount volume with specified drive letter";
        mountvol $driveLetter $volInfo.UniqueId
      
        
        #Start-Sleep -Seconds 1
        #<do work>
        #Start-Sleep -Seconds 1
    
        Write-Host "DisMount ISO volume"; 
        DisMount-DiskImage -ImagePath $isoImg  # not used because SQL install is in an other powershell session
    
        Write-Host "Done";
        exit 0;
    
    }
    else
    {
        Write-Error "This script must be executed as Administrator.";
        exit 1;
    }
0

it is also possible to skip accesing via drive letter, like:

$imgDevice = Mount-DiskImage -ImagePath 'C:\SomeIso.iso' -NoDriveLetter -PassThru;
# dir content - do note the added ending backslash:
Get-ChildItem "$($imgDevice.DevicePath)\"
0

Here you can find all information about Get-DiskImage cmdlet: Get-DiskImage

This command gives you drive letter:

(Get-DiskImage -ImagePath "E:\vhd1.vhdx" | Get-Disk | Get-Partition | Get-Volume).DriveLetter
0

I couldn't get the above to work (Powershell 5 on WS2016), it failed on...

mountvol $driveLetter $volInfo.UniqueId

...Despite being run as Administrator.

But this did work

Get-WmiObject -Class Win32_volume -Filter 'DriveType=5' |
  Select-Object -First 1 |
  Set-WmiInstance -Arguments @{DriveLetter='Z:'}