Disk space
This small piece of script will analyse all computer in a domain or an OU and email this back to me. The output is in .csv format.
The script contains handy pieces as:
- Open a logfile and write to it
- Enumerate all computers
- Get all drive properties
- Email a file with attachment
The emailing part requires CDO which is normally installed with Outlook.
The script accepts one command which specifies the root OU to start with.
For example you could write this to launch it to analyse all computer in the OU luxembourg. If you don’t specify the OU then it runs for the whole domain.
cscript.exe diskspace.vbs luxembourg
Diskspace.vbs
' Dump all drives and their properties to a logfile
rootDomain = "DC=mydomain,DC=prv"
context = "Domain"
'If specified use specified ou
If WScript.Arguments.Count = 1 Then
rootOU = "ou=" & WScript.Arguments(0) & ","
context = WScript.Arguments(0)
End if
'Open logfile for writing
Const ForAppending = 2
LogFile = "Disk space analasys_" & context & ".csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
(LogFile, ForAppending, True)
'Write header line
objTextFile.WriteLine ("Computer name,Compressed,Description," & _
"DeviceID,FileSystem,Size Gb,FreeSpace Gb,Used space %," & _
"Supports File Based,Compression,VolumeName")
'Enumerate computer accounts and execute
EnumComputers()
'Close logfile
objTextFile.Close
'Open the resulting file in excel
'Set fso = CreateObject("Scripting.FileSystemObject")
'Set objExcel = CreateObject("Excel.Application")
'objExcel.Visible = True
'Set objWorkbook = objExcel.Workbooks.Open( fso.GetFolder(".") & _
"\" & LogFile)
Set objEmail = CreateObject("CDO.Message")
With objEmail
.From = "job_LuxChris@mydomain.com"
.To = "luxembourgitnotifications@mydomain.com"
.Subject = "Weekly disk space report"
'.TextBody = ""
.AddAttachment "c:\jobs\Disk space analasys_luxembourg.csv"
.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "luxmail"
.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Configuration.Fields.Update
.Send
End with
WScript.Quit
'-----------------------------------------
'Subs below here
'-----------------------------------------
Sub GetDriveProperties (strComputer)
On Error Resume next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & _
"\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For each objDisk in colDisks
If objDisk.Description = "Local Fixed Disk" Then
objTextFile.WriteLine ( strComputer & "," &_
objDisk.Compressed & "," & objDisk.Description & "," & _
objDisk.DeviceID & "," & objDisk.FileSystem & "," & _
Int(objDisk.Size / 1073741824 * 10) / 10 & "," & _
Int(objDisk.FreeSpace / 1073741824 * 10) / 10 & "," & _
Int((1 - (objDisk.FreeSpace / objDisk.Size)) * 100) & _
"," & objDisk.SupportsFileBasedCompression & "," & _
objDisk.VolumeName _
)
End if
Next
End Sub
'-----------------------------------------
Sub EnumComputers()
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://" & rootOU & rootDomain & "' " _
& "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
WScript.Echo "Retrieving from " & objRecordSet.Fields("Name").Value
GetDriveProperties (objRecordSet.Fields("Name").Value)
objRecordSet.MoveNext
Loop
End sub
I use this script as a scheduled task on my workstation and it runs every week with the following line:
C:\\WINDOWS\\system32\\cscript.exe c:\\jobs\\diskspace.vbs luxembourg
