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

Checkbox for AD Groups

 
Post new topic   Reply to topic    KiXforms Forum Index -> Advanced Scripting
View previous topic :: View next topic  
Author Message
danilda
KiXforms Follower
KiXforms Follower


Joined: 20 Jul 2007
Posts: 16

PostPosted: Tue Aug 14, 2007 5:55 pm    Post subject: Checkbox for AD Groups Reply with quote

I would like to have checkboxes by the list of groups that I retrieve from my AD. I have figured out how to retrieve the groups, but can't figure out how to automatically create the checkboxes and have the spacing correct on them. Can anyone provide guidance. Have attached the script that I have so far. Not much though.

Thanks for your help.

Dee



checkbox.kix
 Description:

Download
 Filename:  checkbox.kix
 Filesize:  2.12 KB
 Downloaded:  1449 Time(s)



Last edited by danilda on Wed Aug 15, 2007 5:53 pm; edited 1 time in total
Back to top
View user's profile Send private message
Les
KiXforms Aficionado
KiXforms Aficionado


Joined: 24 Dec 2003
Posts: 317

PostPosted: Wed Aug 15, 2007 1:55 pm    Post subject: Reply with quote

Generally speaking, attachments are for complete works of art to share or very very long scripts. If looking for help, you odds improve if you post a stripped down scriptlet of code that repro's the issue. Put the code in code tags.
_________________
The Repro Man
Stealing for a living!
Back to top
View user's profile Send private message
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Wed Aug 15, 2007 2:24 pm    Post subject: Reply with quote

could you give us a more information on what kind of form design you are looking for? you mention you want checkboxes but not what you want to use them for, it would make a difference in how the form would be designed. how are you wanting the group, group adspath, user name, user name adspath displayed?
_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
Gargoyle
KiXforms Aficionado
KiXforms Aficionado


Joined: 30 Dec 2003
Posts: 366
Location: Arizona

PostPosted: Wed Aug 15, 2007 2:34 pm    Post subject: Reply with quote

You want to build what is called a control array.


Basically
Code:

 For each $Item in $Array
  $Checkbox[$]= ...
  $ =$+1
 next

_________________
Parents were invented to make children happy by giving them something to ignore.
Back to top
View user's profile Send private message
danilda
KiXforms Follower
KiXforms Follower


Joined: 20 Jul 2007
Posts: 16

PostPosted: Wed Aug 15, 2007 4:57 pm    Post subject: Reply with quote

Sorry Guys,

I just figured out how to pull my groups from AD and at that point I was stuck on how to proceed. I am creating an app that my Help Desk will use to create user accounts and I need to pull all the groups that are available to the users into a form so they can check which groups to put the users in. This is only when they can't use a template for a specific user. If anyone already has something created that I can use, I would appreciate it.

thanks -- dee
Back to top
View user's profile Send private message
danilda
KiXforms Follower
KiXforms Follower


Joined: 20 Jul 2007
Posts: 16

PostPosted: Wed Aug 15, 2007 5:54 pm    Post subject: Reply with quote

I have added some more coding to the script so you can get a better idea of what I am trying to do.

thanks -- dee
Back to top
View user's profile Send private message
benny69
KiXforms Advocate
KiXforms Advocate


Joined: 30 Oct 2003
Posts: 567
Location: Lincoln, Ne

PostPosted: Tue Aug 21, 2007 8:39 pm    Post subject: Reply with quote

Hi danilda,
I have been thinking about what you are asking for and believe this would be a better approach, let me know what you think. I have chosen to write this in KF.Net rather than KF.Classic, KF.Net is the newer ver of KiXforms, you will need to install this Kixforms Development Build before you can run the code I have provided.

This is written to work on a Active Directory Network:

Code:

;region Setup Variables
Break On
$System = CreateObject("Kixforms.System")
If Not $System
   $nul= MessageBox("KiXforms.Net Not Initiated. This Script Will Now Close.","Error",16)
   Quit()
EndIf

