Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


VBScript to check diskspace; threshold & Database shrinking.

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,59970
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript to check diskspace; threshold & Database shrinking.
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 VBScript to check diskspace; threshold & Database shrin... - 5/7/2008 11:43:27 PM   
  marconi

 

Posts: 8
Score: 0
Joined: 5/7/2008
Status: offline
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 not serving the purpose. Request you to please provide me with the correct inputs/script. -------------------------------------------------------------------------------------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
                                               ofile.writeline Server & "," & objLogicalDisk.DeviceID &_
                                      "," & FormatNumber(objLogicalDisk.size/GBCONVERSION,2) & "GB, " &_
                                      FormatNumber(objLogicalDisk.freespace/GBCONVERSION,2) & "GB, "
                               end if
      Next              
Loop
iThreshold=(15/100)*(size)
iSize=freespaceIf Int(iSize) < Int(iThreshold) Then
'free space is less than the threshold so generate an alert
wscript.Echo "Alert!"
strDescription=objLogicalDisk.freespace & " is less than the specified threshold of " &_
FormatNumber(iThreshold,2,,True) & " GB. Free space is " &_
FormatNumber(iSize,2,,True) & " GB"
wscript.echo strDescription
Else
'folder size is OK
WScript.Echo "The size of " &freespace & " (" & iSize &_
") is at least the threshold of " & iThreshold
End If------------------------------------------------------------------------------------------- Thanks a lot in advance. Marconi. 
 
 
Post #: 1
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 12:06:29 AM   
  ebgreen


Posts: 4408
Score: 29
Joined: 7/12/2005
Status: offline
"...but it is not serving the purpose." How so? Do you get errors? How exactly does it fail?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to marconi)
 
 
Post #: 2
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 12:22:25 AM   
  marconi

 

Posts: 8
Score: 0
Joined: 5/7/2008
Status: offline
Hi,
My implementation of the Logic for checking the threshold is incorrect. I am getting Syntax Errors. Could you please provide your inputs. Regards,Marconi.

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 12:25:47 AM   
  ebgreen


Posts: 4408
Score: 29
Joined: 7/12/2005
Status: offline
What error do you get. What line does it error on? While I could read through this line by line pretending I am a script interpreter, you have a perfectly good interpreter that is currently telling you exactly what the problem is. Please share this information.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to marconi)
 
 
Post #: 4
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 2:19:11 AM   
  marconi

 

Posts: 8
Score: 0
Joined: 5/7/2008
Status: offline
Hi,

I am getting the following output :-  "The size of freespace is at least the threshold of 0", which is junk and incorrect.
Probably, my implementation of the Logic of threshold being 15% of the total disk space, is incorrect.
Could you please provide your most valuable inputs i.e. the corrections in the script.

I actually need to do the following :-
1) 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.

Thanks a lot in advance.

Best Regards,
Marconi.




(in reply to ebgreen)
 
 
Post #: 5
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 2:34:24 AM   
  ebgreen


Posts: 4408
Score: 29
Joined: 7/12/2005
Status: offline
Ok, so read through what you have written and understand what it does. For instance, you loop through all the server names then after that you do the comparison. So at best you would only be doing the comparison for the very last drive of the very last server. If you want to do something for every server, it must be in the loop where you are doing things for every server, not outside the loop. Also, look at this line:

iThreshold=(15/100)*(size)


Where do you ever populate the size variable?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to marconi)
 
 
Post #: 6
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 2:56:07 AM   
  marconi

 

Posts: 8
Score: 0
Joined: 5/7/2008
Status: offline
Hi,

Can you please help me with the code for populating the size variable, as for above logic.

Thanks in advance.

Regards,
Marconi.

(in reply to ebgreen)
 
 
Post #: 7
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 2:59:14 AM   
  ebgreen


Posts: 4408
Score: 29
Joined: 7/12/2005
Status: offline
I will happily help you with pseudo code that you can look at to help with the logic for the real code:

Open a file that lists all the servers to check
For each server
   For each disk on the server
       check the free space to see if it is less than the limit
   Next
Next


Right now you do the checking part completely outside the loop.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to marconi)
 
 
Post #: 8
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 3:30:06 AM   
  marconi

 

Posts: 8
Score: 0
Joined: 5/7/2008
Status: offline
Hi,

Yes thats true, the script should be something like this :-
-----------------------------------------------------------------------------------------------------------------------------------------------
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
                                               ofile.writeline Server & "," & objLogicalDisk.DeviceID &_
                                      "," & FormatNumber(objLogicalDisk.size/GBCONVERSION,2) & "GB, " &_
                                      FormatNumber(objLogicalDisk.freespace/GBCONVERSION,2) & "GB, "
                               end if
                                   iThreshold=(15/100)*(Disksize)
                                     If Int(freespace) < Int(iThreshold) Then
                                     'freespace is less than the threshold so generate an alert
                                     wscript.Echo "Alert!"
                                     strDescription=freespace & " is less than the specified threshold of " &_
                                     FormatNumber(iThreshold,2,,True) & " GB. Free space is " &_
                                     FormatNumber(iSize,2,,True) & " GB"
                                     wscript.echo strDescription
                                     Else
                                     'folder size is OK
                                     WScript.Echo "The size of freespace is at least the threshold of " & iThreshold
                                     End If
                Next              
Loop
------------------------------------------------------------------------------------------------------------------------------------------------------

But my query is that, I am getting Junk values due to improper code for calculating the threshold which is 15% of the actual
size. I am calculating as :-

                            iThreshold=(15/100)*(size)

which is not correct.

Request you to please help me with the proper script for this.

Thanks in advance.
Regards,
Marconi.


(in reply to ebgreen)
 
 
Post #: 9
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 4:58:04 AM   
  ebgreen


Posts: 4408
Score: 29
Joined: 7/12/2005
Status: offline
As I stated before, you never create and populate the size variable.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to marconi)
 
 
Post #: 10
 
 RE: VBScript to check diskspace; threshold & Database s... - 5/8/2008 5:13:47 AM   
  Country73


Posts: 709
Score: 8
Joined: 8/25/2004
From: USA
Status: offline
This code has not been tested, but hopefully I was able to read you code well enough to interpret.

Set iFSO = CreateObject("Scripting.FileSystemObject") 'Delete / do not need

Set oFSO = CreateObject("Scripting.FileSystemObject")
InputFile="Diskspace.txt"
Outputfile="Diskspacelist.csv"

SET ofile = oFSO.CreateTextFile(OutputFile)
SET ifile = oFSO.CreateTextFile(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 to make it easier to read
               'Set Disksize variable for size so you can use 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

(in reply to marconi)
 
 
Post #: 11
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript to check diskspace; threshold & Database shrinking. Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts