Home > Windows Server Tips > Windows Systems Management and Administration > Windows scripting secrets for disk quota management
Windows Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

WINDOWS SYSTEMS MANAGEMENT AND ADMINISTRATION

Windows scripting secrets for disk quota management


Brien M. Posey, Contributor
02.13.2008
Rating: -4.50- (out of 5)


Expert advice on Windows-based systems and hardware
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



Brien M. Posey

Although the way disk quotas are implemented in Windows Server 2003 is certainly an improvement over what we had in Windows 2000, it still lacks some management capabilities. For example, while Windows makes it easier to set up quotas, there are no built-in reporting tools that allow you to see how disk space is being used. Fortunately, there is a simple script that admins can use to report on a server's disk space usage.

I know that scripting can be a bit intimidating to those who are not used to working with scripts on a regular basis, so I'll try to make this as painless as possible.

Two classes of disk quota management

Want more advice on
Windows scripting?

Check out our monthly Scripting School columns!
Windows Server 2003 contains two different classes that are related to disk quota management: the Win32_QuotaSetting class and the Win32_DiskQuota class. Both classes contain various properties with values related to the quotas that have been imposed on the system.

If you want to create a report of how disk quotas are being used, all you have to do is to create a script that reads the properties. The interesting thing about these two classes is that -- with the exception of the VolumePath property -- they are all readable and writable. This means that with a little work, you can actually create a script that sets or modifies disk quotas.

Creating a disk quota management script

As you can see, the basic idea behind creating a quota management script is simple. All you have to do is create a script that binds to one of the two classes and then read the values of the various properties. If you don't know how to do that, don't worry -- it's easier than it sounds.

Start out by opening Notepad and entering the following text:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDiskQuotas = objWMIService.ExecQuery("Select * from Win32_DiskQuota")
These lines of code take care of binding the Win32_DiskQuota class. To read the properties that are associated with the class, you have to set up a loop that contains a command to echo the contents of the property that you are interested in. You need a loop because, typically, multiple quota entries will exist, and if you don't have a loop, then only the first quota entry will be displayed. Again, this is easier than it sounds. Take a look at the text below. The first and last lines in this block of code create the loop:
For Each objQuota in colDiskQuotas
Wscript.Echo objQuota.User
Next
The middle line in the block of code above echos the value of one of the class properties. In this case, we are echoing the objQuota.User property, which contains the name of the user to whom the quota entry applies. You can modify this command so that it inserts some explanation text in front of the property that is being displayed. For example, if you wanted to insert the word USERNAME: in front of the property, then the command would look something like this:
Wscript.Echo "USERNAME: " & objQuota.User
As you can see, I simply inserted the text that I wanted displayed immediately after the echo command. The text is enclosed in quotation marks and followed by an ampersand.

That's all you have to do to create a basic script. Of course the real trick to creating a useful script is to know what properties are available to you. The properties vary slightly depending on whether you use the Win32_DiskQuota class or the Win32_QuotaSetting class. You can control which class is being used by modifying the end of the last line in the first block of code that I showed you, like so:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDiskQuotas = objWMIService.ExecQuery("Select * from Win32_DiskQuota")
The tables below show the properties that are available to you.

Available properties for Win32_QuotaSetting
Win32_QuotaSetting Properties Description
DefaultLimit The volume's default quota displayed in KB
DefaultWarningLimit The volume's default warning limit displayed in KB
ExceedNotification Displays a true or false indication of whether or not events are written to the event log when a quota is exceeded
VolumePath Displays the name of the volume that the quota applies to. If you are specifying a volume, then you must append two backslashes to the volume name. For example, to specify C:, you would enter C:\\
WarningExceedNotification Displays a true or false indication of whether a user's warning threshold has been exceeded
State Displays a numerical code indicating the level of quota management for the volume. The possible values are as follows:
0: Quota management is not enabled
1: Quotas are enabled, but not enforced
2: Quotas are being enforced

Available properties for Win32_DiskQuota
Win32_DiskQuota Properties Description
DiskSpaceUsed The amount of space consumed by a particular user
Limit The limit imposed on a particular user
QuotaVolume The volume that the quota applies to
User The name of the user that the quota applies to
WarningLimit The user's warning threshold
Status Displays a numerical code indicating whether or not the user's quota has been exceeded. The possible values are:
0: The user is within the limit
1: The warning threshold has been exceeded
2: The quota has been exceeded

Now let's put it all together and create a simple script that reports on a user's disk space usage. Here is the sample script:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDiskQuotas = objWMIService.ExecQuery("Select * from Win32_DiskQuota")

