This is a better version of my previous script to enumerate the home directory sizes of all active directory users. The problem with the previous one was that some home directory sizes were not calculated because the Filesystem object had difficulties with long pathnames.
I did a search for a WMI based solution but unfortunately i could not find one.
To shorten the UNC path i decided to create a drive mapping with the Wscript Network object.
It’s a bit of a “funky solution” but it works.
Follow the next steps to run the script (admin rights needed for access to the home directories):
* open your favorite text editor
* copy and paste the script into the editor
* save the script (for example c:\temp\homedirectorysizev2.vbs)
* open a command prompt
* go to “c:\temp”
* give “cscript homedirectorysizev2.vbs” (without quotes) and enter
Notes:
1. when you run the script as administrator a h: network drive is created and disconnected for each user. When you want another drive letter you can change h: in the function to another drive letter.
2. when you cancel the script before it is finished please manually disconnect the “h:” drive mapping
The script:
' Name : homedirectorysizev2.vbs
' Description : script to enumerate the home directories and their sizes of all users in Active Directory v2
' Author : dirk adamsky - deludi bv
' Version : 2.00
' Date : 14-07-2011
' Level : intermediate
arrAttributes = Array("homeDirectory","displayname","mail")
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strBase = "<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">"
Set objRootDSE = Nothing
strFilter = "(&(objectCategory=person)(objectClass=user)(homeDirectory=*))"
strAttributes = Join(arrAttributes,",")
Wscript.Echo Join(arrAttributes,";") & " ; home directory size in MB"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
On Error Resume Next
strTempOutput = ""
For i = 0 To Ubound(arrAttributes)
strTempOutput = strTempOutput & " ; " & adoRecordset.Fields(arrAttributes(i)).Value
strOutput = Mid(Ltrim(strTempOutput),3)
Next
Wscript.Echo strOutput & " ; " & Foldersize (adoRecordset.Fields(arrAttributes(0)).Value) & " MB"
adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing
Function Foldersize(strPath)
On Error Resume Next
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("scripting.filesystemobject")
objNetwork.MapNetworkDrive "h:", strPath
Set objFld = objFSO.GetFolder("h:")
Foldersize = Round(objFld.Size/1048576,2)
objNetwork.RemoveNetworkDrive "h:"
Set objFld = Nothing
Set objFSO = Nothing
End Function
When you have problems/questions please post a reply. Also can alo give a ‘star’ rating.
Happy scripting.
Best regards,
Dirk Adamsky – Deludi BV
