WMI 任务:磁盘和文件系统

磁盘和文件系统的 WMI 任务会获取有关磁盘驱动器硬件状态和逻辑卷的信息。 有关其他示例,请通过 https://www.microsoft.com/technet 访问 TechNet ScriptCenter。

本主题中所示的脚本示例仅从本地计算机获取数据。 有关如何使用脚本从远程计算机获取数据的详细信息,请参阅连接到远程计算机上的 WMI

以下过程介绍了如何运行脚本。

运行脚本

  1. 复制代码并将其保存在扩展名为 .vbs 的文件中,例如 filename.vbs。 确保文本编辑器不会向该文件添加 .txt 扩展名。
  2. 打开命令提示符窗口并导航到保存该文件的目录。
  3. 在命令提示符下键入 cscript filename.vbs。
  4. 如果无法访问事件日志,请进行检查以查看是否正从提升的命令提示符运行。 某些事件日志(例如安全事件日志)可能受用户访问控制 (UAC) 的保护。

注意

默认情况下,cscript 会在命令提示符窗口中显示脚本的输出。 由于 WMI 脚本可以生成大量输出,因此可能需要将输出重定向到文件。 在命令提示符下键入 cscript filename.vbs > outfile.txt 以将 filename.vbs 脚本的输出重定向到 outfile.txt。

下表列出了可用于从本地计算机获取各种类型的数据的脚本示例。

如何实现... WMI 类或方法
…了解每位用户当前在计算机上使用了多少磁盘空间? 如果使用的是磁盘配额,请使用 Win32_DiskQuota 类并检索 User 和 DiskSpaceUsed 属性的值。
VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colQuotas = objWMIService.ExecQuery ("Select * from Win32_DiskQuota")
For each objQuota in colQuotas
    Wscript.Echo "Volume: "& vbTab &  objQuota.QuotaVolume
    Wscript.Echo "User: "& vbTab &  objQuota.User      
    Wscript.Echo "Disk Space Used: " & vbTab &  objQuota.DiskSpaceUsed
Next
PowerShell
$strComputer = "."
$colItems = Get-WmiObject -Class Win32_DiskQuota -ComputerName $strComputer
foreach ($objQuota in $colItems) 
{ 
    "Volume: " + $objQuota.QuotaVolume
    "User: " + $objQuota.User      
    "Disk Space Used: " + $objQuota.DiskSpaceUsed
}
…确定何时已将可移动驱动器添加到计算机或从计算机中移除?

使用查询 Win32_VolumeChangeEvent 类的监视脚本。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService. ExecNotificationQuery( "Select * from Win32_VolumeChangeEvent")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.DriveName
    Wscript.Echo objLatestEvent.EventType
    Wscript.Echo objLatestEvent.Time_Created
Loop
…确定 CD 是否在 CD-ROM 驱动器中?

使用 Win32_CDROMDrive 类和 MediaLoaded 属性。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( "Select * from Win32_CDROMDrive")
For Each objItem in colItems
    Wscript.Echo "Device ID: " & objItem.DeviceID
    Wscript.Echo "Media Loaded: " & objItem.MediaLoaded
Next
PowerShell
$strComputer = "."
$colItems = Get-WmiObject -Class Win32_CDROMDrive -ComputerName $strComputer
foreach ($objItem in $colItems)
{
    "Device ID: " + $objItem.DeviceID
    "MediaLoaded: " + $objItem.MediaLoaded
}
…确定磁盘是否在软盘驱动器中?

使用 Win32_LogicalDisk 类并检查 FreeSpace 属性。 如果值为 Null,则驱动器中没有磁盘。

