Space in HardDisk

Discussion in 'Scripting' started by MiguelA, Dec 23, 2009.

  1. MiguelA

    MiguelA Guest


    I need a script that monitors the hard drives of servers and when you reach
    a minimum space constraints, I send an email warning.
    How can I do?, Exists already implemented?

    MiguelA, Dec 23, 2009
    1. Advertisements

  2. You could use this script as a basis when working out the amount of free
    disk space:

    aDType = Split("Unknown Removable Fixed Network CDROM RAMDISK")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    LF = Chr(10)

    sLine = "Drive Type State Label F/S Size Free
    WScript.Echo sLine
    WScript.Echo String(Len(sLine)+2, "-")
    For Each oDrive In oFSO.Drives
    D = oDrive.DriveType
    if D > UBound(aDType) then D = 0
    sLine = oDrive.Path & " " & aDType(D)

    If oDrive.IsReady Then
    sLine = Append(sLine, "ready", 18, False)
    If oDrive.DriveType = 3 Then
    sLine = Append(sLine, Left(oDrive.ShareName, 13), 29, False)
    sLine = Append(sLine, Left(oDrive.VolumeName, 13), 29, False)
    End If

    sLine = Append(sLine, oDrive.FileSystem, 44, False)
    sLine = Append(sLine, Int(oDrive.TotalSize/1000000), 51, True)
    sLine = Append(sLine, Int(oDrive.FreeSpace/1000000), 59, True)
    sLine = Append(sLine, Hex(oDrive.SerialNumber), 67, False)
    sLine = Append(sLine, "not ready", 18, False)
    End If
    WScript.Echo sLine

    Function Append(L, S, n, numerical)
    if n < Len(L) + 2 then n = 2 Else n = n - Len(L)
    If numerical Then
    Append = L & Left(Space(60), n + 6 - Len(S)) & S
    Append = L & Left(Space(60), n) & S
    End If
    End Function
    And here is something to send a message:

    const cdoBasic=1
    schema = ""
    Set objEmail = CreateObject("CDO.Message")
    With objEmail
    .From = ""
    .To = ""
    .Subject = "Test Mail"
    .Textbody = "The quick brown fox " & Chr(10) & "jumps over the lazy dog"
    .AddAttachment "d:\Testfile.txt"
    With .Configuration.Fields
    .Item (schema & "sendusing") = 2
    .Item (schema & "smtpserver") = ""
    .Item (schema & "smtpserverport") = 25
    .Item (schema & "smtpauthenticate") = cdoBasic
    .Item (schema & "sendusername") = ""
    .Item (schema & "smtpaccountname") = ""
    .Item (schema & "sendpassword") = "SomePassword"
    End With
    End With
    Pegasus [MVP], Dec 23, 2009
    1. Advertisements

  3. MiguelA

    MiguelA Guest

    I can not implement all the code!

    MiguelA, Dec 31, 2009
  4. The idea is for you to grab the parts that are relevant for you. Feel free
    to ask if certain details are unclear.
    Pegasus [MVP], Dec 31, 2009
  5. MiguelA

    MiguelA Guest

    I do not understand is as if it detects that there is little space to run
    the email sending.
    If a server has multiple disks, it detects all?.
    MiguelA, Dec 31, 2009
  6. When you examine and run the script then you will see several things:
    a) The script will inspect *all* drive letters.
    b) The property oDrive.Path shows the drive letter (e.g. D:)
    c) The property oDrive.AvailableSpace shows the amount of free space.

    You now need to compare oDrive.AvailableSpace against whatever limit you
    wish to set, then invoke the EMail module if your free space drops below the
    set limit.
    Pegasus [MVP], Dec 31, 2009
  7. MiguelA

    MiguelA Guest

    I will try and tell you something

    MiguelA, Dec 31, 2009
  8. MiguelA

    MiguelA Guest

    I have this, but as email command??

    Option Explicit

    Dim strComputer, objWMIService, colMonitoredDisks, objDiskChange
    Dim i, strMessage
    Const LOCAL_HARD_DISK = 3

    strComputer = "West204"
    Set objWMIService = GetObject("winmgmts:" &
    "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" & strComputer
    & "\root\cimv2")
    Set colMonitoredDisks = objWMIService.ExecNotificationQuery ("SELECT * FROM
    __instancemodificationevent WITHIN 30 WHERE " & "TargetInstance ISA
    i = 0
    Do While i = 0
    Set objDiskChange = colMonitoredDisks.NextEvent
    If objDiskChange.TargetInstance.DriveType = LOCAL_HARD_DISK Then
    If objDiskChange.TargetInstance.Size < 100000000 Then
    strMessage = "Hard disk " & objDiskChange.TargetInstance.Name
    & " on computer " & strComputer & " is below 100,000,000 bytes."
    End If
    End If
    MiguelA, Jan 8, 2010
  9. I gave you an EMail function in my reply of two weeks ago. Did you use the
    function? If something is not clear then please say exactly what it is.

    Note also that while WMI is a wonderful thing, it can place a heavy load on
    the CPU. I recommend that you check your CPU loading before finalising this
    Pegasus [MVP], Jan 8, 2010
  10. See inline.

    *** Yes.
    *** Yes, WMI is one method, psexec.exe another.
    *** Correct.
    *** For good performance I would launch psexec.exe from a batch
    *** file on the server, to address each client PC in turn.
    Pegasus [MVP], Jan 12, 2010
  11. Pegasus [MVP], Jan 12, 2010
  12. *** No. I wrote "WMI is one method, psexec.exe another". You should also
    look at the OP's script - he uses WMI.
    Pegasus [MVP], Jan 12, 2010
  13. Based on the link below, you could do something like this:

    sDrive = "D:\"
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oRoot = oFSO.GetFolder(sDrive)

    For Each oFolder In oRoot.SubFolders
    On Error Resume Next
    iSize = oFSO.GetFolder(oFolder.Path).Size
    If Err.number = 0 Then
    WScript.Echo Right(Space(14) & iSize, 14), oFolder.Path
    WScript.Echo "(Inaccessible)", oFolder.Path
    End If
    On Error Goto 0
    Pegasus [MVP], Jan 13, 2010
  14. As I mentioned before in this thread, you can invoke psexec.exe for each
    machine in order to invoke my script locally, e.g. like so:

    @echo off
    for /F %%a in (c:\PCs.lst) do (
    psexec.exe \\%%a -u Administrator -p xxx \\Server\Tools\Script.bat %%a

    The file \\Server\Tools\Script.bat would look like this:
    @echo off
    cscript //nologo c:\tools\Foldersize.vbs > c:\FolderSizes\%1.txt

    c:\PCs.lst is a list of all NetBIOS names that you must compile
    c:\tools\Foldersize.vbs is your VB Script
    c:\FolderSizes is the result folder

    All files and folders are kept centrally on your server for ease of
    Pegasus [MVP], Jan 13, 2010
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.