$objRootDSE = GetObject("LDAP://rootDSE")
$defaultNamingContext = $objRootDSE.Get("defaultNamingContext")
;endregion

;region Main Form
$Form = $System.Form()
$Form.StartPosition = 1  ;FormStartPosition_CenterScreen
$Form.Size = $System.Size(543,444) ;(Width,Height)
$Form.Text = "Edit User Groups"

;region StatusBar
$StatusBar = $System.StatusBar()
$nul = $Form.Controls.Add($StatusBar)

$ProgressBar = $System.ProgressBar()
$ProgressBar.Left = $StatusBar.Right-20-100
$ProgressBar.Height = 18
$ProgressBar.Top = 3
$ProgressBar.Width = 100
$ProgressBar.Value = 0
$ProgressBar.Visible = 0
$nul = $StatusBar.Controls.Add($ProgressBar)
;endregion

;region User Groups GroupBox
$GroupBox = $System.GroupBox()
$GroupBox.Height = 330
$GroupBox.Left = 10
$GroupBox.Text = "User Groups"
$GroupBox.Top = 40
$GroupBox.Width = 430
$GroupBox.Enabled = 0
$nul = $Form.Controls.Add($GroupBox)

;region Available Groups
$AvailableGroupsLbl = $System.Label()
$AvailableGroupsLbl.Height = 20
$AvailableGroupsLbl.Left = 10
$AvailableGroupsLbl.Text = "Available Groups:"
$AvailableGroupsLbl.TextAlign = 16  ;MiddleLeft
$AvailableGroupsLbl.Top = 20
$nul = $GroupBox.Controls.Add($AvailableGroupsLbl)

$AvailableGroupsListBox = $System.ListBox()
$AvailableGroupsListBox.DoubleClick = "AddToMemberOf()"
$AvailableGroupsListBox.Height = 251
$AvailableGroupsListBox.Left = 10
$AvailableGroupsListBox.Sorted = -1  ;True
$AvailableGroupsListBox.Top = 40
$AvailableGroupsListBox.Width = 180
$nul = $GroupBox.Controls.Add($AvailableGroupsListBox)

$RefreshAvailableBtn = $System.Button()
$RefreshAvailableBtn.Click = "RefreshAvailableGroups()"
$RefreshAvailableBtn.Left = 60
$RefreshAvailableBtn.Text = "Refresh"
$RefreshAvailableBtn.Top = 300
$nul = $GroupBox.Controls.Add($RefreshAvailableBtn)
;endregion

;region Member Of
$MemberOfLbl = $System.Label()
$MemberOfLbl.Height = 20
$MemberOfLbl.Left = 240
$MemberOfLbl.Text = "Member Of:"
$MemberOfLbl.TextAlign = 16  ;MiddleLeft
$MemberOfLbl.Top = 20
$nul = $GroupBox.Controls.Add($MemberOfLbl)

$MemberOfListBox = $System.ListBox()
$MemberOfListBox.DoubleClick = "RemoveFromMemberOf()"
$MemberOfListBox.Height = 251
$MemberOfListBox.Left = 240
$MemberOfListBox.Sorted = -1  ;True
$MemberOfListBox.Top = 40
$MemberOfListBox.Width = 180
$nul = $GroupBox.Controls.Add($MemberOfListBox)

$RefreshMemberOfBtn = $System.Button()
$RefreshMemberOfBtn.Click = "RefreshMemberOfGroups()"
$RefreshMemberOfBtn.Left = 290
$RefreshMemberOfBtn.Text = "Refresh"
$RefreshMemberOfBtn.Top = 300
$nul = $GroupBox.Controls.Add($RefreshMemberOfBtn)
;endregion

;region User Group Controls
$AddAllBtn = $System.Button()
$AddAllBtn.Click = "AddAllToMemberOf()"
$AddAllBtn.Left = 200
$AddAllBtn.Text = ">>"
$AddAllBtn.Top = 110
$AddAllBtn.Width = 30
$nul = $GroupBox.Controls.Add($AddAllBtn)

