3

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

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

但是,我无法找到如何将这两个字段链接在一起。这可能与 WMI 吗?

4

2 回答 2

2

对于分配了驱动器号的卷,您可以像这样关联磁盘和卷:

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

否则WMI 似乎不可能

但是,在 Windows 8/Server 2012 或更高版本上,您可以使用Get-Partitioncmdlet:

Get-Partition | Select-Object DiskNumber, DriveLetter, @{n='VolumeID';e={
  $_.AccessPaths | Where-Object { $_ -like '\\?\volume*' }
}}
于 2016-03-08T23:20:53.253 回答
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
            }
        }
    }
}
于 2019-11-12T13:39:55.540 回答