1

I am trying to build a GUI that will eventually allow our second line team to easily apply look up AD accounts. I have got so far but I cannot get PowerShell to look up the value entered into a text box to then evaluate if the user exists in AD.

Here is the script:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles() 

$Form               = New-Object System.Windows.Forms.Form
$Form.ClientSize    = '400,400'
$Form.Text          = "Add DXE Mailbox Permissions"
$Form.TopMost       = $false

$Label1             = New-Object System.Windows.Forms.Label
$Label1.Text        = "Username"
$Label1.AutoSize    = $true
$Label1.Width       = 25
$Label1.Height      = 10
$Label1.Location    = New-Object System.Drawing.Point(15, 145)
$Label1.Font        = 'Microsoft Sans Serif,10'

$TextBox1           = New-Object System.Windows.Forms.TextBox
$TextBox1.Multiline = $false
$TextBox1.Width     = 168
$TextBox1.Height    = 20
$TextBox1.Location  = New-Object System.Drawing.Point(15, 165)
$TextBox1.Font      = 'Microsoft Sans Serif,10'

$Button1            = New-Object System.Windows.Forms.Button
$Button1.Text       = "Check Username"
$Button1.Width      = 120
$Button1.Height     = 30
$Button1.Location   = New-Object System.Drawing.Point(199, 162)
$Button1.Font       = 'Microsoft Sans Serif,10'
$Button1.Add_Click($Button1_Click)

$Form.Controls.AddRange(@($Label1, $TextBox1, $Button1))

$Button1_Click = {
    $username = $Label1.Text
    $Checkuser = Get-ADUser -Identity $username
    if ($Checkuser -eq $null) {
        $Button1.Text = "Can't Find User"
        $button1.ForeColor = "Red"
    } elseif ($Checkuser -ne $null) {
        $Button1.Text = "Found User"
    }
}

[void]$Form.ShowDialog()

I believe the problem I am having is to do with the line $username = $Label1.Text. I am not sure if $Label1.Text should be assigned to a varible, and if so how would I make PowerShell retrive the text that has been entered? I had a quick look around and I am hoping that there is a way to do this without opening and closing another window.

2 Answers 2

1

You are referring to a wrong object here:

$username = $Label1.text

That's, em, the label. Of course you should get the value from the TextBox:

$username = $TextBox1.text
Sign up to request clarification or add additional context in comments.

1 Comment

ah, I see my mistake now. Thank you for your help.
1

You need to assign the $Button1_Click action after you've defined it - otherwise you're just assigning $null to the Click event:

<# define controls here ... #>
$Form.controls.AddRange(@($Label1,$TextBox1,$Button1))

$Button1_Click = {
  $username = $Label1.text
  $Checkuser = Get-ADUser -Identity $username
  If($Checkuser -eq $null){
    $Button1.Text = "Can't Find User"
    $button1.ForeColor = "Red"
  }
  Elseif($Checkuser -ne $null){
    $Button1.Text = "Found User"
  }
}

$Button1.Add_click($Button1_Click)

[void]$Form.ShowDialog()

If you want to grab the username from the text box, change $username = $Label1.text to $username = $TextBox1.Text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.