使用 PowerShell WMI 或 Diskpart 检测选定磁盘上已卸载的卷

发布于 2024-12-28 10:28:04 字数 416 浏览 0 评论 0原文

如何将未安装卷链接到物理磁盘?

假设我需要在磁盘 3 上查找并安装已卸载的卷,编号为 DiskpartWMIC 或 PowerShell WMI。如何使用脚本找出磁盘 3 的哪些卷未安装?或者,给定的未安装卷(没有驱动器盘符)驻留在哪个物理磁盘上?

卸载卷后,该卷不存在任何逻辑磁盘或安装点。我想可以使用 GetRelated 方法找到关系,但我找不到适合该任务的代码示例。

How do I link unmounted volumes to physical disks?

Say I need to find and mount unmounted volumes on disk 3 as numbered by Diskpart or WMIC, or PowerShell WMI. How do I find out, with a script, what volumes of disk 3 aren't mounted? Or, alternatively, what physical disk a given unmounted volume (having no DriveLetter) resides on?

When a volume is unmounted, no logical disk or mount point exist for it. I suppose the relation can be found with GetRelated method, but I can't find such a code example suited for the task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小ぇ时光︴ 2025-01-04 10:28:04

尝试一下,它将:

  • 使用 WMI 获取给定驱动器索引 $targetDisk 的所有已卸载分区 使用
  • diskpart 脚本将目标磁盘上发现的分区装载到下一个可用驱动器号。

使用 GetRelated 方法就是为了了解您需要关联的内容。它有助于了解 WMI 类代表您正在寻找的 Win32_DiskPartition。在您的情况下,您想要查找未与逻辑磁盘(已卸载)关联的分区,因此我们会查找没有关联的 Win32_LogicalDiskWin32_DiskPartition 实例。

由于您只想在特定物理磁盘上卸载卷,因此我们需要进一步关联类。为此,我们需要获取 Win32_DiskPartition 关联的 Win32_DiskDrive 实例。

$targetDisk = 3

$unmounted = gwmi -class win32_DiskPartition | ? {
    ($_.GetRelated('Win32_LogicalDisk')).Count -eq 0 
}

if ($unmounted) {
    $commands = @()
    $unmounted | ? { $_.GetRelated('Win32_DiskDrive') | ? { $_.Index -eq $targetDisk} } | % {
        $commands += "select disk {0}" -f $_.DiskIndex
        $commands += "select partition {0}" -f ($_.Index + 1)
        $commands += "assign"
    }

    $tempFile = [io.path]::GetTempFileName()
    $commands | out-file $tempFile -Encoding ASCII

    $output = & diskpart.exe /s $tempFile 2>&1
    if ($LASTEXITCODE -ne 0) {
        Write-Error $output
    }
}

Give this a try, it will:

  • Get all unmounted partitions for a given drive index $targetDisk using WMI
  • Mount the discovered partitions on the target disk to the next available drive letter using a diskpart script.

Using the GetRelated method is all about knowing what you need to relate. It helps to know what WMI class represents what you are looking for Win32_DiskPartition. In your case you want to find the partitions which are not associated with a logical disk (unmounted) so we look for instances of Win32_DiskPartition which don't have an associated Win32_LogicalDisk.

Since you only want unmounted volumes on a particular physical disk we need to further associate classes. To do this we need to get Win32_DiskPartition's associated Win32_DiskDrive instance.

$targetDisk = 3

$unmounted = gwmi -class win32_DiskPartition | ? {
    ($_.GetRelated('Win32_LogicalDisk')).Count -eq 0 
}

if ($unmounted) {
    $commands = @()
    $unmounted | ? { $_.GetRelated('Win32_DiskDrive') | ? { $_.Index -eq $targetDisk} } | % {
        $commands += "select disk {0}" -f $_.DiskIndex
        $commands += "select partition {0}" -f ($_.Index + 1)
        $commands += "assign"
    }

    $tempFile = [io.path]::GetTempFileName()
    $commands | out-file $tempFile -Encoding ASCII

    $output = & diskpart.exe /s $tempFile 2>&1
    if ($LASTEXITCODE -ne 0) {
        Write-Error $output
    }
}
陈年往事 2025-01-04 10:28:04

将此代码集成到上面的答案中:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_Volume Where Name = 'D:\\'")

For Each objItem in colItems
    objItem.AddMountPoint("W:\\Scripts\\")
Next

它通过使用卷 DeviceID 而不是其 DriveLetter 在 Windows 7 PowerShell 中进行查找,并将卷与磁盘 3 相关联,如上面的答案所示。可以使用与上面类似的方法(AddMountPoint 或 Mount),但不使用 Diskpart。

Integrate this code into the above answer:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_Volume Where Name = 'D:\\'")

For Each objItem in colItems
    objItem.AddMountPoint("W:\\Scripts\\")
Next

It looks in Windows 7 PowerShell by using the Volume DeviceID instead of its DriveLetter, and relating the Volume to Disk 3 as shown in the above answer. A similar approach (AddMountPoint or Mount) can be used as above, but without using Diskpart.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文