' SDSpace.vbs
' Server Disk Space checker written by ******** 10/04/09
'
' Usage
' Requires input file containing list of computers to be checked, first connect uses logged on user credentials, if fail
' will try local admin account.
'
' Change Parameters below to suit
'
localdate = Date()
' wscript.echo localdate
' ************************************************************
' Parameters, Change to Suit.
' ************************************************************
InputFile="C:\Capacity Monitoring\Server list.txt"
Outputfile="C:\Capacity Monitoring\Freespacelist_"+cstr(Year(now()))+"_"+cstr(Month(now()))+"_"+cstr(day(now()))+".csv"
' ************************************************************
Const CONVERT_GB = 1073741824
Const HARD_DISK = 3
On Error Resume Next
Set iFSO = CreateObject("Scripting.FilesyStemObject")
Set oFSO = CreateObject("Scripting.FilesyStemObject")
Set ofile = ofso.createTextFile(OutputFile, True)
Set ifile = iFSO.OpenTextFile(inputfile)
' Open Output file and write headings
ofile.writeline "Computer,Drive,Label,Disk Size (GB),Used (GB),FreeSpace (GB),% Available"
' Read Input file and process
Do until ifile.AtEndOfLine
Computer = ifile.ReadLine
Set objWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" & Computer & "\root\cimv2")
If err Then
' Error Encountered using current credentials, try local Admin connect
err = 0
TryLocalAdminConnect
Else
Set colLogicalDisk = objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk WHERE DriveType = " & HARD_DISK & "")
For Each objLogicalDisk In colLogicalDisk
If objLogicalDisk.Size <> "" Then
WriteOutput
End if
Next
End If
Loop
' **************************************************************************************************************************
'Email out resulting csv to the whole world - well at least TS team.
' **************************************************************************************************************************
ofile.Close
ExecuteShellProgram "C:\Program Files\febooti Command line email\febootimail.exe -SERVER Exchange4 -FROM ******** -TO ********* -SUBJECT Server Disk Space Report for "+cstr(Date())+" -MSG See attached for details of server disk space usage as of today -ENTER -ENTER -MSG System list located at WS010898\C:\Capacity Monitoring\Server list.txt -ATTACH "+Outputfile
Function WriteOutput
Free = FormatNumber(objLogicalDisk.FreeSpace/CONVERT_GB,2,,,0)
Total = FormatNumber(objLogicalDisk.Size/CONVERT_GB,2,,,0)
Used = FormatNumber (Total - Free,2,,,0)
Percent = FormatNumber((Free/Total) * 100,1)
VolName = objLogicalDisk.VolumeName
ofile.writeline Computer & "," & objLogicalDisk.DeviceID & "," & VolName & "," & Total & "," & Used & "," & Free & "," & Percent
' wscript.echo Computer & "," & objLogicalDisk.DeviceID & "," & Total & "," & Used & "," & Free & "," & Percent
End Function
Function TryLocalAdminConnect
On Error Resume Next
' Create log file objects
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFso.CreateTextFile("C:\ConnectLog.txt", True)
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
arrayPasswords = Array("*******", "*******", "*********")
objLogFile.WriteLine "Maximum Passwords to try: " & UBound(arrayPasswords) + 2
For i = 0 To UBound(arrayPasswords)
Set objSWbemServices = objSWbemLocator.ConnectServer(sComputer, "root\cimv2", "Administrator", arrayPasswords(i))
objLogFile.WriteLine "Connection Attempt: " & i + 1
If Err Then
' Output error if need be...
objLogFile.WriteLine "Connection to: " & sComputer & " Using Password: " & arrayPasswords(i) & " Failed."
objLogFile.WriteLine "Reason: " & Err.Description
objLogFile.WriteLine "----------------------------------------------------------------------------"
Else
' Do what you need
objLogFile.WriteLine "Connection to: " & sComputer & " Using Password: " & arrayPasswords(i) & " Succeeded."
objLogFile.WriteLine "Reason: N/A"
objLogFile.WriteLine "----------------------------------------------------------------------------"
TryAdminConnect = True ' Success
Exit Function
End If
Next
End Function
Function ExecuteShellProgram(ByVal sFileName)
Dim poShell
Dim poProcess
Dim iStatus
Set poShell = CreateObject("WScript.Shell")
Set poProcess = poShell.Exec(sFileName)
'Check to see if we started the process without error
if ((poProcess.ProcessID=0) and (poProcess.Status=1)) then
Err.Raise vbObjectError,,"Failed executing process"
end if
'Now loop until the process has terminated, and pull out
'any console output
Do
'Get current state of the process
iStatus = poProcess.Status
'Forward console output from launched process
'to ours
WScript.StdOut.Write poProcess.StdOut.ReadAll()
WScript.StdErr.Write poProcess.StdErr.ReadAll()
'Did the process terminate?
if (iStatus <> 0) then
Exit Do
end if
Loop
'Return the exit code
ExecuteShellProgram = poProcess.ExitCode
End Function