locked
GUI to manage groups domain A to add the users from domain B RRS feed

  • Question

  • Hello,

    I run the script from computer in the domain A.

    I'm creating GUI to manage some AD Groups from domain A to add user from domain B and I have problem to list groups of members from domain B; someone could help me please ?

    Add-Type -AssemblyName System.Windows.Forms
    Import-Module ActiveDirectory
    
    
    ##function to populate the current group list box with the users current security group membership domain A
    function cgroups { process {
          $group10 = Get-ADPrincipalGroupMembership -Identity $id2
          foreach($groups in $group10){
                     $Currentgroups.Items.Add($groups.Name);
                 }
           }
    }
    
    ##function to populate the current group list box with the users current security group membership domain B
    function cgroups1 { process {
          $group11 = Get-ADUser -server "server.domainB.local" -Identity $id3 -properties Memberof | select-object Memberof
          foreach($groups in $group11){
                     $Currentgroups.Items.Add($groups.Name);
                 }
           }
    }
    
    # function to populate the allgroups listbox with all current security groups located in the domain AD
    function sgroups { process{
    
         $group1 = Get-ADGroup -Filter {name -like "*application*"}
         foreach($group in $group1){
                     $allgroups.Items.Add($group.Name);
                  }
          }
    }
    
    
    
    function clear {  process{
    
               $Currentgroups.Items.Clear();
               $userid.Text = "";
         }
    
    }
    
    function clear {  process{
    
               $Currentgroups.Items.Clear();
               $useridDomainB.Text = "";
         }
    
    }
    
    #convert system drawing to a displayable image and displays it in the image box. 
    function success { process {
      $file1 = (Get-Item 'C:\AD\check.jpg')
      $img1 = [System.Drawing.Image]::Fromfile($file1);
      $pbox2.Image = $img1;
    
    }
    }
    
    #convert system drawing to a displayable image and displays it in the image box. 
    function fail { process {
      $file2 = (Get-Item 'C:\AD\redx.jpg')
      $img2 = [System.Drawing.Image]::Fromfile($file2);
      $pbox2.Image = $img2;
    
    
    }
    }
    
    
    #clears specified fields and selected items
    function cclear { process{
    
               Start-Sleep -Seconds 3
               $pbox2.Image = $null;
               $complete1.text = "";
               $allgroups.ClearSelected()
    }
    }
    
    $Form = New-Object system.Windows.Forms.Form
    $Form.Text = "Application AD Groups"
    $Image = [system.drawing.image]::FromFile("C:\AD\company.jpg")
    $Form.BackgroundImage = $Image
    $Form.BackgroundImageLayout = "Center"
    $form.BackColor =”Lightskyblue”
    $Form.Width = 700
    $Form.Height = 417
    $Form.StartPosition = "CenterScreen"
    $Form.FormBorderStyle = 'Fixed3D'
    $Form.MaximizeBox = $false
     
    
    $Currentgroups = New-Object system.windows.Forms.ListBox
    $Currentgroups.Text = "listBox"
    $Currentgroups.Width = 249
    $Currentgroups.Height = 201
    $Currentgroups.location = new-object system.drawing.point(17,94)
    $Form.controls.Add($Currentgroups)
    $Currentgroups.SelectionMode = "MultiExtended"
    
    
    $allgroups = New-Object system.windows.Forms.ListBox
    $allgroups.Text = "listBox"
    $allgroups.Width = 249
    $allgroups.Height = 201
    $allgroups.location = new-object system.drawing.point(420,94)
    $Form.controls.Add($allgroups)
    $allgroups.SelectionMode = "MultiExtended"
    
    
    $userID = New-Object system.windows.Forms.TextBox
    $userID.Width = 100
    $userID.Height = 10
    $userID.location = new-object system.drawing.point(96,12)
    $userID.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($userID)
    
    
    $label8 = New-Object system.windows.Forms.Label
    $label8.Text = "UserID Domain A:"
    $label8.AutoSize = $true
    $label8.Width = 25
    $label8.Height = 10
    $label8.location = new-object system.drawing.point(16,15)
    $label8.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($label8)
    
    $userIDDomainB = New-Object system.windows.Forms.TextBox
    $userIDDomainB.Width = 100
    $userIDDomainB.Height = 10
    $userIDDomainB.location = new-object system.drawing.point(96,39)
    $userIDDomainB2.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($userIDDomainB)
    
    
    
    $label9 = New-Object system.windows.Forms.Label
    $label9.Text = "UserID DomainB:"
    $label9.AutoSize = $true
    $label9.Width = 25
    $label9.Height = 10
    $label9.location = new-object system.drawing.point(18,42)
    $label9.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($label9)
    
    #finds user specified in text box and returns their group membership to the Currentgroups listbox Domain A. 
    $button10 = New-Object system.windows.Forms.Button
    $button10.Text = "Find"
    $button10.BackColor = "White"
    $button10.Width = 101
    $button10.Height = 20
    $button10.Add_MouseClick({
                   $script:id2 = $userID.Text;
                   $getu = Get-ADUser -Identity $id2
                   
                  $Currentgroups.Items.Clear();
                  $pbox2.Image = $null;
                  $complete1.Text = "";
                  if($getu -eq $null){
                     fail;
                     $complete1.Text = "User Not Found";
                     $complete1.location = new-object system.drawing.point(450,24)
                    $complete1.Font = "Calibri,11,style=bold"
                  }else {
                     cgroups;
                     success;
                     $complete1.Text = "User Found";
                     $complete1.location = new-object system.drawing.point(470,24)
                    $complete1.Font = "Calibri,11,style=bold"
                     cclear;
                  
                  }       
          
    
    })
    $button10.location = new-object system.drawing.point(213,14)
    $button10.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($button10)
    
    
    #finds user specified in text box and returns their group membership to the Currentgroups listbox DomainB. 
    $button11 = New-Object system.windows.Forms.Button
    $button11.Text = "Find"
    $button11.BackColor = "White"
    $button11.Width = 101
    $button11.Height = 20
    $button11.Add_MouseClick({
                   $script:id3 = $userIDDomainB.Text;
                   $getu2 = Get-ADUser  -server server.domainB.local -Identity $id3
                   
                  $Currentgroups.Items.Clear();
                  $pbox2.Image = $null;
                  $complete1.Text = "";
                  if($getu2 -eq $null){
                     fail;
                     $complete1.Text = "User Not Found";
                     $complete1.location = new-object system.drawing.point(450,24)
                    $complete1.Font = "Calibri,11,style=bold"
                  }else {
                     cgroups1;
                     success;
                     $complete1.Text = "User Found";
                     $complete1.location = new-object system.drawing.point(470,24)
                    $complete1.Font = "Calibri,11,style=bold"
                     cclear;
                  
                  }       
          
    
    })
    $button11.location = new-object system.drawing.point(213,41)
    $button11.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($button11)
    
    #adds user to group selected in either the distribution groups listbox or the security groups listbox
    $button12 = New-Object system.windows.Forms.Button
    $button12.Text = "Add"
    $button12.BackColor = "White"
    $button12.Width = 250
    $button12.Height = 34
    $button12.Add_MouseClick({
    
                 $sgroup = $allgroups.SelectedItems;
                  Add-ADPrincipalGroupMembership -Identity $id2,$id3 -MemberOf $sgroup;
                  success;
                  $complete1.Text = "Added to group";
                  $complete1.location = new-object system.drawing.point(430,24)
                  $complete1.Font = "Calibri,11,style=bold"
                  Start-Sleep -Seconds 1;
                  $Currentgroups.Items.Clear();
                  cgroups;
                  cclear;
                 
    })
    $button12.location = new-object system.drawing.point(420,334)
    $button12.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($button12)
    
    
    
    #Button exit
    $button18 = New-Object system.windows.Forms.Button
    $button18.Text = "Exit"
    $button18.BackColor = "White"
    $button18.Width = 150
    $button18.Height = 34
    
    $button18.Add_Click({
        $form.Close()
                 
    })
    
    $button18.location = new-object system.drawing.point(267,295)
    $button18.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($button18)
    
    #removes user from selected group in "Current Groups" list box and refreshes the results of that box. 
    $button15 = New-Object system.windows.Forms.Button
    $button15.Text = "Remove"
    $button15.BackColor = "White"
    $button15.Width = 249
    $button15.Height = 30
    $button15.Add_MouseClick({
               $group = $Currentgroups.SelectedItem;
    
               Remove-ADGroupMember -Identity $group -Member $id2 -Confirm: $false;
               Success;
               $complete1.Text = "Removed to group"
               $complete1.location = new-object system.drawing.point(420,24)
               $complete1.Font = "Calibri,11,style=bold"
               Start-Sleep -Seconds 1;
               $Currentgroups.Items.Clear();
               cgroups;
               cclear;
               
    })
    $button15.location = new-object system.drawing.point(17,335)
    $button15.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($button15)
    
    $pbox2 = New-Object system.windows.Forms.PictureBox
    $pbox2.Width = 98
    $pbox2.Height = 78
    $pbox2.location = new-object system.drawing.point(552,4)
    $Form.controls.Add($pbox2)
    
    $complete1 = New-Object system.windows.Forms.Label
    $complete1.AutoSize = $true
    $complete1.Width = 25
    $complete1.Height = 10
    $complete1.location = new-object system.drawing.point(350,7)
    $complete1.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($complete1)
    
    $label23 = New-Object system.windows.Forms.Label
    $label23.Text = "Current Groups"
    $label23.AutoSize = $true
    $label23.Width = 25
    $label23.Height = 10
    $label23.location = new-object system.drawing.point(18,74)
    $label23.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($label23)
    
    $label25 = New-Object system.windows.Forms.Label
    $label25.Text = "Security groups"
    $label25.AutoSize = $true
    $label25.Width = 25
    $label25.Height = 10
    $label25.location = new-object system.drawing.point(420,74)
    $label25.Font = "Microsoft Sans Serif,10"
    $Form.controls.Add($label25)
    
    
    
    $Icon = New-Object system.drawing.icon ("C:\AD\company.ico")
    $Form.Icon = $Icon 
    
    
    sgroups
    
    [void]$Form.ShowDialog()
    $Form.Dispose()

     

    • Moved by Bill_Stewart Wednesday, November 29, 2017 6:49 PM This is not "fix/debug/rewrite my script for me" forum
    Monday, October 23, 2017 7:57 AM

