如何在 Powershell 中将 ISO 映像挂载到指定的驱动器号?

如何在 Powershell 中将 ISO 映像挂载到指定的驱动器号?

我有一个想要安装的 ISO 映像(在本例中为 MS Office ISO)。

我想使用 Powershell,并在挂载时指定驱动器号分配,这样我就可以在挂载的 ISO(驱动器)上的文件上使用脚本命令,之后我想卸载 ISO。

如何才能做到这一点?

背景:我想根据 ISO 映像编写 MS Office 的安装脚本。

答案1

以下 Powershell 命令将把指定的 ISO 映像挂载到指定的驱动器号。安装卷命令需要提升权限,因此运行 Powershell作为管理员

# 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  

背景:这是一个有用的参考:https://www.derekseaman.com/2010/04/change-volume-drive-letter-with.html

答案2

我无法使上述操作正常工作(WS2016 上的 Powershell 5),它失败了......

mountvol $driveLetter $volInfo.UniqueId

...尽管以管理员身份运行。

但这确实有效

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

答案3

这是一个可用的版本(至少对我来说)

    $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;
    }

答案4

也可以跳过通过驱动器号访问,例如:

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

相关内容