在 Windows 10 中從命令列建立通知

在 Windows 10 中從命令列建立通知

我有一個與鍵盤快捷鍵關聯的腳本,當我觸發該快捷鍵時,我希望出現通知,確認腳本已執行。

如何從腳本內部建立通知?

編輯

在Linux上,我曾經這樣做過command && notify-send "My message"

答案1

在 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);

相關內容