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.