Announcement

Collapse
No announcement yet.

VB Script to Report Drive size in GB

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VB Script to Report Drive size in GB

    I need to figure out how to have VB report drive size in GB rather than in Bytes.

    This is what I use now,,,

    Const HARD_DISK = 3
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
    For Each objDisk in colDisks
    Wscript.Echo "DeviceID: "& vbTab & objDisk.DeviceID
    Wscript.Echo "Free Disk Space: "& vbTab & objDisk.FreeSpace
    Next


    Thanks in advance

  • #2
    Re: VB Script to Report Drive size in GB

    Code:
    x Bytes *1024 *1024 *1024  =  x GB
    vbs example: Translating size and Rounding the number (using the system's default format for numbers);
    sizeGB = FormatNumber(objDisk.FreeSpace /(1024^3))

    Or, when you'd use the function Round(xxxx, 2) here instead of FormatNumber(xxxx), the number will be showed unformatted.

    Note,
    The Round() and the FormatNumber() functions performs round to even, this is a statistically more accurate rounding algorithm than the common round to larger.


    If you don't like rounding at all, you can translate the value to GB, and rounding the result to a number with three or more places to the right of the decimal (you cannot use the Round() function for this because it will always remove any trailing zero's after the decimal, while the function FormatNumber() even will add zero's if needed). After that cut off the last x digit(s).
    Here is an example:
    Code:
    Const HARD_DISK = 3
    
    strComputer = "."
    
    Set objWMIService = GetObject("winmgmts:" _
       & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colDisks = objWMIService.ExecQuery _
       ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
    
    For Each objDisk in colDisks
       Wscript.Echo "DeviceID: "& vbTab & objDisk.DeviceID
       sizeGB = FormatNumber(objDisk.FreeSpace /(1024^3), 3 )
       sizeGB = left(sizeGB, len(sizeGB) -1 )
       Wscript.Echo "Free Disk Space: "& vbTab & sizeGB & " GB"
    Next
    Last edited by Rems; 10th January 2013, 20:12.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Here is a VB Script that will Email a Report on Drive size in GB

      Re: Here is a VB Script that will Email a Report on Drive size in GB

      Needs to run against a computers.txt file and will check on HD Space Left and give notice if the system is not pingable

      Code:
      'Needs to run against a computers.txt file and will check on HD Space Left and give notice if the system is not pingable
      
      On Error Resume Next
       
      'Constants for drive types
      Const Unknown = 0
      Const Removable = 1
      Const Fixed = 2
      Const Remote = 3
      Const CDROM = 4
      Const RAMDisk = 5
      ' general constants ' NEED TO BE MODIFIED FOR YOUR ENVIRONMENT
      Const MailServer = "192.168.1.25" ' Mail Server to use (SMTP)
      Const MailServerPort = "25" ' SMTP Port used at Mail server (25 is default)
      Const LOCAL_HARD_DISK = 3
      ' Send a mail message
      Sub SendMail(Sender, Recipient, Subject, Message)
      Set objMessage = CreateObject("CDO.Message")
      objMessage.Subject = Subject
      objMessage.From = Sender
      objMessage.To = Recipient
      objMessage.TextBody = Message
      objMessage.Configuration.Fields.Item _
      ("h t tp://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      'Name or IP of Remote SMTP Server
      objMesage.Configuration.Fields.Item _
      ("h t tp://schemas.microsoft.com/cdo/configuration/smtpserver") = MailServer
      'Server port (typically 25)
      objMessage.Configuration.Fields.Item _
      ("h t tp://schemas.microsoft.com/cdo/configuration/smtpserverport") = MailServerPort
      objMessage.Configuration.Fields.Update
      objMessage.Send
      End Sub
      ' get current computer name (from system environment variables)
      Function GetCurrentComputerName
      set oWsh = WScript.CreateObject("WScript.Shell")
      set oWshSysEnv = oWsh.Environment("PROCESS")
      GetCurrentComputerName = oWshSysEnv("COMPUTERNAME")
      End Function
      str = ""
      'Only enumerate physical disks (Not Network Drives)
      Const HARD_DISK = 3
      INPUT_FILE_NAME = "Computers.txt"
      Const FOR_READING = 1
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
      strComputers = objFile.ReadAll
      objFile.Close
      arrComputers = Split(strComputers, vbCrLf)
      For Each strComputer In arrComputers
      vbcrlf
      Set WshShell = WScript.CreateObject("WScript.Shell")
      Set WshExec = WshShell.Exec("ping -n 2 -w 2000 " & strComputer) 'send 1 echo requests, waiting 5 secs each
      strPingResults = LCase(WshExec.StdOut.ReadAll)
      If InStr(strPingResults, "reply from") then
      Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      Set colDisks = objWMIService.ExecQuery _
      ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
      str = str & "System: " & strComputer & vbcrlf
      For Each objDisk in colDisks
      str = str & "Disk: "& objDisk.DeviceID & vbTab
      str = str & " Free Disk Space: "& FormatNumber(CLng(objDisk.FreeSpace /(1024^3)),0,,,-1) & " GB" & vbcrlf
      str = str & vbcrlf
      Next
      Else
      str = str & "System: " & strComputer & vbcrlf
      str = str & "This system appears to be offline. If the reason the system is offline is unknown, please locate, verify the system is turned on, and verify the system is connected to the network. If there are issues with the machine, please submit a trouble ticket through the Help Desk." & vbcrlf
      str = str & vbcrlf
      End If
      Next
      str = str & vbcrlf & vbcrlf & "This email was created by an SCCM Advertisement or a scheduled Task running on System Name and is designed to give ample warning of systems approaching LOW DISK SPACE thresholds and identify systems that are OFFLINE (unpingable)."
      str = str & vbcrlf & ""
      str = str & vbcrlf & vbcrlf & "The SCCM Advertisement was created by Admin Name using Google as a resource."
      str = str & vbcrlf & ""
      str = str & vbcrlf & vbcrlf & "[SCCM Information: Pkg Adv is located @ \\SystemName\c$\Windows\SysWOW64\CCM\Cache\] [SCCM Advertisement: XXXXXXXX] [SCCM Collection: XXXXXXXX] [SCCM Package Name: Hard Drive Intel] [TaskSequence: XXXXXXXX]"
      str = str & vbcrlf & ""
      str = str & vbcrlf & vbcrlf & "Equipment Location: Mr. Someones Name @ (XXX) XXX-XXXX"
      str = str & vbcrlf & vbcrlf & "Equipment Maintenance: Mr. Someones Name @ (XXX) XXX-XXXX"
      str = str & vbcrlf & vbcrlf & "Subject Matter Expert for System Use: Mr. Someones Name @ (XXX) XXX-XXXX"
      str = str & vbcrlf & vbcrlf & "Primary Systems Administration: Mr. Someones Name @ (XXX) XXX-XXXX"
      str = str & vbcrlf & vbcrlf & "SCCM and Backup Systems Administration: Mr. Someones Name @ (XXX) XXX-XXXX"
      str = str & vbcrlf & vbcrlf & Help Desk: @ (800) XXX-XXXX, Option 3"
       
      'Send the email
      SendMail "[email protected]", "[email protected];[email protected];[email protected];[email protected];[email protected]", "System Intel Report", str
      Last edited by labensky; 16th May 2013, 03:15.

      Comment

      Working...
      X