WMI 工作:磁碟和檔案系統

磁片和檔案系統的 WMI 工作會取得磁片磁碟機硬體狀態和邏輯磁片區的相關資訊。 如需其他範例,請參閱 位於 的 TechNet ScriptCenter https://www.microsoft.com/technet

本主題所示的腳本範例只會從本機電腦取得資料。 如需如何使用腳本從遠端電腦取得資料的詳細資訊,請參閱 連線到遠端電腦上的 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 類別,並擷取 UserDiskSpaceUsed 屬性的值。
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") 

針對每個 wmiDiskDrive In wmiDiskDrives WScript.Echo 「磁片磁碟機標題:」 & wmiDiskDrive.Caption & VbNewLine & 「DeviceID: 」 「 & (」 & wmiDiskDrive.DeviceID & 「) 」

'使用磁片磁碟機裝置識別碼' 尋找相關聯的分割區查詢 = 「ASSOCIATORS OF {Win32_DiskDrive.DeviceID='」 _ & wmiDiskDrive.DeviceID & 「'} WHERE AssocClass = Win32_DiskDriveToDiskPartition」
設定 wmiDiskPartitions = wmiServices.ExecQuery (查詢)

針對每個 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

`