LBMCreation
Technical User
Hi All, I have been tasked at work to build a script which can do the following.
1. Add all users from a OU to a group i.e. ou=user1 to group1 etc
2. Delete users from group1 if no longer in ou=user1
3. This has to be done on a input file basis for example:
Excel: HeaderA1(Users OU) | HeaderB1(Group CN)
OU=Users,DC=blah,DC=com CN=Group1,DC=blah,DC=com
I do currently have a script which adds all users from a OU to a group, but the problem is their are going to be loads of groups which need populating by different OU's. It would be would better to be able to just add a line to the excel doc to specify a new users to group instruction.
=================================================================
My script is:
OPTION EXPLICIT
dim strFilter, strRoot, strScope, strGroupName
dim strNETBIOSDomain, strGroupDN
dim cmd, rs,cn, objGroup
strFilter = "(&(objectCategory=person)(objectClass=user))"
strScope = "subtree"
strRoot = "OU=Users,DC=blah,DC=com"
strNETBIOSDomain = "LBMSLN"
strGroupName = "Group1"
strGroupDN = GetDN(strNETBIOSDomain,strGroupName)
set objGroup = getobject("LDAP://" & strGroupDN)
set cmd = createobject("ADODB.Command")
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")
cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn
cmd.commandtext = "<LDAP://" & strRoot & ">;" & strFilter & ";ADsPath,sAMAccountName;" & strScope
cmd.properties("page size")=1000
set rs = cmd.execute
while rs.eof <> true and rs.bof <> true
on error resume next
objGroup.Add rs("ADsPath")
if err.number = -2147019886 then
elseif err.number <> 0 then
else
end if
err.clear
on error goto 0
rs.movenext
wend
cn.close
Function GetDN(byval strDomain,strObject)
DIM objTrans
Set objTrans = CreateObject("NameTranslate")
objTrans.Init 1, strDomain
objTrans.Set 3, strDomain & "\" & strObject
GetDN = objTrans.Get(1)
end function
================================================================
I also have a delete script which basically is the same but reversed
This script is going to be run at midnight each night unattended.
Hope you all can help with this.
Many Thanks
1. Add all users from a OU to a group i.e. ou=user1 to group1 etc
2. Delete users from group1 if no longer in ou=user1
3. This has to be done on a input file basis for example:
Excel: HeaderA1(Users OU) | HeaderB1(Group CN)
OU=Users,DC=blah,DC=com CN=Group1,DC=blah,DC=com
I do currently have a script which adds all users from a OU to a group, but the problem is their are going to be loads of groups which need populating by different OU's. It would be would better to be able to just add a line to the excel doc to specify a new users to group instruction.
=================================================================
My script is:
OPTION EXPLICIT
dim strFilter, strRoot, strScope, strGroupName
dim strNETBIOSDomain, strGroupDN
dim cmd, rs,cn, objGroup
strFilter = "(&(objectCategory=person)(objectClass=user))"
strScope = "subtree"
strRoot = "OU=Users,DC=blah,DC=com"
strNETBIOSDomain = "LBMSLN"
strGroupName = "Group1"
strGroupDN = GetDN(strNETBIOSDomain,strGroupName)
set objGroup = getobject("LDAP://" & strGroupDN)
set cmd = createobject("ADODB.Command")
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")
cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn
cmd.commandtext = "<LDAP://" & strRoot & ">;" & strFilter & ";ADsPath,sAMAccountName;" & strScope
cmd.properties("page size")=1000
set rs = cmd.execute
while rs.eof <> true and rs.bof <> true
on error resume next
objGroup.Add rs("ADsPath")
if err.number = -2147019886 then
elseif err.number <> 0 then
else
end if
err.clear
on error goto 0
rs.movenext
wend
cn.close
Function GetDN(byval strDomain,strObject)
DIM objTrans
Set objTrans = CreateObject("NameTranslate")
objTrans.Init 1, strDomain
objTrans.Set 3, strDomain & "\" & strObject
GetDN = objTrans.Get(1)
end function
================================================================
I also have a delete script which basically is the same but reversed
This script is going to be run at midnight each night unattended.
Hope you all can help with this.
Many Thanks