daka Asked: 2020-03-18 14:04:46 +0800 CST2020-03-18 14:04:46 +0800 CST 2020-03-18 14:04:46 +0800 CST 在 Windows 10 中从命令行创建通知 772 我有一个与键盘快捷键关联的脚本,当我触发快捷键时,我希望出现一个通知,确认脚本已执行。 如何从脚本内部创建通知? 编辑 在Linux上,我曾经这样做command && notify-send "My message" windows-10 windows 2 个回答 Voted Best Answer Yisroel Tech 2020-03-18T14:24:41+08:002020-03-18T14:24:41+08:00 在 PowerShell 中,您可以运行(从此处): [reflection.assembly]::loadwithpartialname("System.Windows.Forms") [reflection.assembly]::loadwithpartialname("System.Drawing") $notify = new-object system.windows.forms.notifyicon $notify.icon = [System.Drawing.SystemIcons]::Information $notify.visible = $true $notify.showballoontip(10,"Script Completed!","Your script ran succesfully!",[system.windows.forms.tooltipicon]::None) 或者这个(从这里): $ErrorActionPreference = "Stop" $notificationTitle = "Notification: Your script has been completed successfully" [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01) $toastXml = [xml] $template.GetXml() $toastXml.GetElementsByTagName("text").AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null $xml = New-Object Windows.Data.Xml.Dom.XmlDocument $xml.LoadXml($toastXml.OuterXml) $toast = [Windows.UI.Notifications.ToastNotification]::new($xml) $toast.Tag = "Test1" $toast.Group = "Test2" $toast.ExpirationTime = [DateTimeOffset]::Now.AddSeconds(5) $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("Script Completed!") $notifier.Show($toast); TechGuru 2022-10-28T02:32:49+08:002022-10-28T02:32:49+08:00 不,这篇文章有详细的解释,但基本上,命令行无法访问通知中心,所以你必须通过通知设置>添加应用程序权限> CMD.exe给它。您需要这样做才能通过命令行创建通知。
在 PowerShell 中,您可以运行(从此处):
或者这个(从这里):
不,这篇文章有详细的解释,但基本上,命令行无法访问通知中心,所以你必须通过通知设置>添加应用程序权限> CMD.exe给它。您需要这样做才能通过命令行创建通知。