VB
              
              strComputer = "." Set objWMIService = GetObject( "winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery ("Select * From Win32_LogicalDisk Where DeviceID = 'A:'") 

For Each objItem in colItems intFreeSpace = objItem.FreeSpace If IsNull(intFreeSpace) Then Wscript.Echo "There is no disk in the floppy drive." Else Wscript.Echo "There is a disk in the floppy drive." End If Next

PowerShell
              
              $strComputer = "." $colItems = Get-WmiObject -Class Win32_LogicalDisk -Namespace "root\cimv2" -ComputerName $strComputer | ` Where-Object { $_.DeviceID -eq "A:" } 

foreach ($objItem in $colItems) { $intFreeSpace = $objItem.FreeSpace if ($intFreeSpace -eq $null) { "There is no disk in the floppy drive." } else { "There is a disk in the floppy drive." } }

…区分固定硬盘和可移动硬盘?

使用 Win32_LogicalDisk 类并检查 DriveType 属性的值。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
    Wscript.Echo "DeviceID: "& vbTab _
        &  objDisk.DeviceID       
    Select Case objDisk.DriveType
        Case 1
            Wscript.Echo "No root directory. " & "Drive type could not be " & "determined."
        Case 2
            Wscript.Echo "DriveType: "& vbTab &  "Removable drive."
        Case 3
            Wscript.Echo "DriveType: "& vbTab &  "Local hard disk."
        Case 4
            Wscript.Echo "DriveType: "& vbTab &  "Network disk."      
        Case 5
            Wscript.Echo "DriveType: "& vbTab &  "Compact disk."      
        Case 6
            Wscript.Echo "DriveType: "& vbTab &  "RAM disk."   
        Case Else
            Wscript.Echo "Drive type could not be" & " determined."
    End Select
Next

PowerShell
              
              $strComputer = "." $colDisks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $strComputer 

foreach ($objDisk in $colDisks) { "DeviceID: " + $objDisk.deviceID switch ($objDisk.DriveType) { '1' { "No root directory. Drive type could not be determined." } '2' { "DriveType: Removable drive." } '3' { "DriveType: Local hard disk." } '4' { "DriveType: Network disk." } '5' { "DriveType: Compact disk." } '6' { "DriveType: RAM disk." } default: { "Drive type could not be determined." } } }

…确定驱动器上使用的是什么文件系统?

使用 Win32_LogicalDisk 类和 FileSystem 属性。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
    Wscript.Echo "DeviceID: " & objDisk.DeviceID       
    Wscript.Echo "File System: " & objDisk.FileSystem
Next
…确定驱动器上有多少可用空间?

使用 Win32_LogicalDisk 类和 FreeSpace 属性。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
    Wscript.Echo "DeviceID: " & objDisk.DeviceID       
    Wscript.Echo "Free Disk Space: " & objDisk.FreeSpace
Next
…确定驱动器的大小?

使用 Win32_LogicalDisk 类和 Size 属性。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
    Wscript.Echo "DeviceID: " & objDisk.DeviceID       
    Wscript.Echo "Disk Size: " & objDisk.Size
Next
…找出计算机上映射了哪些驱动器?

使用 Win32_MappedLogicalDisk 类。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService. ExecQuery("Select * from Win32_MappedLogicalDisk")
For Each objDisk in colDisks
    Wscript.Echo "Device ID: " & objDisk.DeviceID
    Wscript.Echo "Name: " & objDisk.Name
    Wscript.Echo "Free Space: " & objDisk.FreeSpace
    Wscript.Echo "Size: " & objDisk.Size
Next
…对硬盘进行碎片整理?

使用 Win32_Volume 类和 Defrag 方法。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colVolumes = objWMIService.ExecQuery ("Select * from Win32_Volume Where Name = 'K:\\'")
For Each objVolume in colVolumes
     errResult = objVolume.Defrag()
Next
…检测哪个驱动器号与逻辑磁盘分区关联?
  1. Win32_DiskDrive 类开始,使用 DeviceID 属性和 Win32_DiskDriveToDiskPartition 关联类查询 Win32_DiskPartition 的实例。 现在,物理驱动器上有了分区集合。
  2. 使用 Win32_DiskPartition.DeviceID 属性和 Win32_LogicalDiskToPartition 关联类查询表示分区的 Win32_LogicalDisk
  3. Win32_LogicalDisk.DeviceID 获取驱动器号。

VB
              
              ComputerName = "." Set wmiServices = GetObject ( _ "winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName) ' Get physical disk drive Set wmiDiskDrives = wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive") 

For Each wmiDiskDrive In wmiDiskDrives WScript.Echo "Disk drive Caption: " & wmiDiskDrive.Caption & VbNewLine & "DeviceID: " & " (" & wmiDiskDrive.DeviceID & ")"

'Use the disk drive device id to ' find associated partition query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _ & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set wmiDiskPartitions = wmiServices.ExecQuery(query)

For Each wmiDiskPartition In wmiDiskPartitions 'Use partition device id to find logical disk Set wmiLogicalDisks = wmiServices.ExecQuery _ ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _ & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")

For Each wmiLogicalDisk In wmiLogicalDisks WScript.Echo "Drive letter associated" _ & " with disk drive = " _ & wmiDiskDrive.Caption _ & wmiDiskDrive.DeviceID _ & VbNewLine & " Partition = " _ & wmiDiskPartition.DeviceID _ & VbNewLine & " is " _ & wmiLogicalDisk.DeviceID Next Next Next

脚本和应用程序的 WMI 任务

WMI C++ 应用程序示例

TechNet ScriptCenter

`