如何用PowerShell在AD描述字段搜索姓名并集成到GUI?
AD计算机描述搜索与GUI脚本集成需求
环境与格式说明
Active Directory中计算机对象的描述字段遵循固定格式:3字符站点代码 - 真实姓名 - 设备型号 - 序列号 - 财务状态 - 保修到期日期
示例:ABC - John Doe - 840 G6 - abc123xyz - (L) - 01/01/2024
初始搜索问题
尝试通过以下PowerShell命令搜索描述字段中包含指定真实姓名(如John Doe)的计算机,命令无报错但未返回任何结果:
(Get-ADComputer -SearchBase "OU=xxxx,OU=yyyy,OU=zzzz,DC=123,DC=456" -Filter {Description -Like "Doe"} -Properties Description).Description | Out-GridView
GUI脚本集成需求
后续找到可正常运行的CLI搜索方法,但需要将该功能集成到Windows Forms GUI脚本中。需实现两个核心目标:
- 将搜索到的计算机名称填入结果文本框
- 添加禁用计算机的功能按钮
优化后的GUI脚本代码
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);' [void][Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0) <# .NAME 离职人员设备管理工具 #> 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.TopMost = $false $Lbl_Leaver = New-Object system.Windows.Forms.Label $Lbl_Leaver.Text = "离职人员姓名" $Lbl_Leaver.AutoSize = $true $Lbl_Leaver.location = New-Object System.Drawing.Point(11,8) $Lbl_Leaver.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Tbx_Leaver = New-Object system.Windows.Forms.TextBox $Tbx_Leaver.Text = "" $Tbx_Leaver.multiline = $false $Tbx_Leaver.width = 200 $Tbx_Leaver.height = 20 $Tbx_Leaver.location = New-Object System.Drawing.Point(15,43) $Tbx_Leaver.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Btn_Search = New-Object system.Windows.Forms.Button $Btn_Search.Text = "搜索设备" $Btn_Search.width = 80 $Btn_Search.height = 30 $Btn_Search.location = New-Object System.Drawing.Point(28,88) $Btn_Search.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Btn_Search.Add_Click({ Find_Leaver_Devices }) $Btn_Disable = New-Object system.Windows.Forms.Button $Btn_Disable.Text = "禁用计算机" $Btn_Disable.width = 100 $Btn_Disable.height = 30 $Btn_Disable.location = New-Object System.Drawing.Point(120,88) $Btn_Disable.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Btn_Disable.Enabled = $false $Btn_Disable.Add_Click({ Disable_Target_Computer }) $Tbx_Results = New-Object system.Windows.Forms.TextBox $Tbx_Results.Text = "" $Tbx_Results.multiline = $false $Tbx_Results.width = 250 $Tbx_Results.height = 20 $Tbx_Results.location = New-Object System.Drawing.Point(13,192) $Tbx_Results.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) $Form.controls.AddRange(@($Lbl_Leaver,$Tbx_Leaver,$Btn_Search,$Btn_Disable,$Tbx_Results)) # 全局变量存储选中的计算机对象 $global:selectedComputer = $null Function Find_Leaver_Devices { $LeaverName = $Tbx_Leaver.Text.Trim() if (-not $LeaverName) { [System.Windows.Forms.MessageBox]::Show("请输入离职人员姓名","提示") return } $searchPattern = "*$LeaverName*" $foundDevices = Get-ADComputer -SearchBase "OU=123,OU=456,OU=abc,DC=def,DC=ghi" -Filter {Description -Like $searchPattern} -Properties Description | Select-Object Name, Description if ($foundDevices) { $global:selectedComputer = $foundDevices | Out-GridView -Title "选择目标设备" -Passthru if ($global:selectedComputer) { $Tbx_Results.Text = $global:selectedComputer.Name $Btn_Disable.Enabled = $true } else { $Btn_Disable.Enabled = $false } } else { [System.Windows.Forms.MessageBox]::Show("未找到匹配的设备","提示") $Btn_Disable.Enabled = $false } } Function Disable_Target_Computer { if ($global:selectedComputer) { $confirm = [System.Windows.Forms.MessageBox]::Show("确定要禁用计算机 $($global:selectedComputer.Name) 吗?","确认操作",[System.Windows.Forms.MessageBoxButtons]::YesNo) if ($confirm -eq [System.Windows.Forms.DialogResult]::Yes) { try { Disable-ADAccount -Identity $global:selectedComputer.DistinguishedName -ErrorAction Stop [System.Windows.Forms.MessageBox]::Show("计算机 $($global:selectedComputer.Name) 已成功禁用","操作完成") $Btn_Disable.Enabled = $false } catch { [System.Windows.Forms.MessageBox]::Show("禁用失败:$($_.Exception.Message)","错误") } } } } [void]$Form.ShowDialog()
说明:
- 修正了原脚本中变量拼接的语法错误,确保搜索通配符正确生效
- 添加了禁用计算机的按钮及逻辑,包含确认提示与错误处理
- 优化了交互体验:空输入提示、无结果提示、按钮状态随选中状态自动切换
内容的提问来源于stack exchange,提问作者PCMedicJAX
相关产品推荐
相关产品推荐

