To create an instance of the DiskQuotaControl object, you use VBScript's CreateObject function with the programmatic identifier (ProgID) of Microsoft.DiskQuota.1, as callout A in Listing 1 shows. The first operation you perform with the DiskQuotaControl object is to initialize its quota control object. To do so, use the Initialize method to specify and open a disk volume, as the code at callout A shows. The Initialize method requires two arguments. The first argument is a string value that contains the fully qualified path of the volume you want to initialize. Typically, this string is the root of an NTFS volume drive. The second argument is a Boolean value that specifies the volume's read/write modality. If you pass a value of true, the volume will be available for read/write access; if you pass a value of false, the volume will be available only for read access. After the Initialize method executes, you're connected to the volume's disk quota system.
What you do next depends on your needs. For the purposes of this article, let's look at two techniques you can use to obtain and report disk usage for each user. Listing 1 contains code that determines and displays users' quota disk usage, whereas Listing 2 contains code that determines users' quota disk usage and creates an HTML-based report from that data.
Displaying Disk Quota Usage
The DiskQuotaControl object lets you set global volume properties that apply to all users. To set quota-related properties that pertain to a particular user, and to add or remove users to and from the disk quota system, you use the DIDiskQuotaUser object. This object exposes all users who have a disk quota as elements of a collection. Each member of the collection is a DIDiskQuotaUser object. As with any other COM-based collection, you can use VBScript's For Each...Next statement to enumerate the quota users collection. The code at callout B in Listing 1 iterates each user in the collection and creates a string that contains the name of the user and the amount of space he or she is using. The DIDiskQuotaUser object's LogonName property returns the user's logon account name and is a read-only property. The QuotaUsed property contains the user's current disk usage, expressed in bytes. (The data type of the value returned by this property is Double.) As I mentioned earlier, disk compression doesn't affect the disk quota system. Therefore, the value that QuotaUsed returns always reflects the amount of disk space that uncompressed files require.
If you access quota information for reporting purposes, you probably don't need to know the exact number of bytes that each user is consuming. Thus, the value that QuotaUsed returns goes far beyond your needs and, worse, is hard to read. To arrive at a more readable number, the code at callout B uses VBScript's Round function to convert the number of bytes into the number of megabytes. The Round function uses a specified number of decimal digits (two digits in this sample) to round the number up or down.
The code in Listing 1 is configured to work on an NTFS-formatted C volume. To adapt the code to your system, change the first argument of the Initialize method in the code at callout A so that it reflects the volume you want to work on.
Preparing an HTML Report
Listing 1 provides information about users' disk usage, but you can hardly say that the output is well formatted or pleasant to read. With a little effort, you can write code so that the disk usage information flows seamlessly into an HTML framework. HTML isn't a difficult formatting language, as long as your needs remain relatively simple. The tasks of formatting data into columns and applying some straightforward layout rules are relatively easy and don't require the use of an ad hoc report-generator tool.
The script in Listing 2 queries Win2K for disk-usage information, then formats the data into an HTML file. To create the HTML report more effectively and easily, the code uses the QuotaUsedText property instead of the QuotaUsed property. QuotaUsedText returns a string that expresses the amount of space in gigabytes, megabytes, or kilobytes (e.g., 6.78GB, 443.56MB, 4.89KB). The text returned matches the data that appears in the volume's Quota Entries window's Amount Used column. All the information displayed in the Quota Entries window has a counterpart in the DiskQuotaControl and the DIDiskQuotaUser objects.
The script initializes a string with the HTML header tags necessary to set up a page with a table. As the code at callout A in Listing 2 shows, the code creates a table with a seashell background and border of one pixel in size. The code then creates two columns named Logon Name and Amount Used. If you need to create a third column, just insert a <th>...</th> line before the closing </thead> tag. Note that the <th> elements define only a cell in the table's header. A <th> tag alone doesn't represent a column.
At this point, the HTML page's static infrastructure has been set up. The next task is to simply fill the page with the raw quota information, as the code at callout B in Listing 2 shows. In this code, the script adds a new table rowa new <tr> elementfor each user registered in the quota system. The row consists of as many cells<td> elementsas the header cells previously defined. The Amount Used column, which is the second column in the table, is right aligned because it's made of strings that represent numbers. At the end of the loop, you close the main HTML tags and save the page. The file is now ready for deployment over an intranet and for viewing locally or remotely through a Web browser.
Code a Quota
The DiskQuotaControl component represents the scriptable counterpart of the disk quota system. You can use it to selectively extract the information you need from the quota system. This flexibility provides quota information in ways that are specific to corporate standards or administrator preference. Although Win2K provides powerful built-in auditing subsystems, a simple script like that in Listing 2 can use the same tools that the rest of the system uses to read quota information, then persist the information into HTML or even XML or a comma-separated value (CSV) file. You can now build your own history of user disk usage and create easily readable files.