?
Solved

Remove groups from users different groups from different users. File as this.

Posted on 2008-10-03
11
Medium Priority
?
219 Views
Last Modified: 2012-05-05
Hi,

Remove groups from users different groups from different users. File as this.

I have a txt file as
Username,Group1,Group3
Username1,Groups3,Group4
Username2,Group6,Group1

So when script run has to query the first name and remove just these groups next to the users.
So each user may have different groups for each. Any way of doing this.

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
  • 6
  • 5
11 Comments
 
LVL 5

Expert Comment

by:si_shamil
ID: 22639019
Are you have an Active Directory environment or you just want it for Local computer?
0
 
LVL 11

Author Comment

by:bsharath
ID: 22639760
Its an Active directory environment
0
 
LVL 5

Expert Comment

by:si_shamil
ID: 22640586
Use this script, save it to "DeleteFromGroup.vbs"
Note: Runt it from command line!!!

Usage: cscript //NoLogo DeleteFromGroup.vbs --usersfile <file_path>
Example: cscript //NoLogo DeleteFromGroup.vbs --usersfile users.txt
'===============================================================================
' Script Name  : DeleteFromGroup.vbs
' Usage Syntax : cscript.exe //NoLogo DeleteFromGroup.vbs --usersfile <file_path>
' Author       : Alex Simenduev, PlanetIT.WS (http://www.planetit.ws)
' Version      : 1.0.1
' LastModified : 5/10/2008
' Description  : Removes users from groups by parsing a users text file.
'                Users file syntax: <USERNAME>.<GROUP[,GROUP[,...]]>
'                Example:
'                         user1, group1, group2
'                         user4, group8, group2, group3, group10
'===============================================================================
Option Explicit
 
' Global Variables
Dim gsUsage : gsUsage = "Usage: " & Wscript.ScriptName & " --usersfile <file_path>"
Dim gsFile, goFile, gsLine, gaSplitedLine, gsUser, gsGroup, gsUserDN, gsGroupDN
 
' Parse Command Line arguments
If Wscript.Arguments.Count = 2 Then
    If Lcase(Wscript.Arguments.Item(0)) = "--usersfile" Then
        gsFile = Wscript.Arguments.Item(1)
    Else
        Wscript.Echo gsUsage
        Wscript.Quit    
    End If
Else
    Wscript.Echo gsUsage
    Wscript.Quit
End If
 
Wscript.Echo "Opening Users file ('" & gsFile & "')..."
If openUserFile(gsFile) Then
    While Not goFile.AtEndOfStream
        gsLine = goFile.ReadLine
        
        ' Skip empty lines
        If gsLine <> "" Then
            gaSplitedLine = Split(gsLine, ",")
            If Ubound(gaSplitedLine) = 0 Then
                Wscript.Echo "Line '" & gsLine & "' cannot be parsed, skipping..."
            Else
                gsUser = Trim(gaSplitedLine(0))
                gsUserDN = GetUserDN(gsUser)
                
                Dim i : For i = 1 to Ubound(gaSplitedLine)
                    gsGroup = Trim(gaSplitedLine(i))
                    gsGroupDN = GetGroupDN(gsGroup)
                    
                    If gsUserDN <> "" AND gsGroupDN <> "" Then
                        Wscript.StdOut.Write "Removing user '" & gsUser & "' from '" & gsGroup & "' group..."
                        If removeUserFromGroup(gsUserDN, gsGroupDN) Then Wscript.Echo "Sucsess."
                    End If
                Next
            End If
        End If
    Wend
    
    goFile.Close
End If
 
'===============================================================================
' Function Name  : openUserFile(pFileName)
' Return value   : Boolean (True or False)
' Author         : Alex Simenduev, www.planetit.ws
' Date           : 04/10/2008
' Description    : Opens a file with user/group info
'===============================================================================
Function openUserFile(pFileName) : openUserFile = False : On Error Resume Next 
    Dim objFSO  : Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If objFSO.FileExists(pFileName) Then
        Set goFile = objFSO.OpenTextFile(pFileName)
        If Err.Number = 0 Then openUserFile = True
    Else
        Wscript.Echo "File '" & pFileName & "' not exists."
    End If
    
    Set objFile = Nothing
    Set objFSO = Nothing
End Function
 
