# encoding: ascii
# api: powershell
# title: Select-UserGroup
# description: A script for ShowUI 1.0 — using Quest’s active directory cmdlets to retrieve user information and allow you to select one of the groups they’re a member of.  Could be extended to populate a bunch of form fields from active directory and even allow updating them…
# version: 0.1
# author: Joel Bennett
# license: CC0
# x-poshcode-id: 2737
# x-archived: 2012-01-03T12:18:12
# x-published: 2012-06-17T20:07:00
#
#
New-Grid -ControlName SelectUserGroups -Columns Auto,* -Rows 4 {
    $GetGroups = { 
        $user = Get-QADUuser $this.Text -SizeLimit 1
        if($User.LogonName -eq $this.Text -or $User.Email -eq $this.Text) {
            $this.Foreground = "Black" 
            $Group.ItemsSource = Get-QADGroup -ContainsMember $user
            $UserName.Text = $user.LogonName
            $EmailAddress.Text = $user.Email
        } else {
            $this.Foreground = "Red" 
            $Group.ItemsSource = @()         
        }
    }
    
    New-Label "Name"
    New-Textbox -name UserName -minwidth 100 -Column 1 -On_LostFocus $GetGroups
    
    New-Label "Email" -Row 1
    New-Textbox -name EmailAddress -minwidth 100  -Column 1 -Row 1  -On_LostFocus $GetGroups
    
    New-Label "Group" -Row 2
    New-Listbox -name Group -Column 1 -Row 2
    
    New-Button "OK" -Row 3 -Column 1 -On_Click { Get-ParentControl | Set-UIValue -Passthru | Close-Control }
} -Show
