香港新浪網 MySinaBlog
« 上一篇 | 下一篇 »
Emily | 1st Mar 2007, 08:39 AM | Script | (1072 Reads)

WMI Example: Local Disk Space

All Windows Platform

 

'---------------------------------------------------------------------------------------
' Module    : Sheet1
' DateTime  : 1/3/2007 08:43
' Author    :
' Purpose   : Show free local diskspace for network computer
'---------------------------------------------------------------------------------------
'

Sub FreeDiskSpace()
Dim strComputer
Dim freespace As Long

Cells(1, 1) = "PC Name"
Cells(1, 2) = "Free Space"

Set fso = CreateObject("Scripting.FileSystemObject")
x = 2
y = 2

Set bF = fso.opentextFile("C:list.txt") ' PC List, one PC per line
Do While bF.AtEndofStream <> True
    strComputer = bF.readline
    On Error Resume Next
    Cells(x, 1) = strComputer

    Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/CIMV2")
    If Err.Number <> 0 Then
        Cells(x, y) = "Computer not found or Unavailable"
        y = y + 1
    Else
        Set
colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk", "WQL", _
            wbemFlagReturnImmediately + wbemFlagForwardOnly)
        For Each objitem In colItems
            If objitem.DriveType = 3 Then  ' Local Disk Only
                freespace = Round(objitem.freespace / 1073741824, 2)
                Cells(x, y) = objitem.Caption & " " & freespace & " GB"
                y = y + 1
            End If
        Next

        Err.Clear
    End If
    x = x + 1
    y = 2
Loop

Cells.Columns.AutoFit
Cells(1, 1).Select

End Sub

mySinaBlog 不支援斜線,斜線現用全形\取代 ,請改回。