Tip

Windows scripting secrets for disk quota management

Although the way disk quotas are implemented in Windows Server 2003 is certainly an improvement over what we had in Windows 2000

Requires Free Membership to View

, 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.

ABOUT THE AUTHOR
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.

This was first published in February 2008

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

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.