Windows scripting secrets for disk quota management

Despite improvements made to disk quota management, Windows Server 2003 does not include any built-in reporting tools. Fortunately, admins can use a simple script to create reports on disk space usage.

By submitting my Email address I confirm that I have read and accepted the Terms of Use and Declaration of Consent.

By submitting your personal information, you agree to receive emails regarding relevant products and special offers from TechTarget and its partners. You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

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.

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.

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:

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:

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:

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:

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

0 comments

E-Mail

Username / Password

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