如何清除PowerShell WinForms TextBox中的多余字符?
问题解决:PowerShell WinForms中TextBox显示多余对象格式字符
你的问题出在获取邮箱地址的代码上:Get-ADUser -identity $User.text -Properties * | select EmailAddress 返回的是一个自定义对象,PowerShell把它转成字符串后就会显示成@{EmailAddress=first.last@domain.com}这种格式,而不是纯邮箱字符串。
另外原代码里还有个小问题:$User.Text这个变量根本没定义,应该直接用变量存储从Out-GridView选择的用户名。
修正方案
- 提取纯邮箱字符串:不要用
select EmailAddress,改用以下两种方式之一:- 用
Select-Object -ExpandProperty EmailAddress直接提取属性值 - 访问对象的
.EmailAddress属性
- 用
- 修正未定义的变量问题:把Out-GridView的结果存到变量里,再传给Get-ADUser
修正后的完整代码
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) 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 = "AD用户邮箱查询工具" $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) $TextBox1.PlaceholderText = "输入名" $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) $TextBox2.PlaceholderText = "输入姓" $TextBox3 = New-Object system.Windows.Forms.TextBox $TextBox3.multiline = $false $TextBox3.width = 250 $TextBox3.height = 20 $TextBox3.location = New-Object System.Drawing.Point(61,130) $TextBox3.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $TextBox3.ReadOnly = $true $Button1 = New-Object system.Windows.Forms.Button $Button1.text = "查询邮箱" $Button1.width = 80 $Button1.height = 30 $Button1.location = New-Object System.Drawing.Point(320,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.Trim() $lname = $TextBox2.Text.Trim() # 先获取用户名并选择 $selectedUser = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" | Select-Object -ExpandProperty 'SamAccountName' | Out-Gridview -Title '选择Windows登录账号' -PassThru if ($selectedUser) { # 方法1:用-ExpandProperty提取邮箱字符串 # $TextBox3.Text = Get-ADUser -Identity $selectedUser -Properties EmailAddress | Select-Object -ExpandProperty EmailAddress # 方法2:直接访问对象的EmailAddress属性(更高效) $userObj = Get-ADUser -Identity $selectedUser -Properties EmailAddress $TextBox3.Text = $userObj.EmailAddress } else { $TextBox3.Text = "未选择用户或无匹配结果" } } #endregion [void]$Form.ShowDialog()
额外优化说明
- 给TextBox加了占位提示,更友好
- 把TextBox3设为只读,防止误修改
- 增加了空选择的判断,避免报错
- 获取AD用户时只加载
EmailAddress属性,比Properties *更高效
内容的提问来源于stack exchange,提问作者PCMedicJAX
相关产品推荐
相关产品推荐

