A few years ago I decided to write a script to automate this process for me as at that point in time I was managing about 30 servers each with 3 maybe 4 drives.
The script I had back then was pretty neat and reported all the disk space statuses of servers listed in a text file, just by dragging a text file with the servers listed in it onto the VBScript.
The problem with the original script was that it checked each server for drives C: to L: so I had a piece of code for each drive like this:
'C$ share Set drv = fso.GetDrive(fso.GetDriveName("\\" + SrvName + "\C$_
"))
writefile.Write (tblstart2)
writefile.writeline (tcspace1 & SrvName & fin) & _
(tcspace2 & "C:\" & fin)& _
(tcspace3 & drv.volumename & fin) & _
(tcspace4 & FormatNumber(drv.TotalSize / 1073741824,_
2)& "GB" & fin) & _
(tcspace5 & FormatNumber((drv.TotalSize / 1073741824)_
-(drv.FreeSpace / 1073741824), 2) & "GB" & fin) & _
(tcspace6 & FormatNumber((drv.FreeSpace / 1073741824)_
, 2) & "GB" & fin) & _
(endtr)
writefile.writeline (tblend)
'writefile.write SrvName + " " + vbTab + "C$" + vbTab + _
drv.filesystem + _ 'vbTab + vbTab + FormatNumber(drv.TotalSize / 1024, 0)+ " _
Kb" + vbTAb + _ 'FormatNumber((drv.TotalSize / 1073741824)-(drv.FreeSpace /_
1073741824), 2) + " GB" + vbTab + FormatNumber(drv.FreeSpace _
/ 1024, 0) +_ '" GB" + vbCrlf totCap = totCap + (drv.TotalSize / 1073741824) totUsed = totUsed + (drv.TotalSize / 1073741824)-(drv._
FreeSpace / 1073741824)
totFree = totFree + drv.FreeSpace / 1073741824
Set drv = Nothing
so I had that from C$ to L$
The new script was a lot simpler:
strComputer = SrvName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer &_
"\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DriveType = 3")
For Each objDisk in colDisks
intFreeSpace = objDisk.FreeSpace
intTotalSpace = objDisk.Size
'Wscript.Echo strComputer, objDisk.DeviceID,_
objDisk.FreeSpace pctFreeSpace = intFreeSpace / intTotalSpace usedSpace = intTotalSpace - intFreeSpace GigFree = FormatNumber (intFreeSpace / 1073741824, 2) Gigtot = FormatNumber (intTotalSpace / 1073741824, 2) totUsedSpace = FormatNumber (usedSpace / 1073741824, 2) writefile.Writeline (strComputer & " " _
& objDisk.DeviceID & " " & Gigtot & " " & _
totUsedSpace & " " & GigFree & " " & _
FormatPercent(pctFreeSpace))
Next
That is the main part of the script that checks all the disks on all the servers on a text file.
The complete script is at the bottom on the post. I have just done a simple copy and paste from Notepad++ as the code snippet plug-in for live writer doesn’t like the width of the blog skin at the moment :)
All I need to do now is create a text file with all my servers in it (each server on a new line) and then drag that file over the VBS file and it will run all those servers against the list.
Looking at all the possibilities I cold potentially get more information out of the servers I run it against so watch this space!
The first revision of the script outputted to a text file called Disk Report.csv. however I hadn’t accounted for the TB disks I had in a couple of the servers so the comma separated txt file proved to be problematic as it would spit up 1,024 GB into two columns.
So I ended up using a tab separated txt file.
The next thing was to save the file with the date in the file name so I could save more than one file!
VBScripting has limited date formats so I needed a custom function that would set the date format to ISO (YYYYMMDD)
I found a function already created on the link below.
----------Final Script Below here---------------------
'VBScript to check the disk space on a text file containing a list of servers
'created by Andrew Kemp on 12th March 2009
'Date Function by: http://aspadvice.com/blogs/xsherry/archive/2005/01/27/2166.aspx
'Revision 6
dim filesys, text, readfile, contents, datenow
Function GetDate(dateVal, delimiter)
'To comply with Option Explict
Dim dateMonth, dateDay
dateVal = CDate(dateVal)
' Converts the dateVal parameter to a date.
' This will cause an error and cause the function
' to exit if the value passed is not a real date.
delimiter = CStr(delimiter)
' Converts the delimiter parameter, which designates
' the delimiting character between the datepart values
' to a string value. If you don't want a delimiting
' character, (such as / or -) then you'd simply pass
' in an empty string for this parameter.
dateMonth = Month(dateVal)
dateDay = Day(dateVal)
GetDate = CStr(Year(dateVal)) & delimiter
If dateMonth < 10 Then
GetDate = GetDate & "0"
End If
GetDate = GetDate & CStr(dateMonth) & delimiter
If dateDay < 10 Then
GetDate = GetDate & "0"
End If
GetDate = GetDate & CStr(dateDay)
End Function
' set datenow = GetDate (Now, "")
'Create a Shell and FileSystem Object
Set WshShell = WScript.CreateObject("WScript.Shell")
set filesys = CreateObject("Scripting.FileSystemObject")
Set text = filesys.CreateTextFile("c:\Disk Report-" & GetDate (Now, "") & ".txt")
text.Writeline ("Server Name Drive Total Disk Space (GB) Used Space (GB) Free Space (GB) Free Space (%)")
text.close
'Read the Argument, pathname of file dropped onto script
Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
filname = objArgs(I)
'Open the file
set readfile = filesys.OpenTextFile(filname, 1, false)
'Get a line from the input file, ignore a semi-colon
do while readfile.AtEndOfStream = False
Name = readfile.ReadLine
Getchar = instr(Name,";")
if Getchar = "0" then ShowDriveInfo(Name)
loop
readfile.close
Next
Sub ShowDriveInfo(SrvName)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set writefile = fso.OpenTextFile("c:\Disk Report.txt",8, True)
strComputer = SrvName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DriveType = 3")
For Each objDisk in colDisks
intFreeSpace = objDisk.FreeSpace
intTotalSpace = objDisk.Size
'Wscript.Echo strComputer, objDisk.DeviceID, objDisk.FreeSpace
pctFreeSpace = intFreeSpace / intTotalSpace
usedSpace = intTotalSpace - intFreeSpace
GigFree = FormatNumber (intFreeSpace / 1073741824, 2)
Gigtot = FormatNumber (intTotalSpace / 1073741824, 2)
totUsedSpace = FormatNumber (usedSpace / 1073741824, 2)
writefile.Writeline (strComputer & " " & objDisk.DeviceID & " " & Gigtot & " " & totUsedSpace & " " & GigFree & " " & FormatPercent(pctFreeSpace))
Next
writefile.write vbcrlf + vbcrlf
writefile.close
End Sub