'===============================================================================
' Function Name  : removeUserFromGroup(pUserDN, pGroupDN)
' Return value   : Boolean (True or False)
' Author         : Alex Simenduev, www.planetit.ws
' Date           : 04/10/2008
' Description    : Removes a given user from given group
'===============================================================================
Function removeUserFromGroup(pUserDN, pGroupDN) : removeUserFromGroup = False : On Error Resume Next
    Dim objGroup    : Set objUser = GetObject("LDAP://" & pUserDN)
    Dim objUser     : Set objGroup = GetObject("LDAP://" & pGroupDN)    
 
    If objGroup.IsMember(objUser.AdsPath) Then
        objGroup.Remove(objUser.AdsPath)
        If Err.Number = 0 Then removeUserFromGroup = True
    Else
        Wscript.Echo "Failed (not member of '" & objGroup.cn & "')."
    End If
 
    Set objGroup = Nothing
    Set objUser = Nothing
End Function
 
'===============================================================================
' Function Name  : getUserDN(pUserName)
' Return value   : String of Distinguished Name
' Author         : Alex Simenduev, www.planetit.ws
' Date           : 04/10/2008
' Description    : Gets the Distinguished Name from the given user name
'===============================================================================
Function getUserDN(pUserName) : getUserDN = "" : On Error Resume Next
    Dim objRootDSE  : Set objRootDSE = GetObject("LDAP://RootDSE")
    Dim strDomainDN : strDomainDN = objRootDSE.Get("defaultNamingContext")
 
    Dim strBase     : strBase	= "<LDAP://" & strDomainDN & ">;"    
    Dim strFilter   : strFilter	= "(&(objectCategory=person)(objectClass=user)(sAMAccountname=" & pUserName & "));"
    Dim strAttrs    : strAttrs	= "distinguishedName;"
    Dim strScope    : strScope	= "subtree"
 
    Dim objConn     : Set objConn = CreateObject("ADODB.Connection")
    objConn.Open "Provider=ADsDSOObject;"
 
    Dim objRS       : Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
    If Err.Number <> 0 Then
        ' Query failed
        Wscript.Echo "User name lookup for '" &  pUserName & "' failed, error number: " & Err.Number
        Exit Function
    End If
    
    ' Query succeeded
    If objRS.RecordCount = 0 Then
        WScript.Echo "User '" & pUserName & "' not found."
    Else
        objRS.MoveFirst
        getUserDN = objRS.Fields("distinguishedName")
    End If
 
    Set objRS = Nothing 
    Set objConn = Nothing
    Set objRootDSE = Nothing
End Function
 
'===============================================================================
' Function Name  : getGroupDN(pUserName)
' Return value   : String of Distinguished Name
' Author         : Alex Simenduev, www.planetit.ws
' Date           : 04/10/2008
' Description    : Gets the Distinguished Name from the given group name
'===============================================================================
Function getGroupDN(pGroupName) : getGroupDN = "" : On Error Resume Next
    Dim objRootDSE  : Set objRootDSE = GetObject("LDAP://RootDSE")
    Dim strDomainDN : strDomainDN = objRootDSE.Get("defaultNamingContext")
 
    Dim strBase     : strBase	= "<LDAP://" & strDomainDN & ">;"    
    Dim strFilter   : strFilter	= "(&(objectCategory=group)(cn=" & pGroupName & "));"
    Dim strAttrs    : strAttrs	= "distinguishedName;"
    Dim strScope    : strScope	= "subtree"
 
    Dim objConn     : Set objConn = CreateObject("ADODB.Connection")
    objConn.Open "Provider=ADsDSOObject;"
 
    Dim objRS       : Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
    If Err.Number <> 0 Then
        ' Query failed
        Wscript.Echo "Group name lookup for '" &  pGroupName & "' failed, error number: " & Err.Number
        Exit Function
    End If
    
    ' Query succeeded
    If objRS.RecordCount = 0 Then
        WScript.Echo "Group '" & pGroupName & "' not found."
    Else
        objRS.MoveFirst
        getGroupDN = objRS.Fields("distinguishedName")
    End If
 
    Set objRS = Nothing 
    Set objConn = Nothing
    Set objRootDSE = Nothing
End Function

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Author Comment

by:bsharath
ID: 22640612
Thank U... Will this work for groups that are on the root Domain?
0
 
LVL 5

Expert Comment

