Enforce disk quotas for user profiles

Return to the article

Source Code for check_disk_quotas.wsf:


<job>
<script language="JScript">
/*  =======================================
    ENFORCE DISK QUOTAS FOR USER PROFILES
    check_disk_quotas.wsf
    Jonathan Snook
    http://www.snook.ca/jonathan/
    January 10, 2005

    Feel free to use this code as you wish.
    =======================================
*/

// ====================================
// set application parameters
var maxfsize = 500000 * 1024;        // stored in bytes (* 1024 to set in kilobytes)
var fpath = "e:\\users\\";           // escaped path to user folder
var smtpserver = "smtpserver";       // smtp server to send support e-mail to
var domainname = "domainname";       // domain name
var debug = false;                   // display a dialog for debug mode
var revoke = false;                  // revoke write access if maxfsize exceeded?
var supportemail = "support@example.com";

// ====================================
// do the checking
var fso, f, fc, s;
var isexceeded = false;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFolder(fpath);

fc = new Enumerator(f.SubFolders);
s = "";
for (; !fc.atEnd(); fc.moveNext())
{
    if (fc.item().size > maxfsize)
    {
        // this user has exceeded their limit
        s += fc.item().name + ' is currently at ' + Math.round( fc.item().size/1024 ) + " KB \n";
        isexceeded = true;
        if(revoke)
        {// revoke access
            wsh = new ActiveXObject("WScript.Shell");
            wsh.run('cacls '+ fpath + fc.item().name+' /T /E /P '+domainname+'\\'+fc.item().name+':r',  7, false);
        }

    }
}

if(isexceeded){
    // Send using the Pickup directory on the IIS server
    var Flds, strHTML
    var cdoSendUsingPickup = 1;
    var cdoSendUsingPort = 2;

    var iMsg = new ActiveXObject("CDO.Message");
    var iConf = new ActiveXObject("CDO.Configuration");

    // set the CDOSYS configuration fields to use the SMTP service pickup directory
    Flds = iConf.Fields;
    Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort;
    Flds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver;
    Flds.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10;
    Flds.Update();

    // build text for message body
    strHTML = "Exceeded User Quotas:\n";
    strHTML += s;

    // apply settings to the message
    iMsg.Configuration = iConf;
    iMsg.To = supportemail;
    iMsg.From = supportemail;
    iMsg.Subject = "Exceeded User Quotas";
    iMsg.TextBody = strHTML;
    iMsg.Send();

}

// ====================================
// spit out exceeded values
if(debug) WScript.Echo(s);

</script>
</job>