For Each objQuota in colDiskQuotas
Wscript.Echo "User: " & objQuota.User
Wscript.Echo "Disk Volume: " & objQuota.QuotaVolume
Wscript.Echo "Disk Space Used: " & objQuota.DiskSpaceUsed
Wscript.Echo "Status: " & objQuota.Status
Wscript.Echo "Limit: " & objQuota.Limit
Wscript.Echo "Status: " & objQuota.Status
Wscript.Echo "Warning Limit: " & objQuota.WarningLimit
Wscript.Echo
Next

Enter the script's text into notepad and save it as a .VBS file. In case you are wondering, the empty WSCRIPT.ECHO command near the end of the script simply inserts a blank line before the loop occurs. You can run the script by opening a command prompt window and entering the CSCRIPT.EXE command, followed by the filename that you assigned to your script. Depending on how many users you have, the script may initially appear to lock up; eventually, though, it will produce output similar to what you see in Figure A.

Figure A

That is what the script looks like in action. Obviously, this script produces a lot of data. Fortunately, there are a few things you can do to make it more manageable. One option is to use the redirect command to dump the output to a text file. To do so, you would enter the following command when you run the script:

CSCRIPT.EXE script.vbs > output.txt
That command would write the output to a text file so that you can view the output at your leisure. Here's another way to modify the script so that the information is filtered. Take a look at the third line of the script above:
Set colDiskQuotas = objWMIService.ExecQuery("Select * from Win32_DiskQuota")
The Select statement at the end of this command controls which data the script is going to read (and eventually display). Right now the command is set to select *, which tells Windows to read all of the entries. If you want it to read only the data that you are interested in, you can modify the select statement.

To do so, append the WHERE command to the statement, followed by a property name, an operator and a value. For example, if you only want to see users who are consuming more than 500,000 KB of disk space, then you would modify the command as shown below:

Set colDiskQuotas = objWMIService.ExecQuery("Select * from Win32_DiskQuota where DiskSpaceUsed > 500000")
Likewise, you could modify this command so that it only shows you the users who have exceeded their quota. Just make the following modification:
Set colDiskQuotas = objWMIService.ExecQuery("Select * from Win32_DiskQuota where Status = 2")
While this may seem like a lot of information, it really only begins to scratch the surface of what is possible with these types of scripts. If you are interested in learning more about quota-related scripts, check out this link from Microsoft.

Brien M. Posey, MCSE, has received Microsoft's Most Valuable Professional Award four times for his work with Windows Server, IIS and Exchange Server. He has served as CIO for a nationwide chain of hospitals and healthcare facilities, and was once a network administrator for Fort Knox. You can visit his personal Web site at www.brienposey.com.

Rate this Tip
To rate tips, you must be a member of SearchWindowsServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
Windows Management Instrumentation (WMI) Scripting
Windows scripting tutorials for systems administrators
Working with WMI providers to PowerShell
Customizing a mapped drive inventory
Vista's Windows kill switch: What to do if RFM kicks in
Why you should care about Windows PowerShell
Defrag script for all volumes
Christa Anderson's Scripting School for Windows administrators
Microsoft tool exposes WMI namespaces and properties
Microsoft utility helps admins diagnose WMI services
Scripting School: Enhancing scripts that require user input

Windows Data Storage Administration Tools
Top Windows storage improvements for cutting costs in '09
Breaking down the Windows Server Backup tool for Windows 2008
eZines and eBooks for Windows server professionals
Top 10 Windows storage management tips of 2007
Exploring the Windows Server 2003 Resource Kit: Compress.exe and Expand.exe
How to use the g4u network-based hard disk cloning utility
When to use third-party tools in place of NTBACKUP
CDBurnerXP 4.0: A CD/DVD/ISO freeware tool for Windows
Data backup utility mkisofs uses UDF disk-image format
Create batch file to log chkdsk results

Windows File Management
Windows NTFS Tutorial
Windows registry hack improves offline file access for mobile users
How to format NTFS: More tricks to improve file system performance
Optimizing NTFS file system performance
How to receive automatic notification of file changes
Identify file extension types with TrID
Windows System File Checker helps stop system failures
How to reverse NTFS object ownership from administrators to object's creator -- and why
Use PageDefrag to defragment immovable system files
Case Study: Troubleshooting Distributed File System Replication
Windows File Management Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
NTFS  (SearchWindowsServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Server Room Design - Planning, Cooling, Maintenance
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2004 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts