#cls #Generated Form Function function search_login { ######################################################################## # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.7.0 # Generated On: 11/27/2009 3:59 PM # Generated By: ¸ø @ ######################################################################## #region Import the Assemblies [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null #endregion #---------------------------------------------- #Generated Event Script Blocks #---------------------------------------------- #Provide Custom Code for events specified in PrimalForms. $add_OnClick={#write-host $listBox1.SelectedItem addlogin $form1.Close() } $VAL_OnClick= {recherche} $cancel_OnClick={$form1.Close()} $OnLoadForm_StateCorrection={$form1.WindowState = $InitialFormWindowState} #---------------------------------------------- #region Generated Form Code $form1 = New-Object System.Windows.Forms.Form $form1.Text = "Search_login" $form1.Name = "form1" $form1.ClientSize = New-Object System.Drawing.Size(732,273) #Button ADD $add = New-Object System.Windows.Forms.Button $add.TabIndex = 3 $add.Name = "add" $add.Size = New-Object System.Drawing.Size(75,23) $add.UseVisualStyleBackColor = $True $add.Text = "addmember" $add.Location = New-Object System.Drawing.Point(645,72) $add.add_Click($add_OnClick) $form1.Controls.Add($add) #Button cancel $cancel = New-Object System.Windows.Forms.Button $cancel.TabIndex = 2 $cancel.Name = "Cancel" $cancel.Size = New-Object System.Drawing.Size(75,23) $cancel.UseVisualStyleBackColor = $True $cancel.Text = "Cancel" $cancel.Location = New-Object System.Drawing.Point(645,42) $cancel.add_Click($cancel_OnClick) $form1.Controls.Add($cancel) #Button VAL $VAL = New-Object System.Windows.Forms.Button $VAL.TabIndex = 1 $VAL.Name = "Val login" $VAL.Size = New-Object System.Drawing.Size(75,23) $VAL.UseVisualStyleBackColor = $True $VAL.Text = "Val login" $VAL.Location = New-Object System.Drawing.Point(645,12) $VAL.add_Click($VAL_OnClick) $form1.Controls.Add($VAL) #REM Listbox return results $listBox1 = New-Object System.Windows.Forms.ListBox $listBox1.FormattingEnabled = $True $listBox1.Location = New-Object System.Drawing.Point(12,42) $listBox1.Size = New-Object System.Drawing.Size(585,221) $listBox1.ScrollAlwaysVisible = $True $listBox1.HorizontalScrollbar = $True $listBox1.Name = "listBox1" $listBox1.Sorted = $True $listBox1.TabIndex = 0 $form1.Controls.Add($listBox1) #REM Login $login = New-Object System.Windows.Forms.TextBox $login.Location = New-Object System.Drawing.Point(72,12) $login.Size = New-Object System.Drawing.Size(525,70) $login.Name = "login" $login.text = "" $login.TabIndex = 3 $form1.Controls.Add($login) #REM Write login $wlogin = New-Object System.Windows.Forms.Label $wlogin.TabIndex = 4 $wlogin.Size = New-Object System.Drawing.Size(115,23) $wlogin.Text = "Write login" $wlogin.Location = New-Object System.Drawing.Point(12,15) $wlogin.Name = "write login" $form1.Controls.Add($wlogin) #endregion Generated Form Code #---------------------------------------------- #Save the initial state of the form $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState $InitialFormWindowState = $form1.WindowState #Init the OnLoad event to correct the initial state of the form $form1.add_Load($OnLoadForm_StateCorrection) #Show the Form $form1.ShowDialog()| Out-Null } #End Function #---------------------------------------------- function recherche{ #REM Search & ListBox Populate $domain = [ADSI]"" #$request = read-host loginuser: $request = $login.text $search = New-Object DirectoryServices.DirectorySearcher($domain) $search.filter = "(&(|(objectclass=group)(objectClass=user))(sAMAccountName=$request))" $Search.PageSize = 1000 $ldap = $search.findall() if ($ldap.count -gt 0) { $test = 0 foreach ($i in $ldap){ $test= ($i.GetDirectoryEntry().path +";" + $i.GetDirectoryEntry().distinguishedname +";" + $i.GetDirectoryEntry().samaccountname) | %{ $_.Split(';')[2];} $listBox1.Items.Add($test)|Out-Null $test = $test+ 1 } } } #---------------------------------------------- function addlogin{ #REM DN extraction from the selectditem and add to group $domain = [ADSI]"" $request = $listBox1.SelectedItem #write-host "request" $request $search = New-Object DirectoryServices.DirectorySearcher($domain) $search.filter = "(&(|(objectclass=group)(objectClass=user))(sAMAccountName=$request))" $ldap = $search.findall() foreach ($i in $ldap){ $test2= $i.GetDirectoryEntry().distinguishedname write-host "test2" $test2 [ADSI]'' $ok= $test2 write-host $ok $group = [ADSI]"LDAP://CN=marketing,OU=Marketing,OU=Idera,dc=echo,dc=bravo" $group.add($ok) $group.setinfo() } } #---------------------------------------------- #REM ADD USER TO GROUP #Call the Function # trap { $error[0].Exception | get-member } search_login search_login