共用方式為


WMI 工作:磁碟和文件系統

磁碟和文件系統的 WMI 工作會取得磁碟驅動器硬體狀態和邏輯磁碟區的相關信息。 如需其他範例,請參閱 TechNet ScriptCenter at 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 = “.”設定 wmiServices = GetObject (_ “winmgmts:{impersonationLevel=Impersonate}!{ ” & ComputerName) ' 取得實體磁碟驅動器 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(query)

針對每個 wmiDiskPartition In wmiDiskPartitions '使用分割區裝置標識符尋找邏輯磁碟 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

`