我有一个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属性,但这只是分配给该卷的驱动器号。 没有其他属性的值与事件日志中报告的内容类似。
我知道我可以parsing来自fltmc volumes或DISKPART的输出以获取此信息,但是必须有一种方法可以在PowerShell脚本中使用WMI获取此信息。
我还查看了Win32_DiskDrive , Win32_DiskPartition和Win32_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 } } }
不知道这是否是你正在寻找的答案,但看起来不像这个数据在Windows中完全列出,从我可以从命令行看到。 我试图从diskpart中取出数据来显示信息,但没有列出设备名称。 在一个侧面说明,看起来像更多的磁盘命令已被添加到2012年,如get-volume,get-disk和get-physicaldisk,但是这对2008年没有帮助。
有几个第三方公用事业公司会这样做
这两个实用程序都有命令行选项,所以应该能够在PowerShell脚本中使用它们。