Skip to content

Josh's IT-Blog

Information Technology, and other interesting things …

  • Home
  • About
  • Contact
  • Links
  • Home
  • About
  • Contact
  • Links

Remove all disabled users from distribution lists

  1. Home   »  
  2. Remove all disabled users from distribution lists

Remove all disabled users from distribution lists

23. June 201114. July 2011 Burkard JoshActive Directory, Windows 2008 R2Tagged Active Directory, VBScript

Cause of company policy we don’t delete users which are leaving, but we disabled them. The exchange mailbox will be removed after some months. For this incomming mails have to be forwarded to an exchange contact with an unresolvable address, so the sender receives an error message.

Cause of this, we need to remove the disabled users from all distribution list. If not, senders receive error messages each time a message was send to a distribution list with disabled users.

To automate this, i wrote a script. You can filter it by OU and run it first in a display-only mode before you remove the disabled users definitely from all distribution lists.

You can download the script RemoveDisabledUsers.vbs or copy/paste it from here:

' ===========================================================================================
'
'   Script Information
'
'   Title:              RemoveDisabledUsers.vbs
'   Author:             Josh Burkard
'   Date:               23.06.2011
'   Description:        - displays or removes disabled users from all distribution lists
'                       - you can filter the output / removal by User-OU
'                       - to remove users from AD, you need to start the script with an
'                         administrator account
'
'   Startup:            only display disabled users in distribution lists:
'                       --------------------------------------------------
'                            cscript RemoveDisabledUsers.vbs
'
'                       remove disabled users from distribution lists:
'                       ----------------------------------------------
'                            cscript RemoveDisabledUsers.vbs remove
'
' ===========================================================================================

' If enabled users are filtered by OU's:
booFilterOUs = true
' User-OU's (this array will be ignored if booFilterOUs is false):
strOUs = Array (	"OU=OU1,DC=domain,DC=local", _
					"OU=OU2,DC=domain,DC=local", _
					"OU=OU3,DC=domain,DC=local")

if wscript.arguments.length = 0 then
	wscript.echo "Display Mode"
	wscript.echo "To remove this users from distribution lists, start the script with parameter 'remove'."
else
	if lcase(wscript.arguments(0)) = "remove" then
		mode = "remove"
		wscript.echo "Remove Mode"
	else
		wscript.echo "Display Mode"
		wscript.echo "To remove this users from distribution lists, start the script with parameter 'remove'."
	end if
end if
wscript.echo
set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
set conn1 = createobject("ADODB.Connection")
strConnString = "Data Provider=NONE; Provider=MSDataShape"
conn1.Open strConnString
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strNameingContext = iAdRootDSE.Get("configurationNamingContext")
strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")
set objParentRS = createobject("adodb.recordset")
set objChildRS = createobject("adodb.recordset")
strSQL = "SHAPE APPEND" & _
			"  NEW adVarChar(255) AS GRPDisplayName, " & _
			"  NEW adVarChar(255) AS GRPDN, " & _
			" ((SHAPE APPEND  " & _
			"      NEW adVarChar(255) AS USDisplayName, " & _
			"      NEW adVarChar(255) AS USDN, " & _
			"      NEW adVarChar(255) AS USGRPDisplayName, " & _
			"      NEW adVarChar(255) AS USGRPDN " & _
			")" & _
			"      RELATE GRPDN TO USGRPDN) AS rsGRPUS "
objParentRS.LockType = 3
objParentRS.Open strSQL, conn1
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"

' Read distribution lists from AD
GALQueryFilter =  "(&(mailnickname=*)(|(objectCategory=group)))"
strQuery = "<LDAP://"  & strDefaultNamingContext & ">;" & GALQueryFilter & ";distinguishedName,displayname,legacyExchangeDN,homemdb;subtree"
Com.ActiveConnection = Conn
Com.CommandText = strQuery
Set Rs = Com.Execute
while not rs.eof
	objParentRS.addnew
	objParentRS("GRPDisplayName") = rs.fields("displayname")
	objParentRS("GRPDN") = rs.fields("distinguishedName")
	objParentRS.update
	rs.movenext
