Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


August 2004

Assigning Disk Quotas by Group Membership

Script a quota management tool
RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

One of the advantages of scripting is that it lets you make the OS work in ways that are technically possible but that the graphical tools don't support. For example, take disk quotas. Windows 2000 introduced disk quotas for NTFS volumes. This feature is great, except for one drawback: You assign disk quotas by username, not by group membership. This limitation complicates disk quota management—you need to create and delete quotas for individual users; you can't manage quotas for groups of users. If you have more than a few user accounts, managing them gets unwieldy. However, you can use scripting to get around this limitation.

The groupquota.vbs script I created for this task, which Listing 1 shows, creates disk quota entries for people according to their current group memberships. The tool is an improvement over the GUI but does have some limitations. For example, although the script works by querying current memberships, it can't reflect changes in the group population. If someone leaves or joins a group, the disk quotas in place won't automatically reflect the change. Although groupquota.vbs isn't a complex quota management tool, it does let you more easily manage Windows disk quotas by using only the tools in the box. In this issue, I show you how to perform an action on all members of a group, enable disk quotas for a machine, and apply new quota limits based on group membership.

Warning: If you enable and enforce a disk quota for someone who's already over quota for that disk, you'll prevent that user from writing to that disk at all—including temporary files. Be very careful how you use this script.

Specifying a Group and Enumerating Its Members
The first part of groupquota.vbs can apply to any action you want to perform on all members of a user group or of an Active Directory (AD) organizational unit (OU) or domain, if you choose to organize your groups that way. Many of the scripts I write for this column deal with user or computer accounts, so they wouldn't work well for this situation—we want to work with groups, not individuals. Also, I didn't want to run this script as a logon script; I wanted to set up disk space quotas for members of a group on a file server or other shared computer once, then forget it. Therefore, the script works by connecting to a certain group in a certain domain, then executing a set of statements on each member of that group.

I could have hard-coded the domain name and group name into the script, but that approach is awkward. Instead, I let the person who runs the script supply as arguments the group name and the drive letter for which he or she wants to set up quotas.

To launch the script, you need to open the command-shell window and run the command

groupquota.vbs group x:\

where group is the name of the group to which you want to connect and x is the drive letter that represents the disk to which you want to assign quotas. If your group name includes spaces, you must enclose it in quotation marks.

To retrieve the group name and drive letter, the script uses the Arguments property of the WScript object, which is Windows Script Host's (WSH) root object. The script then assigns those values to the sGroup and sDrive variables, respectively.

The script gets the domain name from the computer on which you're running the script. As the code at callout A in Listing 1 shows, the script creates an instance of WSH's WshNetwork object, then uses that object's UserDomain property to retrieve the domain name. Because you can use the disk quota object only on the local computer, not a remote computer, the script will provide the correct domain name.

Setting Up Disk Quotas
After you sort out the group memberships for the current user, you can apply the code for setting up the disk quotas. To work with any aspect of disk quotas—setting per-user quotas, enumerating quota properties, or even checking to see whether quotas are enabled—you use the DiskQuotaControl object to create a sort of prototype of a disk quota. After you have this generic disk quota, the DiskQuotaControl object's Initialize method lets you specify the disk to which you want to apply quotas and connect to that disk's quota system. (If you plan to set up quotas for more than one disk, you need to initialize the connection to each one individually. Windows manages quotas for each disk separately, and you can maintain a connection to only one drive letter at a time.) For example, if you enter a command-line argument of C:\, the code at callout B would initialize the connection to the C drive. The True value initializes the connection for read/write access.

Next, you need to enable disk quotas—they're disabled by default, which you can see by echoing the value of the DiskQuotaControl object's QuotaState property to the screen. Table 1 lists the possible values for the QuotaState property. To enable quotas and enforce the limits, set the value of QuotaState to 2, as the code at callout C shows.

While you're adjusting the default quota settings, you can also enable event logging for exceeded quotas or warning thresholds. Simply use the DiskQuotaControl object's LogQuotaLimit and LogQuotaThreshold properties, respectively.

Creating the Quota Entry
Creating the quota entry is a piece of cake. After initializing the connection with read/write access, groupquota.vbs uses the DiskQuotaControl object's AddUser method. As the code at callout D shows, this method needs only one argument: the username of the user to whom you're applying the quota. In this case, the sUsername variable contains the username. Then, the script finds the new user quota—yes, even though you just created the quota, you still have to find it—and sets a warning threshold and quota limit.

Extending the Script
Groupquota.vbs is a convenient, automated interface that lets you set up disk quotas for many users at one time, defined only by their group membership. This script is easily extendable. For example, you could extend this script to read the list of groups in the domain, then use VBScript's Select Case statement to create disk quotas for each group. Or, you could inventory system drives and set up quotas on more than one disk at a time. The main point is that you can use scripting to enable Windows to perform tasks—in this case, setting up disk quotas for groups—that aren't possible from the GUI.

End of Article



Reader Comments
Is it possible to list the quotas for users? I tried the following, it didn't work.

-Jeff


