Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to remove members from groups.

Posted on 2010-09-16
8
Medium Priority
?
526 Views
Last Modified: 2012-05-10
Hi,

Script to remove members from groups.
I have a txt file as this

Groupname1;Ntlogin1,Ntlogin2,ntlogin3
Groupname2;Ntlogin1,Ntlogin2,ntlogin3

When script run has to check each row group name and remove members. If any failures record it in a txt log file.

regards
Sharath
0
Comment
Question by:bsharath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33689738
check it out.
the following cases will be logged:
1. group was not found
2. member if file is not member of the group
3. member was deleted from group
Const ADS_PROPERTY_DELETE = 4 
const SOURCE_FILE = "c:\temp\group_members.txt"
const LOG_FILE = "c:\temp\group_members.log"

Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(SOURCE_FILE, 1)
set objLog  = objFSO.CreateTextFile(LOG_FILE, 2)
groupMembersArr = Split(objFile.ReadAll, vbNewLine)
objFile.Close

for each line in groupMembersArr
	groupName = Split(Line, ";")(0)
	groupMembers = Split(Split(Line, ";")(1), ",")
	groupPath = getGroupPath(groupName)
	if groupPath = "" then
		objLog.WriteLine "Group " & groupName & " could not be found"
	else
		arrMemberOf = GetGroupMmebers(groupPath)
		for each delMember in groupMembers
			isMember=false
			for each member in arrMemberOf
				memberName = Split(Split(member, "CN=")(1), ",")(0)
				if memberName = delMember then
					isMember = true
					DeleteGroupMember groupPath, member
					objLog.WriteLine memberName & " was removed from group " & groupName
				end if
			next
			if isMember = false then
				objLog.WriteLine delMember & " is no member of group " & groupName
			end if
		next 
	end if
Next

objLog.Close
wscript.echo "Done"

function GetGroupMmebers(groupPath)
	Set objGroup = GetObject(groupPath)
	objGroup.GetInfo
	 
	arrMemberOf = objGroup.GetEx("member")
	GetGroupMmebers = arrMemberOf
end function

sub DeleteGroupMember(groupPath, member)
	Set objGroup = GetObject(groupPath) 
	 
	objGroup.PutEx ADS_PROPERTY_DELETE, "member", Array(member)
	 
	objGroup.SetInfo
end sub

function getNC
	set objRoot=getobject("LDAP://RootDSE")
	getNC=objRoot.get("defaultNamingContext")
end function

function getGroupPath(groupname)

	set cmd=createobject("ADODB.Command")
	set cn=createobject("ADODB.Connection")
	set rs=createobject("ADODB.Recordset")
	
	cn.open "Provider=ADsDSOObject;"
	
	cmd.commandtext = "SELECT adspath from 'LDAP://" & getnc & _
			  "' WHERE objectCategory = 'Group' and name = '" & groupname & "'"
	cmd.activeconnection = cn
	
	set rs = cmd.execute
	
	if rs.bof <> true and rs.eof<>true then
		getgrouppath=rs(0)
	else
		getgrouppath = ""
	end if
	cn.close

end function

Open in new window

0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33689740
2. member IN file is not member of the group
0
 
LVL 11

Author Comment

by:bsharath
ID: 33689776
I get as
sharathuy is no member of group Docking

I have this group i am sure and i am member but get the above
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33697299
the script is case sensitive so make sure the name of the member is exactly the same as displayed in active directory
0
 
LVL 11

Author Comment

by:bsharath
ID: 33698063
Thanks it works.
Can we remove the case issue. That would be difficult if i need to match the case for each name
0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 33710486
i changed the script to so members applied with case-insensitive comparison
Const ADS_PROPERTY_DELETE = 4 
const SOURCE_FILE = "c:\temp\group_members.txt"
const LOG_FILE = "c:\temp\group_members.log"

Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(SOURCE_FILE, 1)
set objLog  = objFSO.CreateTextFile(LOG_FILE, 2)
groupMembersArr = Split(objFile.ReadAll, vbNewLine)
objFile.Close

for each line in groupMembersArr
	groupName = Split(Line, ";")(0)
	groupMembers = Split(Split(Line, ";")(1), ",")
	groupPath = getGroupPath(groupName)
	if groupPath = "" then
		objLog.WriteLine "Group " & groupName & " could not be found"
	else
		arrMemberOf = GetGroupMmebers(groupPath)
		for each delMember in groupMembers
			isMember=false
			for each member in arrMemberOf
				memberName = Split(Split(member, "CN=")(1), ",")(0)
				if UCase(memberName) = UCase(delMember) then
					isMember = true
					DeleteGroupMember groupPath, member
					objLog.WriteLine memberName & " was removed from group " & groupName
				end if
			next
			if isMember = false then
				objLog.WriteLine delMember & " is no member of group " & groupName
			end if
		next 
	end if
Next

objLog.Close
wscript.echo "Done"

function GetGroupMmebers(groupPath)
	Set objGroup = GetObject(groupPath)
	objGroup.GetInfo
	 
	arrMemberOf = objGroup.GetEx("member")
	GetGroupMmebers = arrMemberOf
end function

sub DeleteGroupMember(groupPath, member)
	Set objGroup = GetObject(groupPath) 
	 
	objGroup.PutEx ADS_PROPERTY_DELETE, "member", Array(member)
	 
	objGroup.SetInfo
end sub

function getNC
	set objRoot=getobject("LDAP://RootDSE")
	getNC=objRoot.get("defaultNamingContext")
end function

function getGroupPath(groupname)

	set cmd=createobject("ADODB.Command")
	set cn=createobject("ADODB.Connection")
	set rs=createobject("ADODB.Recordset")
	
	cn.open "Provider=ADsDSOObject;"
	
	cmd.commandtext = "SELECT adspath from 'LDAP://" & getnc & _
			  "' WHERE objectCategory = 'Group' and name = '" & groupname & "'"
	cmd.activeconnection = cn
	
	set rs = cmd.execute
	
	if rs.bof <> true and rs.eof<>true then
		getgrouppath=rs(0)
	else
		getgrouppath = ""
	end if
	cn.close

end function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 33710527
Thanks a lot works perfect
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33710558
do u have any open questions which i assist you with, but didn't provide you with a solution yet?
i lost track of all the questions i'm participating in.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question