RunMod. The RunMod subroutine, which is associated with the Run Modify User Quotas button, contains the code for modifying user quotas. Aside from clearing the Status field value (using the statement statval.value = ""), the code checks for the presence of a carriage return or line feed at the end of the User ID input field. A carriage return or line feed is necessary to acquire the final ID, so if one isn't present, the code adds one. RunMod uses the Split function with the vbcrlf delimiter to build an array of user IDs, which the HTA later uses to set the quotas.
The script then assigns the values from the HTA input screen to variables, which are used throughout the script. The script uses GetObject to retrieve an instance of a class. The script uses the Win32_DiskQuota class for both the RunMod and RunQuery subroutines and uses the Win32_ComputerSystem class to retrieve the local computer name when the user leaves the domain input field blank.
The meat and potatoes of the RunMod subroutine takes place at callout A in Listing 1. Here, the subroutine cycles through the user IDs. For each ID, the subroutine retrieves a specific user quota, sets the limit, and commits it by using the objQuota.Put_ method.
RunQuery. The RunQuery subroutine, which is associated with the Run Query Quota button, starts out by setting variables for input values. RunQuery then sets the wbemFlagForwardOnly and wbemFlagReturnImmediately constants. The wbemFlagForwardOnly constant causes the script to return a forward-only enumerator; such enumerators are generally much faster and use less memory than conventional enumerators. The wbemFlagReturnImmediately constant causes the call to return immediately.
Next, the RunQuery subroutine makes a Windows Management Instrumentation (WMI) connection to the server or computer that the user specified; if that field was left blank, the script connects to the local computer. The subroutine then uses a WMI Query Language (WQL) query to grab the existing quota data, as the code at callout B shows. In this query, the script specifies both the wbemFlagForwardOnly and wbemFlagReturnImmediately constants. (If you want to shorten a line of code, you can combine the constants' values and specify a decimal value of 48. An example of this shortened version appears in the code at callout A.)
In the WQL query, the WQL parameter is optional. The code at callout B uses it, but if you choose not to include it, you'll need to have two commas with nothing between them, as the Set colItems statement in callout A shows. I recommend that you specify WQL because its presence clarifies that this is a Select statement for WMI use.
After RunQuery grabs the existing quota data, it displays any returned data in a Microsoft Excel spreadsheet. Note that because no count property is available for the Win32_DiskQuota class, the subroutine puts the Excel initialization inside the For Next loop and sets up a do_once flag. If a count property were available, the subroutine could simply check for a value greater than 0, then avoid the Excel initialization and For Next loop if there were no return results from the query.
Completing the code. The HTA's VBScript section also contains a function called ConvStatus, which callout C shows. This function converts the quota status from the numerals of 0, 1, or 2 to the values of OK, Warning, or Exceeded, respectively. Note that the code checks for errors at key spots throughout the VBScript section. The code uses err.description to capture system-generated errors, then stores errors in the status variable and later displays them in the HTA's Status Box. After the VBScript section, the HTA includes code for the input window.
Create Your Own HTAs
You can view and edit the HTA filefor example, to change the quota limitby using Notepad. If you plan to develop your own HTA interfaces and you're a pro at writing HTML, you're all set; for the most part, you can create your input screen in Notepad and simply rename it with an .hta extension. However, if you aren't familiar with HTML, I suggest you use a Web development application such as Microsoft Office FrontPage 2003. Such applications make learning HTML a lot easier, and you can have a working application up and running in a fraction of the time it might otherwise take.