VBScript to check diskspace and threshold

Discussion in 'Web Design, HTML And CSS' started by marconi, May 15, 2008.

  1. marconi

    marconi New Member

    Joined:
    May 7, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I have 3 jobservers and a database server.

    1) I need to first check the disk space.

    2) then check if the free space on each drive is greater than 15% of the total disk space(which is the threshold value) and notify the user.
    3) then shrink the databases.

    I have written the following VBSCript, but it is giving a type mismatch error at Line 25, which is :- iThreshold=(15/100)*(Disksize)
    ------------------------------------------------------------------------------------------
    Code:
    Set iFSO = CreateObject("Scripting.FilesyStemObject") 
    Set oFSO = CreateObject("Scripting.FileSystemObject") 
    InputFile="Diskspace.txt" 
    Outputfile="Diskspacelist.csv" 
    SET ofile = oFSO.CreateTextFile(OutputFile) 
    SET ifile = iFSO.openTextFile(inputfile) 
    Const GBCONVERSION= 1048576000 
    ofile.writeline "Server,Drive,Disk Size,FreeSpace" 
    
    Do until ifile.AtEndOfLine 
       Server = ifile.ReadLine                 
       SET objWMIService = GetObject("winmgmts://" & Server) 
       SET colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk") 
           FOR EACH objLogicalDisk In colLogicalDisk 
               IF objLogicalDisk.drivetype=3 then 
                    'Set variables 
                   'Set Disksize variable for size to be used later 
                    Disksize = FormatNumber(objLogicalDisk.size/GBCONVERSION,2) & "GB" 
                   Freespace = FormatNumber(objLogicalDisk.freespace/GBCONVERSION,2) & "GB" 
                   DeviceID = objLogicalDisk.DeviceID 
                    'Write to file using the variables 
                   ofile.writeline Server & "," & DeviceID & "," & Disksize & "," & Freespace 
               END IF 
                'Disksize was set in last IF statement 
               iThreshold=(15/100)*(Disksize) 
               
               If Int(freespace) < Int(iThreshold) THEN 
               'freespace is less than the threshold so generate an alert 
                   wscript.Echo "Alert!" 
                    'Set variables to help read code better 
                   iThresh = FormatNumber(iThreshold,2,,TRUE) & " GB" 
                   iFreespace = FormatNumber(freespace,2,,TRUE) & " GB" 
                   
                   strDescription = freespace & " is less than the specified threshold of " &_ 
                   iThresh & ". Free space is " & iFreespace 
                   
                   wscript.echo strDescription 
               ELSE 
               'folder size is OK 
                   WScript.Echo "The size of freespace is at least the threshold of " & iThreshold 
               END IF 
           Next              
       SET colLogicalDisk = NOTHING 
       SET objWMIService = NOTHING 
    LOOP 
    
    -------------------------------------------------------------------------------------------

    Can someone help me for rectifying the same.

    Thanks a lot in advance.

    Best Regards,
    KiranKumar K.
     
    Last edited by a moderator: May 16, 2008
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    Set iFSO = CreateObject("Scripting.FilesyStemObject") 
    Set oFSO = CreateObject("Scripting.FileSystemObject") 
    InputFile="Diskspace.txt" 
    Outputfile="Diskspacelist.csv" 
    SET ofile = oFSO.CreateTextFile(OutputFile) 
    SET ifile = iFSO.openTextFile(inputfile) 
    Const GBCONVERSION= 1048576000 
    ofile.writeline "Server,Drive,Disk Size,FreeSpace" 
    
    Do until ifile.AtEndOfLine 
       Server = ifile.ReadLine                 
       SET objWMIService = GetObject("winmgmts://" & Server) 
       SET colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk") 
           FOR EACH objLogicalDisk In colLogicalDisk
    			Dim iThreshold
    			Dim DiskSpaceSize
               IF objLogicalDisk.drivetype=3 then 
                    'Set variables 
                   'Set Disksize variable for size to be used later 
                    Disksize = FormatNumber(objLogicalDisk.size/GBCONVERSION,2) & "GB" 
    				DiskSpaceSize = objLogicalDisk.size
                   Freespace = FormatNumber(objLogicalDisk.freespace/GBCONVERSION,2) & "GB" 
                   DeviceID = objLogicalDisk.DeviceID 
                    'Write to file using the variables 
                   ofile.writeline Server & "," & DeviceID & "," & Disksize & "," & Freespace 
               END IF 
                'Disksize was set in last IF statement 
               iThreshold=(15/100)*(Disksize) 
               
               If Int(freespace) < Int(iThreshold) THEN 
               'freespace is less than the threshold so generate an alert 
                   wscript.Echo "Alert!" 
                    'Set variables to help read code better 
                   iThresh = FormatNumber(iThreshold,2,,TRUE) & " GB" 
                   iFreespace = FormatNumber(freespace,2,,TRUE) & " GB" 
                   
                   strDescription = freespace & " is less than the specified threshold of " &_ 
                   iThresh & ". Free space is " & iFreespace 
                   
                   wscript.echo strDescription 
               ELSE 
               'folder size is OK 
                   WScript.Echo "The size of freespace is at least the threshold of " & iThreshold 
               END IF 
           Next              
       SET colLogicalDisk = NOTHING 
       SET objWMIService = NOTHING 
    Loop
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice