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

C#实现向Active Directory用户或组推送Windows通知

在Windows中推送通知:手动方式与C#实现方案

一、手动推送Windows通知

1. 使用msg命令(域环境适用)

msg是Windows自带的命令行工具,可快速向指定用户/计算机推送文本通知,适合域内批量操作:

  • 推送单个用户:msg john_doe /server:PC-WORKSTATION01 "AD用户组已完成同步,请检查权限"
  • 推送域内所有用户:msg * /domain:yourcompany.com "重要通知:域安全策略将于今晚22点更新"

注意:需要目标机器开启"Messenger"服务,且执行命令的用户需有对应权限。

2. 使用PowerShell的BurntToast模块

BurntToast支持更丰富的通知样式(图标、按钮、进度条等),步骤如下:

  1. 先安装模块(管理员权限执行):Install-Module -Name BurntToast -Force
  2. 推送本地通知:New-BurntToastNotification -Text "AD系统提醒", "你的用户权限已变更" -AppLogo "C:\path\to\logo.png"
  3. 推送远程通知(需启用PowerShell远程):
    Invoke-Command -ComputerName REMOTE-PC-02 -ScriptBlock {
        New-BurntToastNotification -Text "域管理员通知", "请在3日内完成密码重置"
    }
    

二、C#编程实现Windows通知

1. 桌面应用(WPF/WinForms/.NET 5+):使用Microsoft.Toolkit.Uwp.Notifications

这是微软官方提供的NuGet包,无需UWP环境即可实现Toast通知:

  1. 安装包:在NuGet管理器中搜索并安装Microsoft.Toolkit.Uwp.Notifications
  2. 基础通知示例:
    using Microsoft.Toolkit.Uwp.Notifications;
    
    public void SendADNotification()
    {
        new ToastContentBuilder()
            .AddText("AD用户组操作完成")
            .AddText("已成功更新12个用户的组权限")
            .AddButton(new ToastButton()
                .SetContent("查看日志")
                .AddArgument("action", "openLog"))
            .Show();
    }
    
  3. 推送远程通知(结合PowerShell调用):
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    
    public void SendRemoteNotification(string targetPC, string message)
    {
        var runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
    
        using var ps = PowerShell.Create();
        ps.Runspace = runspace;
        ps.AddCommand("Invoke-Command")
          .AddParameter("ComputerName", targetPC)
          .AddParameter("ScriptBlock", ScriptBlock.Create($@"
              New-BurntToastNotification -Text 'AD系统通知', '{message}'
          "));
    
        var results = ps.Invoke();
        runspace.Close();
    
        // 处理执行异常或结果
        if (ps.HadErrors)
        {
            foreach (var err in ps.Streams.Error)
            {
                // 记录错误信息
            }
        }
    }
    

2. UWP/WinUI应用:原生Toast通知API

直接使用UWP内置的通知API,适合现代Windows应用:

using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;

public void SendUwpToastNotification()
{
    // 获取Toast模板并填充内容
    var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
    var textNodes = toastXml.GetElementsByTagName("text");
    
    textNodes[0].AppendChild(toastXml.CreateTextNode("AD系统提醒"));
    textNodes[1].AppendChild(toastXml.CreateTextNode("你的用户组已被更新,请重新登录生效"));

    // 创建并显示通知
    var toastNotification = new ToastNotification(toastXml);
    ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
}

注意事项

  • 远程通知推送需要目标机器允许PowerShell远程连接,且执行用户需具备管理员/域管理员权限。
  • msg命令在Windows 10/11部分版本中可能被限制,建议优先使用BurntToast或编程方式。
  • 桌面应用使用Microsoft.Toolkit.Uwp.Notifications时,若需处理通知点击事件,需注册Toast通知的激活回调。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 19:54:25