Bharani vel
Bharani vel

Reputation: 21

How to Calculate Percentages of Free Space

I got a script which obtains disk space usage of servers. How to get the output in a table with free space percentage?

Below is the code:

strComputer = "Computer Name"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root'\cimv2")

Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk where drivetype=" & HARD_DISK)

str = str & "SERVER 1 - " & strComputer & vbcrlf
str = str & vbcrlf

For Each objDiskC in colDisks
str = str & objDiskC.DeviceID & " " & FormatNumber(objDiskC.Size/1073741824,2) & " GB" & vbcrlf & vbtab & vbtab
str = str & objDiskC.DeviceID & " " & FormatNumber(objDiskC.FreeSpace/1073741824,2) & " GB" & vbcrlf
Next

str = str & vbcrlf
str = str & vbcrlf
'====================================================================

'Wscript.Echo str

'Send the email
SendMail "[email protected]", "[email protected]", "*** Free Disk Space Summary ***", str
'

Upvotes: 2

Views: 4570

Answers (2)

Clijsters
Clijsters

Reputation: 4256

I would suggest to use a HTML Table. You could also insert a CSS style snippet to the HTML Mail body which sets the margins / paddings inside the table.

For this small solution I would suggest to use string interpolation for creating table, tr, td and the style element. Then use the well known way to make the percentage value out of the absolute sizes.

Have a look at MDN - Table on how to use it.

Upvotes: 0

user692942
user692942

Reputation: 16681

Sounds like you just want to calculate the percentage of free-space.

The calculation for this is simply;

(objDiskC.FreeSpace / objDiskC.Size) * 100

Here have added an extra line to the For Next loop to denote the percentage.

For Each objDiskC in colDisks
  str = str & objDiskC.DeviceID & " " & FormatNumber(objDiskC.Size/1073741824, 2) & " GB" & vbCrLf & vbTab & vbTab
  str = str & objDiskC.DeviceID & " " & FormatNumber(objDiskC.FreeSpace/1073741824, 2) & " GB" & vbCrLf
  'Added this line to your For loop.
  str = str & objDiskC.DeviceID & " " & FormatNumber((objDiskC.FreeSpace / objDiskC.Size) * 100, 2) & "% Free" & vbCrLf
Next

Upvotes: 2

Related Questions