The intent of this form is to retreive first a username then use that username to retreive an email address. it does get the address but it also retreives other characters as well. textbox3 ends up reading @{EmailAddress=first.last@domain.com}. I have tried trimming characters but that did not work.
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);'
[Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0)
<#
.NAME
Template
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,400)
$Form.text = "Form"
$Form.TopMost = $false
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.width = 100
$TextBox1.height = 20
$TextBox1.location = New-Object System.Drawing.Point(61,52)
$TextBox1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline = $false
$TextBox2.width = 100
$TextBox2.height = 20
$TextBox2.location = New-Object System.Drawing.Point(219,52)
$TextBox2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TextBox3 = New-Object system.Windows.Forms.TextBox
$TextBox3.multiline = $false
$TextBox3.width = 100
$TextBox3.height = 20
$TextBox3.location = New-Object System.Drawing.Point(61,130)
$TextBox3.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "button"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(228,127)
$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button1.Add_Click({getacctname})
$Form.controls.AddRange(@($TextBox1,$TextBox2,$TextBox3,$Button1))
#region Logic
function getacctname {
$fname = $TextBox1.Text
$lname = $TextBox2.Text
$User.Text = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" |
Select-Object -ExpandProperty 'SamAccountName' |
Out-Gridview -Title 'Windows Logon' -PassThru
$TextBox3.Text = Get-ADUser -identity $User.text -Properties * | select EmailAddress
}
#endregion
[void]$Form.ShowDialog()