Search code examples
powershelltextboxeventhandler

learning powershell and trying to retrieve value from textbox


I'm new to this and I'm trying to build a Powershell GUI with a textbox field and use the text to query AD. Something is wrong with my code. The text is stored in variables $textbox_HelloWorld.Text and is called in the event handler $button_ClickMe.Add_Click. This does not return any results. If I replace the variable with a string then it does work. It sounds like a scope issue but I've tried using $script: and $global but neither work.

Any help appreciated.

# import AD module
import-module activedirectory

# Load required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")


# Drawing form and controls
$Form_HelloWorld = New-Object System.Windows.Forms.Form
    $Form_HelloWorld.Text = "Hello World"
    $Form_HelloWorld.Size = New-Object System.Drawing.Size(480,240)
    $Form_HelloWorld.FormBorderStyle = "FixedDialog"
    $Form_HelloWorld.TopMost = $true
    $Form_HelloWorld.MaximizeBox = $false
    $Form_HelloWorld.MinimizeBox = $false
    $Form_HelloWorld.ControlBox = $true
    $Form_HelloWorld.StartPosition = "CenterScreen"
    $Form_HelloWorld.Font = "Segoe UI"


# adding firstName TextBox to my form
$textbox_HelloWorld = New-Object System.Windows.Forms.TextBox
    $textbox_HelloWorld.Location = New-Object System.Drawing.Size(8,4)
    $textbox_HelloWorld.Size = New-Object System.Drawing.Size(120,32)
    $textbox_HelloWorld.TextAlign = "MiddleCenter"
    $global:textbox_HelloWorld.Text = "Enter first name!"
    $Form_HelloWorld.Controls.Add($textbox_HelloWorld)
    $firstnme=$textbox_HelloWorld.Text


# adding firstName label to my form
$label_HelloWorld = New-Object System.Windows.Forms.Label
    $label_HelloWorld.Location = New-Object System.Drawing.Size(128,4)
    $label_HelloWorld.Size = New-Object System.Drawing.Size(80,16)
    $label_HelloWorld.TextAlign = "MiddleCenter"
    $label_HelloWorld.Text = "First Name"
    $Form_HelloWorld.Controls.Add($label_HelloWorld)

# adding lastName TextBox to my form
$Secondtextbox_HelloWorld = New-Object System.Windows.Forms.TextBox
    $Secondtextbox_HelloWorld.Location = New-Object System.Drawing.Size(8,38)
    $Secondtextbox_HelloWorld.Size = New-Object System.Drawing.Size(120,32)
    $Secondtextbox_HelloWorld.TextAlign = "MiddleCenter"
    $global:Secondtextbox_HelloWorld.Text = "Enter last name!"
    $Form_HelloWorld.Controls.Add($Secondtextbox_HelloWorld)
    $lastnme=$Secondtextbox_HelloWorld.Text


# adding lastName label to my form
$Secondlabel_HelloWorld = New-Object System.Windows.Forms.Label
    $Secondlabel_HelloWorld.Location = New-Object System.Drawing.Size(128,38)
    $Secondlabel_HelloWorld.Size = New-Object System.Drawing.Size(80,16)
    $Secondlabel_HelloWorld.TextAlign = "MiddleCenter"
    $Secondlabel_HelloWorld.Text = "Last Name"
    $Form_HelloWorld.Controls.Add($Secondlabel_HelloWorld)

# add a button
$button_ClickMe = New-Object System.Windows.Forms.Button
    $button_ClickMe.Location = New-Object System.Drawing.Size(8,80)
    $button_ClickMe.Size = New-Object System.Drawing.Size(240,32)
    $button_ClickMe.TextAlign = "MiddleCenter"
    $button_ClickMe.Text = "Click Me!"
    $button_ClickMe.Add_Click({
        $button_ClickMe.Text = "Submitted"
        $nme = Get-ADUser -Filter "GivenName -like '$global:textbox_HelloWorld.Text' -and Surname -like '$global:Secondtextbox_HelloWorld.Text*'" | Select-Object -ExpandProperty SamAccountName | out-string
        [System.Windows.Forms.MessageBox]::Show($nme , "My Dialog Box")
    })

    $Form_HelloWorld.Controls.Add($button_ClickMe)

# show form
$Form_HelloWorld.Add_Shown({$Form_HelloWorld.Activate()})
[void] $Form_HelloWorld.ShowDialog()

Solution

  • Since you are asking for the variable in quotes, it is seeing "$textbox_helloworld" as your variable and ".text" as a string. You need to run the command as a SubExpression inside the quotes via $()

    e.g. "$city.state" would return "Los Angeles.state" where "$($city.state)" would return "California"

    Also global shouldn't be needed. This is because "Global" and "script" are for finding variables outside of the current environment. Since all of your code is in the same environment, and not in a child environment (e.g. Function or Scriptblock) you don't need to call a parent environment variable.

    Here is what you want to use.

            $nme = Get-ADUser -Filter "GivenName -like '$($textbox_HelloWorld.Text)' -and Surname -like '$($Secondtextbox_HelloWorld.Text)*'" | Select-Object -ExpandProperty SamAccountName | out-string
    

    A final note is that "middleCenter" is not a viable property on Textbox's, at least not on my machine. I would remove your textbox textaligns for more compatibility.