C:\scripts>type listquota.vbs
'Force the script to use only defined variables
Option Explicit
Dim sUserName, objGroup, sDomain, sDrive, sGroup, WshNetwork, colDiskQuotas, obj
Member, objUser, objQuota
'Create a quota collection and initialize its connection
'to the selected drive
Set colDiskQuotas = CreateObject("Microsoft.DiskQuota.1")
colDiskQuotas.Initialize "c:\", True
'Connect to the object representing the group
Set objUser = GetObject ("LDAP://acpdom.acp.edu/CN=Benware\, Joel,OU=IS Departme
nt,OU=ACP Staff,OU=Albany College of Pharmacy,DC=acpdom,DC=acp,DC=edu")
'Apply the quota to each member of that group
sUserName = objUser.sAMAccountName
wscript.echo "Account: " & sUserName
set objQuota = colDiskQuotas.FindUser(sUserName)
'wscript.echo "QuotaThreshold: " & objUser.QuotaThreshold
'wscript.echo "QuotalLImit: " & objUser.QuotaLimit
wscript.echo objQuota


C:\scripts>type listquota.vbs
'Force the script to use only defined variables
Option Explicit
Dim sUserName, objGroup, sDomain, sDrive, sGroup, WshNetwork, colDiskQuotas, obj
Member, objUser, objQuota
'Create a quota collection and initialize its connection
'to the selected drive
Set colDiskQuotas = CreateObject("Microsoft.DiskQuota.1")
colDiskQuotas.Initialize "c:\", True
'Connect to the object representing the group
Set objUser = GetObject ("LDAP://acpdom.acp.edu/CN=Benware\, Joel,OU=IS Departme
nt,OU=ACP Staff,OU=Albany College of Pharmacy,DC=acpdom,DC=acp,DC=edu")
'Apply the quota to each member of that group
sUserName = objUser.sAMAccountName
wscript.echo "Account: " & sUserName
set objQuota = colDiskQuotas.FindUser(sUserName)
wscript.echo "QuotaThreshold: " & objUser.QuotaThreshold
wscript.echo "QuotalLImit: " & objUser.QuotaLimit



C:\scripts>cscript //nologo listquota.vbs
Account: benwarej
C:\scripts\listquota.vbs(14, 1) Microsoft VBScript runtime error: Object doesn't
support this property or method: 'QuotaThreshold'


C:\scripts>




acp-it October 06, 2004 (Article Rating: )


Somehow I was able to limit disk space without using quota and can't find where I did it to reverse this, any suggestions?

nfs April 27, 2005 (Article Rating: )


I want to find out how to assign a block of 1GB to a group of users. Not according to the article. Instead I want the 1GB limit = the sum of all disk usage of the users in the group. Therefore if one user hogs all the space, the whole group suffers but my server doesn't. And on the flip side I set it and forget it.
I'm trying to enable a limitation on an FTP site so that ftp users can not abuse the site and fill up a disk. Anyone know of a software that could do this? Am I asking too much? Do I have the wrong idea of how to use a disk quota?

acrazysailor@hotmail.com July 06, 2006 (Article Rating: )


How i assig one block of 2G to a Group of users called Administrations with this Script?

Please let me know, my e-mail address is jean1983@hotmail.com or J.Defrank@GrupoAmbar.com.

Thanks

JeanDefrank December 20, 2006 (Article Rating: )


How i assig one block of 2G to a Group of users called Administrations with this Script?

Please let me know, my e-mail address is jean1983@hotmail.com or J.Defrank@GrupoAmbar.com.

Thanks

JeanDefrank December 20, 2006 (Article Rating: )


what do you think about this?

http://www.admin-pains.com/group_quota.asp

fborozan March 13, 2007 (Article Rating: )


What are modifying exisiting user folder quotas in 2003 R2 based on group membership?

houstonguy78 October 11, 2007 (Article Rating: )


I meant about

houstonguy78 October 11, 2007 (Article Rating: )


What about this Group_A gets 1GB limit and Group_B gets 2GB limit and User_A is in both Group_A and Group_B

w33mhz135 December 27, 2007 (Article Rating: )


Hello

I don't know why but can't get it working.

I tried someyhing like this:

'Force the script to use only defined variables
Option Explicit
Dim sUserName, objGroup, sDomain, sDrive, sGroup, WshNetwork, colDiskQuotas, objMember, objUser
'Print the instructions for the user
If Wscript.Arguments.Count <1 Then Call Help

'Define the variables
sGroup = "10A1"
sDrive = "C"
Set WshNetwork = WScript.CreateObject("WScript.Network")
sDomain = WshNetwork.UserDomain

'Create a quota collection and initialize its connection
'to the selected drive
Set colDiskQuotas = CreateObject("Microsoft.DiskQuota.1")
colDiskQuotas.Initialize sDrive&":\", True
colDiskQuotas.QuotaState = 2

'Connect to the object representing the group
Set objGroup = GetObject _
("WinNT://" & sDomain & "/" & sGroup &", group")
'Apply the quota to each member of that group
For each objMember in objGroup.Members
Wscript.Echo objMember.Name
sUserName = objMember.Name
set objUser = colDiskQuotas.AddUser(sUserName)
set objUser = colDiskQuotas.FindUser(sUserName)
objUser.QuotaThreshold = 9000000
objUser.QuotaLimit = 10000000
Next

Sub Help
Wscript.Echo "Instructions"
Wscript.Echo "This script sets up disk quotas for a group on a named disk"
Wscript.Echo "To run the script, type the name of a group (using quotation marks)"
Wscript.Echo "if its name has more than one word) and the drive letter."
Wscript.Quit
End Sub


What am i doing wrong?

José Cruz
Portugal

joseestrela March 11, 2008 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...

Escape From Yesterworld

Kevin points you to the funniest SQL Server website ever! ...


Task Automation Whitepapers Essential Guide to E-discovery and Recovery for Microsoft Exchange

Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

Related Events Storage Consolidation for Your Microsoft Applications: Reducing Cost and Complexity

Optimize your VMware Infrastructure with the New Releases from the Symantec Backup Exec Family

Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing