Solved

Disk Space Usage for list of server shares

Posted on 2010-11-11
9
1,535 Views
Last Modified: 2012-06-27
Hello Fellow Experts,

I'm looking for a VB script that will display Disk usage stats from a text file with a list of \\server\shares with the usage column color coded. Red, Yellow or Green based on usage percentage. Red = 90% - 100%, Yellow = 70% - 89%, Green = Below 69%

Here's the layout that I would like.

Server share, Usage (how much space is used in percentage), Total Drive Space, Used Drive Space,  Free Space.

I would like this to export to csv or txt file.  

Thanks in advance!!!!
0
Comment
Question by:keonh
  • 4
  • 3
  • 2
9 Comments
 
LVL 52

Expert Comment

by:Bill Prew
ID: 34114155
Hmmm, there isn't anyway to include field color information in a CSV file.  What were you thinking in that regard?

~bp
0
 
LVL 77

Expert Comment

by:arnold
ID: 34114208
Use wmi to access the remote servers to get the storage information the presentation is completely up to you.
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/2003_Server/Q_22082577.html

http://www.vistax64.com/powershell/57617-powershell-wmi-scripting-disk-usage.html

http://blogs.technet.com/b/heyscriptingguy/archive/2009/03/04/how-do-i-migrate-my-vbscript-wmi-queries-to-windows-powershell.aspx

isolating to the shares, you may have to get a list of the shares to see on which drives they are and then compare the remaining space on those.

The other option is to define. The shares within the file server manager with quotas and then use the quota settings to generate event log events for warning.
0
 

Author Comment

by:keonh
ID: 34114900
@Billprew - I was thinking to export the csv/txt into excel to get the colors for that usage column. See image.

@Arnold - Thanks ...  I'll look at those links.
diskspacestats.jpg
0
 
LVL 77

Expert Comment

by:arnold
ID: 34115446
Note the share is actually in most cases represents the available space on the drive on which it resides.  With the newer windows 2003 R2 and higher, there is a way to set a per share quota in terms of the total size of the space allocated to the share and then a per user quota as well.

The other option using WMI you might be able to query a local system that has the shares mapped.
The distinction deals with the "drive type."
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Expert Comment

by:Bill Prew
ID: 34117501
I'll take a shot at this.  Here's a VBS script that should be close.  Create a list of the shares to report on in a text file.  Edit the constants near the top of the script as needed.

~bp
' Define constants
Const cSharesFile = "c:\temp\EE26608584\shares.txt"
Const cExcelFile = "c:\temp\EE26608584\usage.xlsx"
Const cHeaders = "Server Share;Usage;Total Space;Used Space;Free Space"
Const cExcel7 = 51
 
' Read list of shares into arrary
Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objSharesFile = objFSO.OpenTextFile(cSharesFile, 1)
arrPaths = Split(objSharesFile.ReadAll, vbNewLine)
objSharesFile.Close
 
' Start Excel, create a new worksheet
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.Workbooks.Add
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
 
' Initialize row and column indexes
intCol = 1
intRow = 1
 
' Write header row
For Each strHeader In Split(cHeaders, ";")
   objSheet.Cells(intRow, intCol).Value = strHeader
   intCol = intCol + 1
Next
 
' Process each share
For Each strPath in arrPaths
   If strPath <> "" Then
      intRow = intRow + 1
      arrTokens = Split(strPath, "\")
      strShare = arrTokens(UBound(arrTokens))
      strServer = arrTokens(UBound(arrTokens) - 1)
      strFull = "" 
      numSize = ""
      numFree = ""
      
      ' Enumerate share
      Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
      Set colShares = objWMI.ExecQuery("Select Path from Win32_Share where name = '" & strShare & "'")
   
      ' Get drive share is on
      For Each objShare In colShares
         strFull = objShare.Path
         strDrive = Split(objShare.Path, ":")(0)
         Exit For
      Next
      
      ' Get space info for drive
      Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
      Set colItems = objWMI.ExecQuery("Select FreeSpace,Size from Win32_LogicalDisk where Name='" & strDrive & ":'")
      For Each objItem in colItems
         numSize = (objItem.Size / 1073741824)
         numFree = (objItem.FreeSpace / 1073741824)
         numUsed = numSize - numFree
         numPercent = numUsed / numSize
         Exit For
      Next
      
      ' Store data in Excel
      objSheet.Cells(intRow, 1).Value = strPath
      objSheet.Cells(intRow, 2).Value = numPercent
      objSheet.Cells(intRow, 3).Value = numSize
      objSheet.Cells(intRow, 4).Value = numUsed
      objSheet.Cells(intRow, 5).Value = numFree
   End If
Next
 
' Save excel file, close excel
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs cExcelFile, cExcel7
objExcel.ActiveWorkbook.Close False
objExcel.Application.Quit
 
' Wrap up
Set objSheet = Nothing
Set objExcel = Nothing

Open in new window

0
 

Author Comment

by:keonh
ID: 34123486
@Bilprew - Nice work!! Thanks!! Just a few mods...

a.  Can you format the "Usage" to %
b.  Can you format Total, Used & Free to GB or MB depending on size.
c.  Modify the output filename to include date & time. Ex. 11_12_2010_1:10PM_Usage.xlsx
d.  Add conditional formatting based on Red = 90% to 100% - Yellow = 70% - 89% - Green = 1% to 69%

If you can't get item D, I can do it manually.  Thanks again.

Side note: I'm not a programmer but I was wondering if it's possible to get the same results using an EXE with a customizable refresh rate.  Just a thought.

0
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 34126004
a.  Can you format the "Usage" to %

Yes, add the second line here after the existing comment toward the bottom:

' Save excel file, close excel
objSheet.Columns("B:B").NumberFormat = "0.00%"

b.  Can you format Total, Used & Free to GB or MB depending on size.

The only way to do that would be to treat these fields as character fields, do the scaling you desire in the VBS script, and then put the result in the cells.  But that will mean you can't do ANY math against those values, since they will be character cells, and also will have different scales.  That feels like a mistake to me, since it would be hard to follow the values when the went from say 999 MB to 1 GB.

c.  Modify the output filename to include date & time. Ex. 11_12_2010_1:10PM_Usage.xlsx

Change this line:

Const cExcelFile = "c:\temp\EE26608584\usage.xlsx"

To:

cExcelFile = "c:\temp\EE26608584\" & Replace(FormatDateTime(Now, 2), "/", "-") & "_" & Replace(FormatDateTime(Now, 4), ":", "-") & "_usage.xlsx"

d.  Add conditional formatting based on Red = 90% to 100% - Yellow = 70% - 89% - Green = 1% to 69%

Don't think I can bite that off.

~bp
0
 

Author Comment

by:keonh
ID: 34173626
Thanks bp ... Genius is an understatement.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 34173644
Thanks for the points, and compliment, glad to have provided some useful info for you.

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now