strInputFile = "computers.txt"
strOutputFile = "hard_disk_space.csv"
Dim arrDrives
arrDrives = Array("C","D","E","F")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const adVarChar = 200
Const MaxCharacters = 255
Dim DataList
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "Server", adVarChar, MaxCharacters
For Each strDrive In arrDrives
DataList.Fields.Append strDrive & " Size", adVarChar, MaxCharacters
DataList.Fields.Append strDrive & " Free Space", adVarChar, MaxCharacters
DataList.Fields.Append strDrive & " Percent Free", adVarChar, MaxCharacters
Next
DataList.Open
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading)
While Not objInputFile.AtEndOfStream
strComputer = objInputFile.ReadLine
Get_Free_Space_Details(strComputer)
Wend
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
strHeader = """SERVER"""
For Each strDrive In arrDrives
strHeader = strHeader & ",""" & strDrive & " SIZE"",""" & strDrive & " FREE SPACE"",""" & strDrive & " PERCENT FREE"""
Next
objOutputFile.WriteLine strHeader
DataList.MoveFirst
While Not DataList.EOF
strLine = """" & DataList("Server") & """"
For Each strDrive In arrDrives
strLine = strLine & ",""" & DataList(strDrive & " Size") & """,""" & DataList(strDrive & " Free Space") & """,""" & DataList(strDrive & " Percent Free") & """"
Next
objOutputFile.WriteLine strLine
DataList.MoveNext
Wend
DataList.Close
objOutputFile.Close
MsgBox "Done. Please see " & strOutputFile
'==============
Sub Get_Free_Space_Details(strComputer)
DataList.AddNew
DataList("Server") = strComputer
If Ping(strComputer) = True Then
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If Err.Number = 0 Then
Err.Clear
On Error GoTo 0
For Each strDrive In arrDrives
Set colDisks = objWMIService.ExecQuery("Select FreeSpace,Size From Win32_LogicalDisk Where DriveType = 3 And DeviceID = '" & strDrive & ":'")
For Each objDisk In colDisks
intFreeSpace = objDisk.FreeSpace
intTotalSpace = objDisk.Size
pctFreeSpace = intFreeSpace / intTotalSpace
DataList(strDrive & " Size") = Round(intTotalSpace / 1024 / 1024 / 1024, 2) & " GB"
DataList(strDrive & " Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
DataList(strDrive & " Percent Free") = FormatPercent(pctFreeSpace)
Next
Next
Set objDisk = Nothing
Set colDisks = Nothing
Set objWMIService = Nothing
Else
For Each strDrive In arrDrives
DataList(strDrive & " Size") = "WMI ERROR"
DataList(strDrive & " Free Space") = "WMI ERROR"
DataList(strDrive & " Percent Free") = "WMI ERROR"
Next
End If
Else
For Each strDrive In arrDrives
DataList(strDrive & " Size") = "OFFLINE"
DataList(strDrive & " Free Space") = "OFFLINE"
DataList(strDrive & " Percent Free") = "OFFLINE"
Next
End If
DataList.Update
End Sub
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shell")
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
|