KiXforms Forum Index KiXforms
The Forum for the KiXforms Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Quick Links 
Site News
Downloads
Documentation
Donations
Script Archive
Tracking Systems

Copy Group Members To New Group

 
Post new topic   Reply to topic    KiXforms Forum Index -> Script Archive
View previous topic :: View next topic  
Author Message
sptomey
KiXforms Enthusiast
KiXforms Enthusiast


Joined: 18 Mar 2003
Posts: 158

PostPosted: Mon Apr 26, 2004 10:11 pm    Post subject: Copy Group Members To New Group Reply with quote

Here is a little form I wrote up that will allow you to enter as many "old" and "new" groups into a listview, click copy, and it will copy all the members of the old groups to the corresponding new groups. Plan on adding an option to create the new group if it doesnt already exist...Just waiting for the treeview so you could browse and select OU to create it in.

Thanks to Bryce for the Groupmembers() and GroupAdd() UDFs...

Code:
Break On
$System = CreateObject("Kixtart.System")
If VarType($System)<>9 or @ERROR
  $null=MessageBox("Unable to initialize the KiXforms system namespace!","Error",0)
  exit 0
Endif

;KD START

;************* Form **************
$Form = $System.Form()
$Form.BackColor = 224,223,227
$Form.Height = 320
$Form.Left = 102
$Form.Text = "Member Copier"
$Form.Top = 18
$Form.Width = 295
;**************************************

;************* TitleLabel **************
$TitleLabel = $Form.Controls.Label("Member Copier 1.0", 10, 0, 180, 28)
$TitleLabel.BackColor = 224,223,227
$TitleLabel.FontSize = 14
;**************************************

;************* BarLabel **************
$BarLabel = $Form.Controls.Label("____________________________________________", 10, 20, 270, 20)
$BarLabel.BackColor = 224,223,227
;**************************************

;************* OldGroupLabel **************
$OldGroupLabel = $Form.Controls.Label("Old Group", 10, 40, 100, 15)
$OldGroupLabel.BackColor = 224,223,227
;**************************************

;************* OldGroupTextbox **************
$OldGroupTextbox = $Form.Controls.TextBox("", 10, 55, 100, 20)
;**************************************

;************* NewGroupLabel **************
$NewGroupLabel = $Form.Controls.Label("New Group", 120, 40, 100, 15)
$NewGroupLabel.BackColor = 224,223,227
;**************************************

;************* NewGroupTextbox **************
$NewGroupTextbox = $Form.Controls.TextBox("", 120, 55, 100, 20)
;**************************************

;************* AddButton **************
$AddButton = $Form.Controls.Button("Add", 230, 55, 45, 20)
$AddButton.OnClick = "AddGroups()"
;**************************************

;************* GroupsListView **************
$GroupsListView = $Form.Controls.ListView("ListViewEx1", 10, 85, 265, 82)
$GroupsListView.AutoArrange = "True"
$GroupsListView.FullRowSelect = "True"
$GroupsListView.HideSelection = "False"
$OldGrpColumn = $GroupsListView.Columns.Add("Old Group",124,0)
$NewGrpColumn = $GroupsListView.Columns.Add("New Group",124,0)
;**************************************

;************* CopyButton **************
$CopyButton = $Form.Controls.Button("Copy Users", 40, 177, 75, 20)
$CopyButton.OnClick = "CopyMembers()"
;**************************************

;************* ExitButton **************
$ExitButton = $Form.Controls.Button("Exit", 150, 177, 75, 20)
$ExitButton.OnClick = "$$Form.Hide"
;**************************************

;************* StatusLabel **************
$StatusLabel = $Form.Controls.Label("Status", 10, 205, 100, 15)
$StatusLabel.BackColor = 224,223,227
;**************************************

;************* StatusListbox **************
$StatusListbox = $Form.Controls.ListBox("", 10, 220, 265, 55)
;**************************************

;KD END

$Form.Show
$OldGroupTextbox.SetFocus()
While $Form.Visible
   $=Execute($Form.DoEvents())
Loop
Exit 1

;*********************************
;************* UDFs **************
;*********************************

; ------------------------------------------
;    Obtains A List Of Members From A Specified Group
; ------------------------------------------
FUNCTION Groupmembers($target, $group, optional $flag)
   ;Can Be Found At  http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB12&Number=81736
   DIM $temparray[8], $member, $i, $chunk, $flag, $ADSIFlag, $filterFlag
   $chunk = ubound($temparray)
   $flag = val($flag)
   $i = 0
   $group = getobject("WinNT://$target/$group")
   if vartype($group) <> 9 exit(@error) endif
   select
      case $flag & 1
         $filterflag = 1
      case $flag & 2
         $filterflag = 2
      case $flag & 4
         $filterflag = 4
      case 1
         $filterflag = 1
   endselect
   select
      case $flag & 8
         $ADSIFlag = 8
      case $flag & 16
         $ADSIFlag = 16
   endselect
   for each $member in $group.members
      select
         case $filterflag = 2 AND $member.class = "user"
            if substr($member.name,len($member.name),1) <> "$"
               $temparray[$i] = $member.name
               select
                  case $adsiflag = 8
                     $temparray[$i] = $member.adspath
                  case $adsiflag = 16
                     $temparray[$i] = $member
               endselect
               $i = $i + 1
            endif
         case $filterflag = 4 AND $member.class = "Group"
            if substr($member.name,len($member.name),1) <> "$"
               $temparray[$i] = $member.name
               select
                  case $adsiflag = 8
                     $temparray[$i] = $member.adspath
                  case $adsiflag = 16
                     $temparray[$i] = $member
               endselect
               $i = $i + 1
            endif
         case $filterflag = 1
            if substr($member.name,len($member.name),1) <> "$"
               $temparray[$i] = $member.name
               select
                  case $adsiflag = 8
                     $temparray[$i] = $member.adspath
                  case $adsiflag = 16
                     $temparray[$i] = $member
               endselect
               $i = $i + 1
            endif
         case $filterflag
            ;bit bucket
      endselect
      if $i = ubound($temparray)
         redim preserve $temparray[Ubound($temparray)+$chunk]
      endif
   next
   if $i <> 0
      redim preserve $temparray[$i-1]
      $groupmembers=$temparray
   endif
