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

时间:2016-03-08 21:16:14

标签: powershell wmi disk san

我试图通过PowerShell利用WMI来运行远程服务器上的SAN存储来获取Windows磁盘管理卷标。

我发现这样做的唯一方法是将卷设备ID(\\?\Volume{34243...}与物理磁盘设备ID(\\.\PHYSICALDRIVE01)相关联。

然而,我还没有找到如何将这两个领域联系在一起。这可能与WMI有关吗?

2 个答案:

答案 0 :(得分:1)

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

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*' }
}}

答案 1 :(得分:0)

我完成了一个脚本,该脚本从卷和磁盘WMI中收集最重要的内容。它用于从装有很多磁盘的远程桌面服务器获取信息,但是很难找到谁在使用哪个磁盘。它使用AD查询用户并将其与SID连接以查找文件路径。因此,首先要从不同的磁盘命令收集所有数据,然后组合输出。将磁盘数据与卷数据绑定的最重要的命令是显示deviceid的get-partition

Function Get-VHDMount {

[cmdletbinding()]

Param(
  [Parameter(Position=0,ValueFromPipeline=$True)]
  [ValidateNotNullorEmpty()] 
  [OBJECT[]]$Computername,
  [STRING]$RDSPATH = '\\rdsprofiles'
)
    foreach ($computer in $Computername) {

        $RDSItems      = (Get-ChildItem $RDSPATH -Recurse -Filter *.vhdx)
        $VolumeInfo    = invoke-command -ComputerName $computer -scriptblock  {Get-Volume | select *}
        $VHDMountInfo  = Get-WmiObject Win32_Volume -ComputerName $computer |where Label -eq 'user Disk' 
        $partitioninfo = invoke-command -ComputerName $computer -scriptblock  {Get-Partition | Select-Object DiskNumber, @{n='VolumeID';e={$_.AccessPaths | Where-Object { $_ -like '\\?\volume*' }}}}

        foreach ($VHDmount in $VHDMountInfo) {
            $adinfo = Get-ADUser ($VHDmount.name | Split-Path -Leaf)

            [PSCUSTOMOBJECT]@{
                Computername = $computer
                username     = $VHDmount.name | Split-Path -Leaf
                displayname  = $adinfo.name
                SID          = $adinfo.SID
                deviceid     = $VHDmount.deviceid
                capacity     = ([MATH]::ROUND(($VHDmount.capacity) / 1gb))
                HealthStatus = ($VolumeInfo | where ObjectId -eq ($VHDmount.deviceid)).HealthStatus
                DiskNumber   = ($partitioninfo | where Volumeid -eq ($VHDmount.deviceid)).DiskNumber
                Path         = ($RDSItems | where fullname -like "*$($adinfo.SID)*").FullName
            }
        }
    }
}