Link to home
Create AccountLog in
Avatar of japji_it
japji_it

asked on

Script/Batch File to check the System Drive Total Space and Used Space

Hi,

I need a Batchfile which can scan remote Servers and do the following:

1. Calculate the Total space on C Drive
2. Calculate the Free space on C Drive.

The script should spit the results into Excel File.

Thank you.
SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Here is a "Monitoring" VBScript I had been working on but never had a chance to finish
'##############################################
'#
'# Monitors Disk Space and other things for Computers Listed in
'# DiskSpaceList.txt in the same directory as script and Emails When Below threshold.
'#
'##############################################

Const LOCAL_HARD_DISK = 3 ' 3 is all local drives.
Const FOR_READING = 1
Const CONVERSION = 1073741824 ' This is total bytes in 1 GB.
Const CONVERT = 1048576 ' This is total bytes in 1 MB.
On Error Resume Next

strLocal = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("DiskSpaceList.txt", FOR_READING)

Do Until objFile.AtEndOfStream
    strComputer = objFile.ReadLine
   
    Set objWMIService1 = GetObject("winmgmts:\\" & strLocal & "\root\cimv2")
    Set colPings = objWMIService1.ExecQuery _
           ("Select * From Win32_PingStatus where Address = '" & strComputer & "'")
   'WScript.Echo strComputer
    ' Check if server is alive before getting info.
    For Each objStatus in colPings
        If IsNull(objStatus.StatusCode) or objStatus.StatusCode <> 0 Then
       
        Else   
            Set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
               
            Set colDisks = objWMIService.ExecQuery _
                ("SELECT * FROM Win32_LogicalDisk WHERE DriveType = " & LOCAL_HARD_DISK & "")
               
                For Each objDisk in colDisks
			    intFreeSpace = objDisk.FreeSpace / CONVERSION
    			intTotalSpace = objDisk.Size / CONVERSION
    			pctFreeSpace = intFreeSpace / intTotalSpace
    			'Wscript.Echo pctFreeSpace & " on " & strComputer
               
               
                If pctFreeSpace < .15 Then
					strSubject = UCase(strComputer) & " " & objDisk.DeviceID & " Free space low"
                    strBody = Now & vbCrLf & UCase(strComputer) _
                        & " available disk space is below the 15% threshold on " & objDisk.DeviceID & " with " & FormatPercent(pctFreeSpace) & " available!" & vbCrLf _
                        & vbCrLf & "Total Space: " & FormatNumber(intTotalSpace, 2, False, False, True) & "GB" & vbCrLf _
                        & "Free Space: " & FormatNumber(intFreeSpace, 2, False, False, True) & "GB"
                    call MAILER(strComputer, strSubject, strBody)
                End If
            Next
                
    'Gets PROCESSOR Usage

set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
Set objProcessor = objRefresher.AddEnum _
    (objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
intThresholdViolations = 0
objRefresher.Refresh

    For Each intProcessorUse in objProcessor
        If intProcessorUse.PercentProcessorTime > 90 Then
            intThresholdViolations = intThresholdViolations + 1
                If intThresholdViolations = 1 Then
                    intThresholdViolations = 0
					strSubject = UCase(strComputer) & " CPU usage high"
                    strBody = Now & vbCrLf & UCase(strComputer) & "Processor usage threshold exceeded. " & intProcessorUse.PercentProcessorTime & "% Usage"
                    call MAILER(strComputer, strSubject, strBody)
                End If
        Else
            intThresholdViolations = 0
        End If

Next
 
    'Gets MEMORY Usage
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colSettings = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colSettings 
   
                  intFreeMem = objOperatingSystem.FreePhysicalMemory / CONVERT
                  intTotalMem = objOperatingSystem.TotalVisibleMemorySize / CONVERT
                  pctMem = intFreeMem / intTotalMem

                  If pctMem < .10 Then
					strSubject = UCase(strComputer) & " Memory usage high"
                    strBody = Now & vbCrLf & UCase(strComputer) _
                        & " Memory usage is above the 90% threshold with " & FormatPercent(pctMem, 2, False, False, True) & vbCrLf & vbCrLf _
                         & "Total Memory: " & FormatNumber(intTotalMem, 2, False, False, True) & "Gb" & vbCrLf _
                         & "Free Memory: " & FormatNumber(intFreeMem, 2, False, False, True) & "Gb"
                    call MAILER(strComputer, strSubject, strBody)
                End If
Next

        End If
    Next
    
Loop

objFile.Close

'#####################
'# Sub To Send Email #
'#####################
Sub MAILER(Computer, Subject, Body)
   ' strLength = Len(Wscript.ScriptName) - 4
    'strFrom = Left(Wscript.ScriptName, strLength)
   
    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom & "Administrator@domain.com"
    objEmail.To = "you@domain.com"
    objEmail.Subject = Subject
    objEmail.TextBody = Body
   
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.domain.com"
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objEmail.Configuration.Fields.Update
   
    objEmail.Send
End sub 

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account