如何根据事件日志中报告的卷名称识别 WMI 中的卷?

Kev*_*Kev 5 windows powershell diskmanagement wmi windows-server-2008-r2

我的 Windows 2008R2 服务器报告以下错误:

磁盘上的文件系统结构已损坏且无法使用。请在卷\Device\HarddiskVolume2上运行 chkdsk 实用程序。

使用 Powershell 和 WMI 在查询时如何识别这是哪个卷Win32_Volume

例如,如果我这样做:

Get-WmiObject Win32_Volume
Run Code Online (Sandbox Code Playgroud)

我得到了服务器上所有卷的列表,但是没有一个Win32_Volume类属性使用(看起来是)这个“友好”名称 - \Device\HarddiskVolume2。我可以看到有一个DeviceID属性返回如下值:

DeviceID    : \\?\Volume{4bc3df2a-65c7-11e0-9c33-806e6f6e6963}\
Run Code Online (Sandbox Code Playgroud)

还有一个Name属性,但这只是分配给卷的驱动器号。其他属性的值都与事件日志中报告的值相差甚远。

fltmc volumes我知道我可以解析或 的输出来DISKPART获取此信息,但必须有一种方法可以在 PowerShell 脚本中使用 WMI 来获取此信息。

我还查看了Win32_DiskDrive,Win32_DiskPartitionWin32_LogicalDisk类,但没有提到类似的属性值\Device\HarddiskVolume2

小智 3

看看这段代码:http://poshcode.org/4768它似乎做了你需要的转换,看看你想要什么,你也许可以调整它以满足你的需要,如果你需要帮助,请告诉我但我认为你可以自己弄清楚。

function Get-DevicePath
{
<#
.SYNOPSIS

    Returns the device paths for each volume.

    Author: Matthew Graeber (@mattifestation)
    License: BSD 3-Clause

.DESCRIPTION

    Get-DevicePath returns the corresponding device path for each drive letter. This is useful for converting device paths to drive letters.

.EXAMPLE

    Get-DevicePath

    DevicePath              DriveLetter
    ----------              -----------
    \Device\HarddiskVolume2 D:
    \Device\HarddiskVolume4 C:

.OUTPUTS

    PSObject[]

    For each mount point, a PSObject is returned representing the drive letter and device path.
#>

    # Utilize P/Invoke in order to call QueryDosDevice. I prefer using 
    # reflection over Add-Type since it doesn't require compiling C# code.
    $DynAssembly = New-Object System.Reflection.AssemblyName('SysUtils')
    $AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
    $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('SysUtils', $False)

    # Define [Kernel32]::QueryDosDevice method
    $TypeBuilder = $ModuleBuilder.DefineType('Kernel32', 'Public, Class')
    $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod('QueryDosDevice', 'kernel32.dll', ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [UInt32], [Type[]]@([String], [Text.StringBuilder], [UInt32]), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
    $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
    $SetLastError = [Runtime.InteropServices.DllImportAttribute].GetField('SetLastError')
    $SetLastErrorCustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('kernel32.dll'), [Reflection.FieldInfo[]]@($SetLastError), @($true))
    $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
    $Kernel32 = $TypeBuilder.CreateType()

    $Max = 65536
    $StringBuilder = New-Object System.Text.StringBuilder($Max)

    Get-WmiObject Win32_Volume | ? { $_.DriveLetter } | % {
        $ReturnLength = $Kernel32::QueryDosDevice($_.DriveLetter, $StringBuilder, $Max)

        if ($ReturnLength)
        {
            $DriveMapping = @{
                DriveLetter = $_.DriveLetter
                DevicePath = $StringBuilder.ToString()
            }

            New-Object PSObject -Property $DriveMapping
        }
    }
}
Run Code Online (Sandbox Code Playgroud)