All replies

  • Sorry but there is not enough information to help you and we cannot debug 100+ lines of code for you.  Post mt he lines of code that are at issues and any errors.


    \_(ツ)_/

    Monday, October 23, 2017 8:09 AM
  • A quick look at your code shows many design and coding mistakes.  I suggest that you create independent functions fo all AD access and test he functions independently until they are working correctly then just call them from the form with all necessary arguments.

    Note also that you do not need to use "Drawing Points"  just use the positions as a string>

    $<object>.Location = '10,20'
    $<object>.Size = '15,50'

    Give real names to all objects so you can identify what they are.  The "Name" property exists for all controls.  Use it and name the variables with the same name.

    $buttonFindUser = New-Object System.Windows.Forms.Button
    $buttonFindUser.Name = 'buttonFindUser'

    If you are using WMF 5.x then you can doo this which greatly simplifies the form creation.

    Example:

    #  the "using" statements must be the first lines in the file
    using assembly System.Windows.Forms
    using namespace System.Windows.Forms
    
    $frmAppADGroups = [Form]@{
    Name = 'AppADGroups' Text = 'Application AD Groups' BackgroundImage = [system.drawing.image]::FromFile('C:\AD\company.jpg') BackgroundImageLayout = 'Center' BackColor = 'Lightskyblue' Size = '700,417' StartPosition = 'CenterScreen' FormBorderStyle = 'Fixed3D' MaximizeBox = $false } $btnFindUserA = [button]@{ Name = 'btnFindUserA' Text = 'Find User A' BackColor = 'White' Size = '101,20' Location = '10,20' } $frmAppADGroups.controls.Add($btnFindUserA)
    You can also "Base64" encode the images and store the Base64 string in the PS1 file.


    \_(ツ)_/





    • Edited by jrv Monday, October 23, 2017 8:58 AM
    Monday, October 23, 2017 8:40 AM
  • I should also note that this line is pointless:

    $Form.Dispose()

    If you don't remove all of the controls the form will never be destroyed.  Pace the form in a function and when the "ShowDialog()" exits the variables will all go out of scope and all will be "disposed" automatically.


    \_(ツ)_/

    Monday, October 23, 2017 9:00 AM
  • If you build a button like this it will close the form without having to add code:

    #Button exit
    $frmAppADGroups.Controls.Add(
    		[Button]@{
    		Name = 'btnExit'
    		Text = 'Exit'
    		DialogResult = 'OK'
    		BackColor = "White"
    		Size = '150, 35'
    		Location = '267, 295'
    		Font = 'Microsoft Sans Serif,10'
    	}
    )
    
    Notice how to skip creating a lot of unneeded variables.


    \_(ツ)_/

    Monday, October 23, 2017 9:12 AM