Click here to Skip to main content
11,567,926 members (47,466 online)
Click here to Skip to main content

Tagged as

Script: List user's homedir size

, 26 Feb 2013 CPOL 3.2K 1
Rate this:
Please Sign up or sign in to vote.
The script takes the folder name and searches the Active Directory for a corresponding user.

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...

  1. Path where the homedir folders are located. E.g., d:\home
  2. Width, in characters, of the first output column.
  3. Width, in characters, of the second output column.
'****************************************************************************************
'
' Name: Homedirsize.vbs
' 
' Retrieves the size of each subdirectory and matches them to an AD account.
' Outputs a list of directories, username and size.
'
' Written by: Kristofer Källsbo
' 2013-02-26 - http://www.hackviking.com
'
'****************************************************************************************
Option explicit
dim path, column1, column2, objRoot, domainname, fso, partline, i, rootFolder, folder

' get path of homedirs
path = inputbox("Enter path of homedirs:")

' get column widths
column1 = Cint(inputbox("Enter width of first output column:"))
column2 = Cint(inputbox("Enter width of second output column:"))

' get the current domain
Set objRoot = GETOBJECT("LDAP://RootDSE")
domainname = objRoot.GET("defaultNamingContext")

' get the file system object
Set fso = CreateObject("Scripting.FileSystemObject")

' print description lines
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

' start looping all the subfolders
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kristofer Kallsbo
CEO Kallsbo Consulting
Sweden Sweden
I develop in C# on .Net platforms like MVC. Like to use jQuery to build rich interfaces. I also blog about development and snags I got and the solutions I found for them.

I also a full time CIO at a Swedish energy company. When there is time I do some part time consulting on cloud issues.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 26 Feb 2013
Article Copyright 2013 by Kristofer Kallsbo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid