VBscript to enumerate the home directories and their sizes of all users in Active Directory V2

Posted July 14th, 2011 in home directories by dirk adamsky

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 also give a ‘star’ rating.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

Active Directory: VBscript to count users with multiple entries in a nested distribution group

Posted February 16th, 2010 in distributiongroups by dirk adamsky

This script is something I wanted to do for a long time
It is a mutation of the enumeratenestedgroupV2 script.
In large organizations the main distributiongroups tend to be complex also.
Often the distributiongroups represent the organization hierarchy.
A user in general only needs his/her department distributiongroup membership.
This script checks If a user has multiple entries in the main distributiongroup, if so an entry is added to the output.
Part of the script is the use of the dictionary object, also known as “associative array” in other scripting languages.

What the script does:

  • create a dictionary object
  • fill a variable with the group distinguished name
  • call the subroutine EnumNestedgroup
  • the subroutine checks whether the member is a group or a user
  • when the member is a user the smtp address is added to the dictionary object with value 1
  • when the smtp address is already in the dictionary 1 is added to the value
  • the last routine echoes the dictionary object keys and values

Follow the next steps to run the script (no admin rights needed):

  • find the distinguished name of the nested group (adsiedit.msc)
  • open your favorite text editor
  • copy and paste the script into the editor
  • change the distinguished name
  • save the script (for example c:tempcountmembershipnestedgroup.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript enumeratenestedgroup.vbs” (without quotes) and enter

The script:

' Name : countmembershipnestedgroup.vbs
' Description : script to count users with multiple entries in a nested distribution group
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 16-02-2010
' Level : advanced

Set objDictionary = CreateObject("Scripting.Dictionary")
strTargetGroupDN = "LDAP://CN=testgroup,OU=groups,DC=test,DC=org"
Call EnumNestedgroup(strTargetGroupDN)

Sub EnumNestedgroup(strGroupDN)
	Set objGroup = GetObject(strGroupDN)
	For Each objMember in objGroup.Members
		If (LCase(objMember.Class) = "group") Then
			Call EnumNestedgroup(objMember.AdsPath)
		Else
			If objDictionary.Exists(objMember.DisplayName) Then
				objDictionary.Item(objMember.DisplayName) = objDictionary.Item(objMember.DisplayName) + 1
			Else
				objDictionary.Add objMember.DisplayName, 1
			End If
		End If
	Next
	Set objGroup = Nothing
End Sub

For Each strKey in objDictionary.Keys
	If objDictionary.Item(strKey) > 1 Then
		Wscript.Echo strKey & " ; " & objDictionary.Item(strKey) & " ; entries in list"
	End If
Next

Set objDictionary = Nothing

When you have problems/questions please post a reply.

Happy scripting.

Dirk Adamsky – Deludi BV

Active Directory: VBscript to count the number of files on homedirectory for groupmembers

Posted January 26th, 2010 in files by dirk adamsky

This script is for reporting purposes.
It enumerates the members of a specified group and calculates the number of files that each user has on his/her homedirectory. It is especially useful when run from the scheduler.

Follow the next steps to run the script (no admin rights needed):

  • open your favorite text editor
  • copy and paste the script into the editor
  • change the distinguished name of the group to your specific situation
  • save the script (for example c:tempgroupfilesonhomedirectory.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript groupfilesonhomedirectory.vbs” (without quotes) and enter

The script:

' Name : groupfilesonhomedirectory.vbs
' Description : script to count the number of files on homedirectory for groupmembers
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 26-01-2010

Set objGroup = GetObject("LDAP://CN=test,OU=test,DC=test,DC=org")
For Each M in objGroup.Members
	If M.homeDirectory <> "" Then
		FileCnt = 0
		ReturnFileCountUsingFSO M.homeDirectory, FileCnt
		WScript.echo M.cn & " ; " & M.homeDirectory & " ; " & FileCnt & " files on home directory"
	End If
Next
Set objGroup = Nothing

Function ReturnFileCountUsingFSO(strPath, FileCnt)
	On Error Resume Next
	Set FSO = CreateObject("scripting.filesystemobject")
	Set fld = FSO.GetFolder(strPath)
	FileCnt = FileCnt + fld.Files.Count
	For Each f In fld.SubFolders
		ReturnFileCountUsingFSO f.path, FileCnt
	Next
	Set f = Nothing
	Set fld = Nothing
	Set FSO = Nothing
End Function