Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Enumerate Users Home Folders

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,51721
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Enumerate Users Home Folders
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Enumerate Users Home Folders - 9/10/2007 5:33:11 AM   
  ThePariah

 

Posts: 15
Score: 0
Joined: 6/14/2005
From: United Kingdom
Status: offline
 
Had a problem at work where one of our file servers almost filled (through years of neglect and bad housekeeping, not my fault i hasten to add). So i had to come with a way of determining users home drive sizes. I discovered during the investigation process that al lot of the directories were redundant and belonged to users that had left the organisation.

In some case they even had multiple home directories as a result of moving sites etc......

I know, I know, crappy processes

Anyway what i did was write this script, which performs an LDAP Query for First Name, Surname, Username, Office location,  account status (enabled/disabled). The script will then check each server in the array to determine the users home folder location. All the results are then written to Excel for scrutiny later.

I never did get around to adding the determining the size of the users home folder. Perhaps someone will have the time to look at  this. If you do please share it with the rest of us.


I hope you find it useful.


' --------------------------------------------------------------------------------------------------------------------
' Author: Darren
' Created: 30 May 2007
' Version 1.0
'
'
' Description: A VBScript that performs an LDAP Query for First Name, Surname, Username, Office location,
'                     account status (enabled/disabled). The script will then check each server in the array to
'                     determine the users home folder location.
'             
'       
'
' Usage:   1. Requires Microsoft Excel to be installed on the PC upon which it is run.
'
' --------------------------------------------------------------------------------------------------------------------
Option Explicit
On Error Resume Next
' ## Set constants and variables
Const ADS_UF_ACCOUNTDISABLE = &H02
Dim objRecordSet, objConnection, objCommand, objRootDSE
Dim intUAC
Dim strExcelPath,objExcel,objSheet, intRow
Dim strDNSDomain, strFilter, strQuery
Dim strHomeFolder, strFolderSize
Dim objFSO
' ## Create FileSystemObject.
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
' ## Bind to Excel object.
Set objExcel = CreateObject("Excel.Application")
' ## Spreadsheet file to be created.
strExcelPath = "c:\HomeDrvResults.xls" ' ## Change for required path
intRow = 3
' ## Create a new workbook.
objExcel.Workbooks.Add
' ## Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
objSheet.Name = "Results"
' ## Format spreadsheet cells layout.
objExcel.Cells(1,1).Value = "User Shortcode:"
objExcel.Cells(1,1).Font.Size = 11
objExcel.Cells(1,2).Value = "Account Status:"
objExcel.Cells(1,2).Font.Size = 11
objExcel.Cells(1,3).Value = "First Name:"
objExcel.Cells(1,3).Font.Size = 11
objExcel.Cells(1,4).Value = "Surname:"
objExcel.Cells(1,4).Font.Size = 11
objExcel.Cells(1,5).Value = "Display Name:"
objExcel.Cells(1,5).Font.Size = 11
objExcel.Cells(1,6).Value = "Office:"
objExcel.Cells(1,6).Font.Size = 11
objExcel.Cells(1,7).Value = "Home Folder:"
objExcel.Cells(1,7).Font.Size = 11
objExcel.Cells(1,8).Value = "Folder Size:"
objExcel.Cells(1,8).Font.Size = 11
' ## Use ADO to search the domain for all users.
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

' ## Determine the DNS domain from the RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strFilter = "(&(objectCategory=person)(objectClass=user))"
strQuery = "<LDAP://" & strDNSDomain & ">;" & strFilter _
& ";sAMAccountName,userAccountControl,givenName,sn,cn,physicalDeliveryOfficeName;subtree"

objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
' ## Enumerate Users
Set objRecordSet = objCommand.Execute
If objRecordSet.EOF Then
Wscript.Echo "No Users Found"
Wscript.Quit
End If