by:si_shamil
ID: 22640635
No it will work for domain that running user belongs to.
0
 
LVL 11

Author Comment

by:bsharath
ID: 22640664
Can you add the case to remove the groups no matter which domain they are.

I have users who are a member of a group in the local domain and the root domain too....
0
 
LVL 5

Expert Comment

by:si_shamil
ID: 22640701
The script is not an issue, it can be changed to work with all Forest easily. The issue is your txt file.
If you have group in local domain with same name that in root doamin, then it is impossible to know what group you meant when specified in the TXT file.

0
 
LVL 11

Author Comment

by:bsharath
ID: 22640724
It works perfect on a local Domain....
Thank U
Can you add the functionality to remove groups even if the user is a member in the root domain
0
 
LVL 11

Author Comment

by:bsharath
ID: 22640729
No i will no way have that senario.... The groups are unique.
Only case we need to think is if the groups name is "India" then it has to remove just "India" not groups as "India1" or "1_India"

Only if found exact match then remove...
0
 
LVL 5

Accepted Solution

by:
si_shamil earned 2000 total points
ID: 22640755
OK, up to you, if you do have similar names across domains, then script may remove wrong group from the user.

Regarding the group name, it will search only for exact name, so don't worry

Here is a changed script.
'===============================================================================
' Script Name  : DeleteFromGroup.vbs
' Usage Syntax : cscript.exe //NoLogo DeleteFromGroup.vbs --usersfile <file_path>
' Author       : Alex Simenduev, PlanetIT.WS (http://www.planetit.ws)
' Version      : 1.0.1
' LastModified : 5/10/2008
' Description  : Removes users from groups by parsing a users text file.
'                Users file syntax: <USERNAME>.<GROUP[,GROUP[,...]]>
'                Example:
'                         user1, group1, group2
'                         user4, group8, group2, group3, group10
'===============================================================================
Option Explicit
 
' Global Variables
Dim gsUsage : gsUsage = "Usage: " & Wscript.ScriptName & " --usersfile <file_path>"
Dim gsFile, goFile, gsLine, gaSplitedLine, gsUser, gsGroup, gsUserDN, gsGroupDN
 
' Parse Command Line arguments
If Wscript.Arguments.Count = 2 Then
    If Lcase(Wscript.Arguments.Item(0)) = "--usersfile" Then
        gsFile = Wscript.Arguments.Item(1)
    Else
        Wscript.Echo gsUsage
        Wscript.Quit    
    End If
Else
    Wscript.Echo gsUsage
    Wscript.Quit
End If
 
Wscript.Echo "Opening Users file ('" & gsFile & "')..."
If openUserFile(gsFile) Then
    While Not goFile.AtEndOfStream
        gsLine = goFile.ReadLine
        
        ' Skip empty lines
        If gsLine <> "" Then
            gaSplitedLine = Split(gsLine, ",")
            If Ubound(gaSplitedLine) = 0 Then
                Wscript.Echo "Line '" & gsLine & "' cannot be parsed, skipping..."
            Else
                gsUser = Trim(gaSplitedLine(0))
                gsUserDN = GetUserDN(gsUser)
                
                Dim i : For i = 1 to Ubound(gaSplitedLine)
                    gsGroup = Trim(gaSplitedLine(i))
                    gsGroupDN = GetGroupDN(gsGroup)
                    
                    If gsUserDN <> "" AND gsGroupDN <> "" Then
                        Wscript.StdOut.Write "Removing user '" & gsUser & "' from '" & gsGroup & "' group..."
                        If removeUserFromGroup(gsUserDN, gsGroupDN) Then Wscript.Echo "Sucsess."
                    End If
                Next
            End If
        End If
    Wend
    
    goFile.Close
End If
 
'===============================================================================
' Function Name  : openUserFile(pFileName)
' Return value   : Boolean (True or False)
' Author         : Alex Simenduev, www.planetit.ws
' Date           : 04/10/2008
' Description    : Opens a file with user/group info
'===============================================================================
Function openUserFile(pFileName) : openUserFile = False : On Error Resume Next 
    Dim objFSO  : Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If objFSO.FileExists(pFileName) Then
        Set goFile = objFSO.OpenTextFile(pFileName)
        If Err.Number = 0 Then openUserFile = True
    Else
        Wscript.Echo "File '" & pFileName & "' not exists."
    End If
    
    Set objFile = Nothing
    Set objFSO = Nothing