wend

' Read users from AD
GALQueryFilter = "(&(&(mailnickname=*)(objectCategory=person)(userAccountControl:1.2.840.113556.1.4.803:=2)))"
strQuery = "<LDAP://"  & strDefaultNamingContext & ">;" & GALQueryFilter & ";distinguishedName,displayname,legacyExchangeDN,homemdb;subtree"
Com.ActiveConnection = Conn
Com.CommandText = strQuery
Set Rs1 = Com.Execute
Set objChildRS = objParentRS("rsGRPUS").Value
while not rs1.eof
	if instr(rs1.fields("displayname"),"SystemMailbox{") = 0 then
		set objuser = getobject("LDAP://" & replace(rs1.fields("distinguishedName"),"/","\/"))

		' Check if user is in one of the defined OU's
		If booFilterOUs = true then
			booOU = false
			For Each strOU In strOUs
				If instr(lcase(objuser.distinguishedName), lcase(strOU)) Then
					booOU = true
				End If
			Next
		end if

		If (booOU = true) or (booFilterOUs = false) then
			For each objgroup in objuser.groups
				objChildRS.addnew
				objChildRS("USDisplayName") = rs1.fields("displayname")
				objChildRS("USDN") = rs1.fields("distinguishedName")
				objChildRS("USGRPDisplayName") = objgroup.name
				objChildRS("USGRPDN") = objgroup.distinguishedName
				objChildRS.update
			Next
		End If
	end if
	rs1.movenext
wend

' Output of Groups and Users
objParentRS.MoveFirst
wscript.echo "GroupName,Disabled User's Name"
wscript.echo
Do While Not objParentRS.EOF
	Set objChildRS = objParentRS("rsGRPUS").Value
    if objChildRS.recordCount <> 0 then
		Do While Not objChildRS.EOF
			Wscript.echo objParentRS.fields("GRPDisplayName") & ", " & objChildRS.fields("USDisplayName")

			if mode = "remove" then
				' Removing users from groups
				set objgroup = getobject("LDAP://" & replace(objChildRS.fields("USGRPDN"),"/","\/"))
				Set objUser = getobject("LDAP://" & replace(objChildRS.fields("USDN"),"/","\/"))
				objGroup.Remove(objUser.AdsPath)
				objgroup.setinfo
				wscript.echo "User-Removed"
			end if
			objChildRS.MoveNext
		loop
	end if
	objParentRS.MoveNext
Loop

Post navigation

Previous: Add third-party SSL-Certificate to Cisco WLC’s web authentication page
Next: Notify AD user with mail when password expires

About

Author Image
My name is Josh Burkard.
I'm a DevOps Engineer working with one of Europees largest payroll provider. in my work I have a lot to do with Microsoft server operating systems, System Center, VMware, Microsoft Azure Cloud and other software.
On this site I will write some posts about different technology problems and their solutions.
please note also my tweets and retweets from this area.

Categories

  • General (13)
  • Hardware (9)
    • Network (8)
      • Cisco (2)
    • Storage (2)
  • Hiking (1)
  • Home Assistant (5)
  • Microsoft Azure (1)
    • Automation (1)
  • PowerShell (3)
  • Software (1)
    • Excel (1)
  • System Center (18)
    • SCCM (3)
    • SCDPM (1)
    • SCOM (12)
    • SCSM (1)
    • SMA (1)
  • VMware (8)
  • Windows 2008 R2 (10)
    • Active Directory (7)
  • Windows 2012 R2 (1)
  • Windows 2016 (1)
  • Windows 7 (4)
    • BitLocker (1)
  • WordPress (1)
Proudly powered by WordPress | Theme: goldy-mex by inverstheme.