Do Until objRecordset.EOF
   intUAC=objRecordset.Fields("userAccountControl")
   objSheet.Cells(intRow, 1).Value = objRecordSet.Fields("sAMAccountName").Value ' ## Write shortcode/username to excel
   If (intUAC And ADS_UF_ACCOUNTDISABLE) Then
    objSheet.Cells(intRow, 2).Value = "Disabled" ' ## Account is Disabled, write to excel
   Else
       objSheet.Cells(intRow, 2).Value = "Enabled" ' ## Account is Enabled, write to excel
   End If    
   objSheet.Cells(intRow, 3).Value = objRecordSet.Fields("givenName").Value  ' ## Write First name to excel
   objSheet.Cells(intRow, 4).Value = objRecordSet.Fields("sn").Value  ' ## Write Second name to excel
   objSheet.Cells(intRow, 5).Value = objRecordSet.Fields("cn").Value  ' ## Write Display Name/Common Name to excel
   objSheet.Cells(intRow, 6).Value = objRecordSet.Fields("physicalDeliveryOfficeName").Value  ' ## Write Office Location to excel
   strHomeFolder = sLocHomeFolder(objRecordSet.Fields("sAMAccountName").Value)
   objSheet.Cells(intRow, 7).Value = strHomeFolder  ' ## Write Home Directory Location to excel
   objRecordset.MoveNext
intRow = intRow + 1
Loop
' ## Save the spreadsheet and close the workbook.
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
' ## Quit Excel.
objExcel.Application.Quit
' ## Clean Up
Set objSheet = Nothing
Set objExcel = Nothing
Wscript.Echo "Script Complete!!" & vbCrlf &  vbCrlf & "See " & strExcelPath & " for results."
' ## End of Script


Function sLocHomeFolder(strUserName)
Dim Server
Dim arrServers(10)
Dim strLocation, strFolder ' ## path of the folder
Dim bFirst

bFirst = True
strLocation = " "
strFolderSize = " "

arrServers(0) = "SERVER"
arrServers(1) = "SERVER"
arrServers(2) = "SERVER"
arrServers(3) = "SERVER"
arrServers(4) = "SERVER"
arrServers(5) = "SERVER"
arrServers(6) = "SERVER"
arrServers(7) = "SERVER"
arrServers(8) = "SERVER"
arrServers(9) = "SERVER"
arrServers(10) = "SERVER"

For Each Server In arrServers

strFolder = "\\" & Server & "\users\" & strUserName

If(IfExist(strFolder)) Then
  If (bFirst = True) Then  
   strLocation = Server
   bFirst = False
  Else
   strLocation = strLocation & "," & Server
  End If
End If
Next

sLocHomeFolder = strLocation
End Function

' ## Function: Check for existence of file system object.
' ## Argument: Name of Object (file or Directory) to check for.
Function IfExist(strName)
    IfExist = False
    On Error Resume Next
   
    If objFSO.FileExists(strName) Then IfExist = True
    If objFSO.FolderExists(strName) Then IfExist = True
End Function

 
 
Post #: 1
 
 RE: Enumerate Users Home Folders - 9/10/2007 6:05:12 AM   
  DiGiTAL.SkReAM


Posts: 1056
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
To get the size of a folder, d:\dev for example, do this:

      


_____________________________

"There's the one man who learns by reading, the two men that learn by watching, and the rest of us have to pee on the electric fence for ourselves." - Roy Rogers

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

(in reply to ThePariah)
 
 
Post #: 2
 
 RE: Enumerate Users Home Folders - 9/10/2007 1:57:05 PM   
  Meg


Posts: 123
Score: 2
Joined: 7/13/2006
From: Australia
Status: offline
quote:

ORIGINAL: DiGiTAL.SkReAM

To get the size of a folder, d:\dev for example, do this:
Set oFSO = CreateObject("Scripting.FileSystemObject")
WScript.Echo oFSO.GetFolder("d:\dev").Size




Hi DiGiTAL.SkReAM, I have one too but it is amazing how other peoples scripts can be so much simpler, i.e. yours.  

homedrive="\\office\users\mjp"
showfoldersize homedrive

function ShowFolderSize(filespec)
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(filespec)
s = UCase(f.Name) & " uses " & f.size & " bytes."
wscript.echo (s)
End Function

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 3
 
 RE: Enumerate Users Home Folders - 3/13/2008 8:49:49 AM   
  rac8006

 

Posts: 14
Score: 0
Joined: 3/13/2008
Status: offline
I've tried this vbscript on Vista.  The filespec I used was "c:\windows\system32".  When the script is run it gets a permission denied
error on the f.size statement.

It does not matter if you are the administrator or a user with administrator rights or you use run as administrator.  They all get the permission error.
Also c:\users and c:\windows get the same error.

I've been looking everywhere for a solution.

(in reply to Meg)
 
 
Post #: 4
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> Enumerate Users Home Folders Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts