How can I get rid of extra characters in TextBox?

Viewed 30

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()
1 Answers

There are two issues with your code:

  1. As explained in a comment, you need to get the value from the EmailAddress property of your object, otherwise, because the .Text property value can only be a string, what you will see as a result is a string representation of a PSCustomObject.

  2. The assignment of $User.Text is invalid and will produce an error unless you're not showing us your actual code. The $User variable not defined hence cannot have a .Text property. Trying to assign anything to it will produce an error such as:

The property 'Text' cannot be found on this object. Verify that the property exists and can be set.

How should your function to solve both issues:

function getacctname {
    $fname, $lname = $TextBox1.Text.Trim(), $TextBox2.Text.Trim()
    $TextBox3.Text = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" -Properties mail |
        Select-Object SamAccountName, mail |
        Out-Gridview -Title 'Windows Logon' -PassThru |
        Select-Object -ExpandProperty mail
}

As aside, I would recommend you to make $TextBox3 a ReadOnly TextBox:

$TextBox3.ReadOnly = $true
Related