Windows

如何根據事件日誌中報告的捲名辨識 WMI 中的捲?

  • February 2, 2014

我有一個報告以下錯誤的 Windows 2008R2 伺服器:

磁碟上的文件系統結構已損壞且無法使用。請在卷\Device\HarddiskVolume2上執行 chkdsk 實用程序。

使用 Powershell 和 WMI 如何在查詢時辨識這是哪個卷Win32_Volume

例如,如果我這樣做:

Get-WmiObject Win32_Volume

我得到了伺服器上所有捲的列表,但是沒有一個Win32_Volume類屬性使用(看起來是)這個“友好”名稱 - \Device\HarddiskVolume2。我可以看到有一個DeviceID屬性返回這樣的值:

DeviceID    : \\?\Volume{4bc3df2a-65c7-11e0-9c33-806e6f6e6963}\

還有一個Name屬性,但這只是分配給卷的驅動器號。其他任何屬性都沒有一個與事件日誌中報告的內容相近的值。

我知道我可以解析輸出fltmc volumesDISKPART獲取此資訊,但必須有一種方法可以在 PowerShell 腳本中使用 WMI 來獲取此資訊。

我還查看了Win32_DiskDrive,Win32_DiskPartitionWin32_LogicalDisk類,但沒有提到類似的屬性值\Device\HarddiskVolume2

看看這段程式碼: 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
       }
   }
}

引用自:https://serverfault.com/questions/571354