You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用AD查询结果填充PowerShell XAML窗体的ComboBox控件

解决方案

核心实现步骤

  • 先加载Active Directory模块并执行查询,提取纯服务器名称字符串(避免对象类型导致ComboBox显示异常)
  • 在XAML窗体初始化完成后,将查询到的服务器列表绑定到ComboBox控件

完整示例代码

# 加载Active Directory模块
Import-Module ActiveDirectory -ErrorAction Stop

# 执行AD查询,提取排序后的服务器名称字符串数组
$servers = Get-ADComputer -Filter "OperatingSystem -like '*Server*'" -Properties Name | 
           Select-Object -ExpandProperty Name | Sort-Object

# XAML窗体定义
$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="服务器进程管理工具" Height="450" Width="800">
    <Grid>
        <ComboBox x:Name="ServerComboBox" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="200"/>
        <Button x:Name="GetProcessesBtn" Content="获取进程" HorizontalAlignment="Left" Margin="220,10,0,0" VerticalAlignment="Top" Width="100"/>
        <Button x:Name="KillProcessBtn" Content="终止进程" HorizontalAlignment="Left" Margin="330,10,0,0" VerticalAlignment="Top" Width="100"/>
        <DataGrid x:Name="ProcessGrid" HorizontalAlignment="Left" Margin="10,50,0,0" VerticalAlignment="Top" Height="350" Width="760"/>
    </Grid>
</Window>
"@

# 加载XAML并创建窗体对象
$reader = [System.Xml.XmlReader]::Create([System.IO.StringReader]$xaml)
$window = [System.Windows.Markup.XamlReader]::Load($reader)

# 获取ComboBox控件并填充服务器列表
$serverCombo = $window.FindName('ServerComboBox')
$serverCombo.ItemsSource = $servers
# 默认选中第一个服务器(可选)
if ($servers.Count -gt 0) {
    $serverCombo.SelectedIndex = 0
}

# 获取进程按钮点击事件
$getProcessesBtn = $window.FindName('GetProcessesBtn')
$getProcessesBtn.Add_Click({
    $selectedServer = $serverCombo.SelectedItem
    if (-not $selectedServer) {
        [System.Windows.MessageBox]::Show('请选择一台服务器')
        return
    }
    try {
        $processes = Get-CimInstance -ComputerName $selectedServer -ClassName Win32_Process -ErrorAction Stop |
                     Select-Object ProcessId, Name, ExecutablePath, ThreadCount, WorkingSetSize
        $ProcessGrid = $window.FindName('ProcessGrid')
        $ProcessGrid.ItemsSource = $processes
    }
    catch {
        [System.Windows.MessageBox]::Show("获取进程失败:$_")
    }
})

# 终止进程按钮点击事件
$killProcessBtn = $window.FindName('KillProcessBtn')
$killProcessBtn.Add_Click({
    $selectedServer = $serverCombo.SelectedItem
    $selectedProcess = $ProcessGrid.SelectedItem
    if (-not $selectedServer -or -not $selectedProcess) {
        [System.Windows.MessageBox]::Show('请选择服务器和要终止的进程')
        return
    }
    try {
        Stop-CimInstance -InputObject $selectedProcess -ErrorAction Stop
        [System.Windows.MessageBox]::Show('进程已成功终止')
        # 自动刷新进程列表
        $getProcessesBtn.RaiseEvent([System.Windows.RoutedEventArgs]::new([System.Windows.Controls.Primitives.ButtonBase]::ClickEvent))
    }
    catch {
        [System.Windows.MessageBox]::Show("终止进程失败:$_")
    }
})

# 显示窗体
$window.ShowDialog() | Out-Null

关键细节说明

  • 使用Select-Object -ExpandProperty Name将AD查询返回的对象转换为纯字符串数组,确保ComboBox显示的是服务器名称而非对象实例
  • 直接将服务器数组赋值给ComboBox的ItemsSource,这是WPF中填充列表控件的高效方式
  • 用Get-CimInstance替代Get-Process,在PowerShell 7中对远程服务器的兼容性更好
  • 添加了基础错误处理和用户提示,提升工具的易用性

内容的提问来源于stack exchange,提问作者Matt Williamson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 04:25:03