$AddBtn = $System.Button()
$AddBtn.Click = "AddToMemberOf()"
$AddBtn.Left = 200
$AddBtn.Text = ">"
$AddBtn.Top = 140
$AddBtn.Width = 30
$nul = $GroupBox.Controls.Add($AddBtn)

$RemoveBtn = $System.Button()
$RemoveBtn.Click = "RemoveFromMemberOf()"
$RemoveBtn.Left = 200
$RemoveBtn.Text = "<"
$RemoveBtn.Top = 170
$RemoveBtn.Width = 30
$nul = $GroupBox.Controls.Add($RemoveBtn)

$RemoveAllBtn = $System.Button()
$RemoveAllBtn.Click = "RemoveAllFromMemberOf()"
$RemoveAllBtn.Left = 200
$RemoveAllBtn.Text = "<<"
$RemoveAllBtn.Top = 200
$RemoveAllBtn.Width = 30
$nul = $GroupBox.Controls.Add($RemoveAllBtn)
;endregion
;endregion

;region Form Controls
$FullNameLbl = $System.Label()
$FullNameLbl.Height = 20
$FullNameLbl.Left = 10
$FullNameLbl.Text = "Full Name:"
$FullNameLbl.TextAlign = 64  ;MiddleRight
$FullNameLbl.Top = 10
$FullNameLbl.Width = 80
$nul = $Form.Controls.Add($FullNameLbl)

$FullNameTextBox = $System.TextBox()
$FullNameTextBox.Left = 100
$FullNameTextBox.Text = ""
$FullNameTextBox.Top = 10
$FullNameTextBox.Width = 310
$nul = $Form.Controls.Add($FullNameTextBox)

$EditUserBtn = $System.Button()
$EditUserBtn.Click = "EditUser()"
$EditUserBtn.Left = 450
$EditUserBtn.Text = "Edit User"
$EditUserBtn.Top = 10
$nul = $Form.Controls.Add($EditUserBtn)

$CancelBtn = $System.Button()
$CancelBtn.Click = "CancelEditUser()"
$CancelBtn.Enabled = 0
$CancelBtn.Left = 450
$CancelBtn.Text = "Cancel"
$CancelBtn.Top = 320
$nul = $Form.Controls.Add($CancelBtn)

$ApplyBtn = $System.Button()
$ApplyBtn.Click = "ApplyToUser()"
$ApplyBtn.Enabled = 0
$ApplyBtn.Left = 450
$ApplyBtn.Text = "Apply"
$ApplyBtn.Top = 350
$nul = $Form.Controls.Add($ApplyBtn)
;endregion

$Form.Show  ;Displays the Form

While $Form.Visible
   $Nul = Execute($Form.DoEvents())
Loop
Exit 0
;endregion

Function AdvanceProgressBar($Step,Optional $Text)
   
   $Count = $Count + $Step
   If $Count > 100
      $Count = 0
   EndIf
   $ProgressBar.Value = $Count
   $ProgressBar.Refresh
   $StatusBar.Text = $Text
   Sleep 0.03
   
EndFunction

Function ResetProgressBar()
   
   $Count = 0
   AdvanceProgressBar(0,"")
   $ProgressBar.Visible = 0
   
EndFunction

Function EditUser()
   $GroupBox.Enabled = -1
   $FullNameTextBox.Enabled = 0
   $EditUserBtn.Enabled = 0
   $CancelBtn.Enabled = -1
   $ApplyBtn.Enabled = -1
   If $AvailableGroupsListBox.Items.Count = 0
      GetOU(GetObject("LDAP://"+@domain+"/"+$defaultNamingContext))
   EndIf
   GetMemberOfGroups(GetUserDistinguishedName($FullNameTextBox.Text))
EndFunction

Function RefreshAvailableGroups()
   GetOU(GetObject("LDAP://"+@domain+"/"+$defaultNamingContext))
EndFunction

Function RefreshMemberOfGroups()
   GetMemberOfGroups(GetUserDistinguishedName($FullNameTextBox.Text))
EndFunction

Function CancelEditUser()
   $AvailableGroupsListBox.Items.Clear
   $MemberOfListBox.Items.Clear
   $GroupBox.Enabled = 0
   $FullNameTextBox.Enabled = -1
   $EditUserBtn.Enabled = -1
   $CancelBtn.Enabled = 0
   $ApplyBtn.Enabled = 0
   $FullNameTextBox.Text = ""
EndFunction

Function ApplyToUser()
   Dim $Index,$x
   
   $User = GetObject("LDAP://" + GetUserDistinguishedName($FullNameTextBox.Text))
   $UserName = Split($User.userPrincipalName,"@@")[0]
   
   $MemberOfArray = Split(MemberOfArray(),',')
   
   $ListItem = "No"
   For $Index = 0 to UBound($MemberOfArray)
      For $x = 0 to $MemberOfListBox.Items.Count
         If $MemberOfListBox.Items.Item($x) = $MemberOfArray[$Index]
            $ListItem = "Yes"
         EndIf
      Next
      If $ListItem = "No"
         $GroupName = $MemberOfArray[$Index]
         If Not($GroupName = '')
            RemoveUserFromGroup($UserName,$GroupName)
         EndIf
      EndIf
      $ListItem = "No"
   Next
   
   ReDim $Index,$x
   
   $MemberOfArray = Split(MemberOfArray(),',')
   
   $ListItem = "No"
   For $x = 0 to $MemberOfListBox.Items.Count
      For $Index = 0 to UBound($MemberOfArray)
         If $MemberOfArray[$Index] = $MemberOfListBox.Items.Item($x)
            $ListItem = "Yes"
         EndIf
      Next
      If $ListItem = "No"
         $GroupName = $MemberOfListBox.Items.Item($x)
         If Not($GroupName = '')
            AddUserToGroup($UserName,$GroupName)
         EndIf
      EndIf
      $ListItem = "No"
   Next
   
   GetMemberOfGroups(GetUserDistinguishedName($FullNameTextBox.Text))
   
EndFunction

Function GetOU($Domain)
   $ProgressBar.Visible = -1
   $AvailableGroupsListBox.Items.Clear
   For Each $OU1 in $Domain
      $Object = $OU1.Name
      If $OU1.Class = "group"
         $obj = SubStr($OU1.Name,4,Len($OU1.Name) - 3)
         $Item = $AvailableGroupsListBox.Items.Add($obj)
         AdvanceProgressBar(10,"Available Group: "+$Item)
      EndIf
         GetOU1($Object)
   Next
   ResetProgressBar()
   
EndFunction

Function GetOU1($Object)
   
   $Domain1 = GetObject("LDAP://"+$Object+","+$defaultNamingContext)
   
   For Each $OU2 in $Domain1
      $Object1 = $OU2.Name
      $Object2 = $Object1 + "," + $Object
      If $OU2.Class = "group"
         $obj = SubStr($OU2.Name,4,Len($OU2.Name) - 3)
         $Item = $AvailableGroupsListBox.Items.Add($obj)
         AdvanceProgressBar(10,"Available Group: "+$Item)
      EndIf
      GetOU1($Object2)
   Next

EndFunction

Function AddAllToMemberOf()

   $MemberOfListBox.Items.Clear
   For $x = 0 to $AvailableGroupsListBox.Items.Count
      If $AvailableGroupsListBox.Items.Item($x) <> ""
         $Item = $MemberOfListBox.Items.Add($AvailableGroupsListBox.Items.Item($x))
      EndIf
   Next
   
EndFunction

Function AddToMemberOf()
   
   $ListItem = "No"
   For $x = 0 to $MemberOfListBox.Items.Count
      If $MemberOfListBox.Items.Item($x) = $AvailableGroupsListBox.SelectedItem.Text
         $ListItem = "Yes"
      EndIf
   Next
   If $ListItem = "No"
      $Item = $MemberOfListBox.Items.Add($AvailableGroupsListBox.SelectedItem)
   EndIf
   
EndFunction

Function RemoveFromMemberOf()
   
   $MemberOfListBox.Items.Remove($MemberOfListBox.SelectedItem)

EndFunction

Function RemoveAllFromMemberOf()

   $MemberOfListBox.Items.Clear

EndFunction

Function GetUserDistinguishedName($FullName)
   
   $ADS_UF_ACCOUNTDISABLE = 2
   
   $ObjConnectION = CreateObject("ADODB.ConnectION")
   $ObjConnectION.Open("PROVIDER=ADSDSOOBJECT;")
   $ObjCOMMAND = CreateObject("ADODB.COMMAND")
   $ObjCOMMAND.ActiveConnectION = $ObjConnectION
   $ObjCOMMAND.COMMANDTEXT = "<GC://" + $defaultNamingContext + ">;(OBJECTCATEGORY=User);UserACCOUNTCONTROL,DistinguishedName;SUBTREE"
   $ObjRecordset = $ObjCOMMAND.Execute
   While Not $ObjRecordset.EOF
      $POSITIVE = InStr($ObjRecordset.Fields("DistinguishedName"),"CN=" + $FullName)
      If $POSITIVE <> 0
         $ObjUser = GetObject("LDAP://" + $ObjRecordset.Fields("DistinguishedName"))
         $ObjUser.GetInfo
         $GetUserDistinguishedName = $ObjUser.Get("DistinguishedName")
      EndIf
      $ObjRecordset.MoveNext
   Loop
   $ObjConnectION.Close
   
EndFunction

Function GetMemberOfGroups($UserDistinguishedName)
   
   $MemberOfListBox.Items.Clear
   $ProgressBar.Visible = -1
   $E_ADS_PROPERTY_NOT_FOUND = &8000500D
   $ObjUser = GetObject ("LDAP://" + $UserDistinguishedName)
   $IntPrimaryGroupID = $ObjUser.Get("PrimaryGroupID")
   $ArrMemberOf = $ObjUser.GetEX("MemberOf")
   If $ERR.Number = $E_ADS_PROPERTY_NOT_FOUND
      $= MessageBox("The MemberOf Attribute Is Not Set.","Error!",16)
   Else
      For Each $Group in $ArrMemberOf
         $x = InStr($Group,",")
         $GroupName = SubStr($Group,4,$x - 4)
         $Item = $MemberOfListBox.Items.Add($GroupName)
         AdvanceProgressBar(10,"Member Of: "+$Item)
      Next
   EndIf
   $ObjConnectION.Close
   ResetProgressBar()
   
EndFunction

Function MemberOfArray()
   $E_ADS_PROPERTY_NOT_FOUND = &8000500D
   $ObjUser = GetObject ("LDAP://" + GetUserDistinguishedName($FullNameTextBox.Text))
   $IntPrimaryGroupID = $ObjUser.Get("PrimaryGroupID")
   $ArrMemberOf = $ObjUser.GetEX("MemberOf")
   If $ERR.Number = $E_ADS_PROPERTY_NOT_FOUND
      $= MessageBox("The MemberOf Attribute Is Not Set.","Error!",16)
      Exit
   Else
      For Each $Group in $ArrMemberOf
         $x = InStr($Group,",")
         $GroupName = SubStr($Group,4,$x - 4)
         $MemberOfArray = $MemberOfArray+','+$GroupName
         $Index = $Index + 1
      Next
   EndIf
   $ObjConnectION.Close
EndFunction

Function AddUserToGroup($UserName,$GroupName)
   Dim $ObjUser,$ObjGroup
   
   $ObjUser = GetObject("WinNT://" + @Domain + "/" + $UserName)
   $ObjGroup = GetObject("WinNT://" + @Domain + "/" + $GroupName)
   $ObjGroup.Add($ObjUser.ADsPath)

EndFunction

Function RemoveUserFromGroup($UserName,$GroupName)
   Dim $ObjUser,$ObjGroup
   
   $ObjUser = GetObject("WinNT://" + @Domain + "/" + $UserName)
   $ObjGroup = GetObject("WinNT://" + @Domain + "/" + $GroupName)
   $ObjGroup.Remove($ObjUser.ADsPath)
   
EndFunction

_________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)
Back to top
View user's profile Send private message Send e-mail
danilda
KiXforms Follower
KiXforms Follower


Joined: 20 Jul 2007
Posts: 16

PostPosted: Tue Aug 21, 2007 8:53 pm    Post subject: Reply with quote

Thanks benny69. It is not quite what I am looking for now, but I know that I can put it to good use later on. Below is the code that I finally came up with.


Function fnLDAPQuery($What,Optional $From,Optional $Filter,Optional $OrderBy,Optional $Scope,
Optional $User,Optional $Pswd)

Dim $oCon,$oCMD,$oRS,$sQ,$aR,$C,$R

$sQ="<"+Iif($From="","LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext"),
$From)+">;"+$Filter+";"+Iif(VarType($What)>8192,Join($What,','),$What)+";"+
Iif($Scope<>"base" AND $Scope<>"onelevel","subtree",$Scope)

$oCon=CreateObject("ADODB.Connection")
$oCon.Provider="ADsDSOObject"
$oCon.Properties("Encrypt Password").Value=1
$oCon.Properties("ADSI Flag").Value=1
If $User AND $Pswd
$oCon.Properties("User ID").Value=$User
$oCon.Properties("Password").Value=$Pswd
EndIf
$oCon.Open("Active Directory Provider")

$oCMD=CreateObject("ADODB.Command")
$oCMD.ActiveConnection=$oCon
$oCMD.CommandText=$sQ
$oCMD.Properties("Page Size").Value=1000
$oCMD.Properties("Timeout").Value=30
$oCMD.Properties("Cache Results").Value=0

If InStr($OrderBy,"distinguishedName")
$oRS=CreateObject("ADODB.Recordset")
$oRS.CursorLocation=3
$oRS.Sort=$OrderBy
$oRS.Open($sQ,$oCon,0,1,1)
Else
If $OrderBy
$oCMD.Properties("Sort On").Value=$OrderBy
EndIf
$oRS=$oCMD.Execute
EndIf
If @ERROR Exit @ERROR EndIf
If $oRS.BOF AND $oRS.EOF Exit @ERROR EndIf

$aR = $oRS.GetRows()
Dim $aFR[Ubound($aR,2),Ubound($aR,1)]
For $R=0 to Ubound($aR,2)
For $C=0 to Ubound($aR,1)
$aFR[$R,$C]=$aR[$C,$R]
Next
Next

$fnLDAPQuery=$aFR
EndFunction


Break On
$System = CreateObject("Kixtart.System")

DrawForm()

While $Form.Visible
$=Execute($Form.DoEvents())
Loop

Exit 1

;KD START

Function DrawForm()
;************* Form **************
$Form = $System.Form()
;$Form.BackColor = 212,208,200
$Form.Height = 941
$Form.Left = -4
$Form.Text = "Create Network User Application"
$Form.Top = -118
$Form.Width = 1273
;**************************************

$GroupBox5 = $Form.Controls.GroupBox("Groups/Distribution Lists:", 0, 10, 1265, 605)

; == Return the Name, AdsPath and members of all groups =========
$aAttributes = "Name"
$sADsPath = "LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext")
$strFilter = "(&(objectClass=group))"
dim $h, $i
$h = 10
$i = 15
$aResults = fnLDAPQuery($aAttributes,$sADsPath,$strFilter,"name")
For $r = 0 to Ubound($aResults,1)
For $c = 0 to UBound($aresults,2)
if $i = 600
$h=$h+250
$i = 15
endif
$CheckBox1 = $GroupBox5.Controls.CheckBox($aresults[$r,$c], $h, $i, 250, 15)
$i=$i+15

Next
next

;KD END

$Form.Center
$Form.Show

EndFunction
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> Advanced Scripting 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