End Function
 
'===============================================================================
' Function Name  : removeUserFromGroup(pUserDN, pGroupDN)
' Return value   : Boolean (True or False)
' Author         : Alex Simenduev, www.planetit.ws
' Date           : 04/10/2008
' Description    : Removes a given user from given group
'===============================================================================
Function removeUserFromGroup(pUserDN, pGroupDN) : removeUserFromGroup = False : On Error Resume Next
    Dim objGroup    : Set objUser = GetObject("LDAP://" & pUserDN)
    Dim objUser     : Set objGroup = GetObject("LDAP://" & pGroupDN)    
 
    If objGroup.IsMember(objUser.AdsPath) Then
        objGroup.Remove(objUser.AdsPath)
        If Err.Number = 0 Then removeUserFromGroup = True
    Else
        Wscript.Echo "Failed (not member of '" & objGroup.cn & "')."
    End If
 
    Set objGroup = Nothing
    Set objUser = Nothing
End Function
 
'===============================================================================
' Function Name  : getUserDN(pUserName)
' Return value   : String of Distinguished Name
' Author         : Alex Simenduev, www.planetit.ws
' Date           : 04/10/2008
' Description    : Gets the Distinguished Name from the given user name
'===============================================================================
Function getUserDN(pUserName) : getUserDN = "" : On Error Resume Next
    Dim objRootDSE  : Set objRootDSE = GetObject("LDAP://RootDSE")
    Dim strDomainDN : strDomainDN = objRootDSE.Get("defaultNamingContext")
 
    Dim strBase     : strBase	= "<LDAP://" & strDomainDN & ">;"    
    Dim strFilter   : strFilter	= "(&(objectCategory=person)(objectClass=user)(sAMAccountname=" & pUserName & "));"
    Dim strAttrs    : strAttrs	= "distinguishedName;"
    Dim strScope    : strScope	= "subtree"
 
    Dim objConn     : Set objConn = CreateObject("ADODB.Connection")
    objConn.Open "Provider=ADsDSOObject;"
 
    Dim objRS       : Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
    If Err.Number <> 0 Then
        ' Query failed
        Wscript.Echo "User name lookup for '" &  pUserName & "' failed, error number: " & Err.Number
        Exit Function
    End If
    
    ' Query succeeded
    If objRS.RecordCount = 0 Then
        WScript.Echo "User '" & pUserName & "' not found."
    Else
        objRS.MoveFirst
        getUserDN = objRS.Fields("distinguishedName")
    End If
 
    Set objRS = Nothing 
    Set objConn = Nothing
    Set objRootDSE = Nothing
End Function
 
'===============================================================================
' Function Name  : getGroupDN(pUserName)
' Return value   : String of Distinguished Name
' Author         : Alex Simenduev, www.planetit.ws
' Date           : 04/10/2008
' Description    : Gets the Distinguished Name from the given group name
'===============================================================================
Function getGroupDN(pGroupName) : getGroupDN = "" : On Error Resume Next
    Dim objRootDSE  : Set objRootDSE = GetObject("LDAP://RootDSE")
    Dim strDomainDN : strDomainDN = objRootDSE.Get("defaultNamingContext")
 
    Dim strBase     : strBase	= "<GC://" & strDomainDN & ">;"    
    Dim strFilter   : strFilter	= "(&(objectCategory=group)(cn=" & pGroupName & "));"
    Dim strAttrs    : strAttrs	= "distinguishedName;"
    Dim strScope    : strScope	= "subtree"
 
    Dim objConn     : Set objConn = CreateObject("ADODB.Connection")
    objConn.Open "Provider=ADsDSOObject;"
 
    Dim objRS       : Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
    If Err.Number <> 0 Then
        ' Query failed
        Wscript.Echo "Group name lookup for '" &  pGroupName & "' failed, error number: " & Err.Number
        Exit Function
    End If
    
    ' Query succeeded
    If objRS.RecordCount = 0 Then
        WScript.Echo "Group '" & pGroupName & "' not found."
    Else
        objRS.MoveFirst
        getGroupDN = objRS.Fields("distinguishedName")
    End If
 
    Set objRS = Nothing 
    Set objConn = Nothing
    Set objRootDSE = Nothing
End Function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 22640762
0

Featured Post

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

762 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