VB Script to Report Drive size in GB

Home Forums Scripting Windows Script Host VB Script to Report Drive size in GB

This topic contains 2 replies, has 3 voices, and was last updated by Avatar deja-vu 6 years, 6 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • Avatar
    jdeguerre
    Member
    #160673

    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 & “rootcimv2”)
    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

    Rems
    Rems
    Moderator
    #228260

    Re: VB Script to Report Drive size in GB

    Code:
    [I][B]x[/B][/I] Bytes *1024 *1024 *1024 [SIZE=”3″]=[/SIZE] [I][B]x[/B][/I] GB[/CODE]
    vbs example: Translating size and Rounding the number (using the system’s default format for numbers);
    [COLOR=”Indigo”]sizeGB = FormatNumber([B]objDisk.FreeSpace[/B] /(1024^3))[/COLOR]

    Or, when you’d use the function [B]Round([I][/B]xxxx[B][/I], 2)[/B] here instead of [B]FormatNumber([I][/B]xxxx[B][/I])[/B], the number will be showed unformatted.

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

    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 [I]Round()[/I] function for this because it will always remove any trailing zero’s after the decimal, while the function [I]FormatNumber()[/I] even will add zero’s if needed). After that cut off the last [I]x[/I] digit(s).
    Here is an example:
    [code]
    Const HARD_DISK = 3

    strComputer = “.”

    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)

    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), [B][SIZE=”3″]3[/SIZE][/B] )
    sizeGB = left(sizeGB, len(sizeGB) [B][SIZE=”3”]-1[/SIZE][/B] )
    Wscript.Echo “Free Disk Space: “& vbTab & sizeGB & ” GB”
    Next

    Avatar
    deja-vu
    Member
    #388005

    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:p

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

    ‘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 & “rootcimv2”)
    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
    [color=red][B]str = str & ” Free Disk Space: “& FormatNumber(CLng(objDisk.FreeSpace /(1024^3)),0,,,-1) & ” GB” & vbcrlf[/B][/color]
    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 @ \SystemNamec$WindowsSysWOW64CCMCache] [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[/CODE][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 & “rootcimv2”)
    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 @ \SystemNamec$WindowsSysWOW64CCMCache] [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[/CODE]

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.