I compiled a vbscript that loops through all the sub folders of a given folder and print out the size. The script takes the folder name and searches the Active Directory for a corresponding user. If found it prints the name of the user in the output.
Use:
cscript homedirsize.vbs
Three input boxes will appear...
- Path where the homedir folders are located. E.g., d:\home
- Width, in characters, of the first output column.
- Width, in characters, of the second output column.
Option explicit
dim path, column1, column2, objRoot, domainname, fso, partline, i, rootFolder, folder
path = inputbox("Enter path of homedirs:")
column1 = Cint(inputbox("Enter width of first output column:"))
column2 = Cint(inputbox("Enter width of second output column:"))
Set objRoot = GETOBJECT("LDAP://RootDSE")
domainname = objRoot.GET("defaultNamingContext")
Set fso = CreateObject("Scripting.FileSystemObject")
wscript.echo "homedirsize.vbs runned on " & Date & " - " & Time
wscript.echo ""
wscript.echo LeftJustified("Foldername", column1) & LeftJustified("Username", column2) & "Size (Mb)"
for i = 0 to column1 + column2 + 8
partline = partline & "-"
next
wscript.echo partline
Set rootFolder = fso.GetFolder(path)
For Each folder in rootFolder.SubFolders
Dim folderSize
folderSize = folder.Size
wscript.echo LeftJustified(folder.Name, column1) & LeftJustified(FindUser(folder.Name, _
domainname), column2) & FormatNumber(((folderSize/1024)/1024),2) & " Mb"
Next
Set fso = Nothing
FUNCTION FindUser(BYVAL UserName, BYVAL Domain)
Dim cn,cmd,rs
SET cn = CREATEOBJECT("ADODB.Connection")
SET cmd = CREATEOBJECT("ADODB.Command")
SET rs = CREATEOBJECT("ADODB.Recordset")
cn.open "Provider=ADsDSOObject;"
cmd.activeconnection=cn
cmd.commandtext="SELECT Name FROM 'LDAP://" & Domain & _
"' WHERE sAMAccountName = '" & UserName & "'"
SET rs = cmd.EXECUTE
IF err<>0 THEN
FindUser = 2
wscript.echo "Error connecting to Active Directory Database:" & err.description
ELSE
IF NOT rs.BOF AND NOT rs.EOF THEN
rs.MoveFirst
FindUser = rs.Fields("Name").Value
ELSE
FindUser = "N/A"
END IF
END IF
cn.close
END FUNCTION
Function LeftJustified(ColumnValue, ColumnWidth)
If(ColumnWidth < Len(ColumnValue) OR ColumnWidth = Len(ColumnValue)) then
LeftJustified = Left(ColumnValue, ColumnWidth - 1) & " "
else
LeftJustified = ColumnValue & Space(ColumnWidth - Len(ColumnValue))
End if
End Function