ENDFUNCTION

; ------------------------------------------
;    Adds Users To A Specified Group
; ------------------------------------------
FUNCTION GroupAdd($Target, $Group, $ObjToAdd)
   ;Can Be Found At  http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB12&Number=81735
   DIM $target, $group, $OBJToAdd
   DIM $obj, $objhome
   if substr($target,1,2) = "\\"
      $target = substr($target,3,len($target))
   endif
   If instr($objtoadd,"\") <> 0
      $objtoadd   = split($objtoadd,"\")
      if ubound($objtoadd) <> 1 exit(13) endif
      $objhome    = $objtoadd[0]
      $obj      = $objtoadd[1]
   ELSE
      $objhome    = $target
      $obj      = $objtoadd
   endif
   $group = GetObject("WinNT://$target/$group")
   if vartype($group) = 9 and @error = 0
      $group.Add ("WinNT://$objhome/$obj")
   else
      $GroupAdd = @error
      exit(@error)
   endif
   $GroupAdd = @error
   exit(@error)
ENDFUNCTION

; ------------------------------------------
;    Adds Groups From Boxes To ListView
; ------------------------------------------
FUNCTION AddGroups()
   Dim $group1,$group2,$null,$OldGrpCol
   $group1 = $OldGroupTextbox.Text
   $group2 = $NewGroupTextbox.Text
   If Len ($group1) < 1
      $null = MessageBox("Please Enter A Group Name Into The Old Group Box","Error",0)
      $OldGroupTextbox.SetFocus()
      Exit 0
   Else
      $oldgroup = getobject("WinNT://"+@LDomain+"/$group1")
      If vartype($oldgroup) <> 9
         $null = MessageBox("The Group Name In The Old Group Box Does Not Seem To Be Valid.  Please Re-enter","Error",0)
         $OldGroupTextbox.SetFocus()
         Exit 0
      Endif
      $oldgroup = 0
   Endif
   If Len ($group2) < 1
      $null = MessageBox("Please Enter A Group Name Into The New Group Box","Error",0)
      $NewGroupTextbox.SetFocus()
      Exit 0
   Else
      $newgroup = getobject("WinNT://"+@LDomain+"/$group2")
      If vartype($newgroup) <> 9
         $null = MessageBox("The Group Name In The New Group Box Does Not Seem To Be Valid.  Please Re-enter","Error",0)
         $NewGroupTextbox.SetFocus()
         Exit 0
      Endif
      $newgroup = 0
   Endif
   $OldGrpCol = $GroupsListView.Items.Add($group1)
   $OldGrpCol.SubItems(1).Text = $group2
ENDFUNCTION

; ------------------------------------------
;    Adds A Line To Status List And Selects Last Line
; ------------------------------------------
FUNCTION Status($text)
   $StatusListbox.AddItem($text)
   $StatusListbox.ListIndex=$StatusListbox.ListCount-1
ENDFUNCTION

; ------------------------------------------
;    Copies Members One Group To Another
; ------------------------------------------
FUNCTION CopyMembers()
   Dim $item,$group1,$group2,$group1members,$memberstomove,$membercount,$member,$rc,$errors,$statuslistcount
   For Each $item in $GroupsListView.Items
      $group1 = $item.SubItems(0).Text
      $group2 = $item.SubItems(1).Text
      Status ("Copying members of "+$group1+" to "+$group2+"")
      $group1members = Groupmembers(@LDomain,$group1,18)
      $memberstomove = UBound($group1members)+1
      $membercount = 0
      For Each $member in $group1members
         If $statuslistcount > 0
            $StatusListbox.RemoveItem ($statuslistcount)
         Endif
         $membercount = $membercount+1
         Status ("Adding "+$member.name+" to "+$group2+"("+$membercount+"of"+$memberstomove+")")
         $rc = GroupAdd(@LDomain, $group2, $member.name)
         If $rc <> 0
            Status ("Error "+@SError+" when adding "+$member.name+" to "+$group2)
            $errors = 1
            $statuslistcount = 0
         Else
            $statuslistcount = $StatusListbox.ListCount-1
         Endif
      Next
      If $statuslistcount > 0
         $StatusListbox.RemoveItem ($statuslistcount)
      Endif
   Next
   If $errors = 0
      Status ("Member Copy Was Successful")
   Else
      Status ("There Were Errors Encountered, Please Review Status")
   Endif
ENDFUNCTION

_________________
-Shane
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> Script Archive All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group