powershell - 将物理设备 ID 与卷设备 ID 相关联

标签 powershell wmi disk san

我正在尝试通过 PowerShell 使用 WMI 来运行远程服务器上的 SAN 存储,以获取 Windows 磁盘管理卷标。

我发现执行此操作的唯一方法是将卷设备 ID (\\?\Volume{34243...} 与物理磁盘设备 ID (\\.\PHYSICALDRIVE01)。

但是,我一直无法找到如何将这两个字段链接在一起。这可以通过 WMI 实现吗?

最佳答案

对于分配了驱动器号的卷,您可以将磁盘和卷关联起来,如下所示:

Get-WmiObject Win32_DiskDrive | ForEach-Object {
  $disk = $_
  $partitions = "ASSOCIATORS OF " +
                "{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
                "WHERE AssocClass = Win32_DiskDriveToDiskPartition"
  Get-WmiObject -Query $partitions | ForEach-Object {
    $partition = $_
    $drives = "ASSOCIATORS OF " +
              "{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
              "WHERE AssocClass = Win32_LogicalDiskToPartition"
    Get-WmiObject -Query $drives | ForEach-Object {
      $driveLetter = $_.DeviceID
      $fltr        = "DriveLetter='$driveLetter'"
      New-Object -Type PSCustomObject -Property @{
        Disk        = $disk.DeviceID
        DriveLetter = $driveLetter
        VolumeName  = $_.VolumeName
        VolumeID    = Get-WmiObject -Class Win32_Volume -Filter $fltr |
                      Select-Object -Expand DeviceID
      }
    }
  }
}

否则它doesn't seem possible with WMI .

在 Windows 8/Server 2012 或更高版本上,您可以使用 Get-Partition不过 cmdlet:

Get-Partition | Select-Object DiskNumber, DriveLetter, @{n='VolumeID';e={
  $_.AccessPaths | Where-Object { $_ -like '\\?\volume*' }
}}

关于powershell - 将物理设备 ID 与卷设备 ID 相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35878166/

相关文章:

powershell - 使用管理员权限重新启动 powershell 并继续当前脚本

powershell - http 的 Set-ItemProperty 删除 IIS 中网站的现有绑定(bind)

windows - 每个开发人员都应该知道的 PowerShell 脚本

powershell - 安装 IIS 作为 Octopus Deploy 中的一个步骤

windows - Powershell 删除配置文件脚本 - 错误检查不起作用

C : write() fails while writing to a RAW disk after specific size limit reached

.net - 我可以使用 WMI 和 System.Management 命名空间测试软盘驱动器吗?

c# - Microsoft.Web.管理: System. UnauthorizedAccessException

macos - 无法清理 macOS Catalina 上的空间。 "Other"从 500GB 中取出 370GB,其他工具显示了一半

database - sstableLoader 的意外行为