Files
ObsidianHome/小鱼/闲鱼消息通知/toast.ps1
T

59 lines
1.6 KiB
PowerShell
Raw Normal View History

2026-11-09 09:18:58 +08:00
param(
[Parameter(Mandatory = $true)][string]$Title,
[Parameter(Mandatory = $true)][string]$Body,
[int]$Sound = 1
)
$ErrorActionPreference = 'Stop'
$shown = $false
try {
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
$t = [System.Security.SecurityElement]::Escape($Title)
$b = [System.Security.SecurityElement]::Escape($Body)
$xmlText = @"
<toast>
<visual>
<binding template="ToastGeneric">
<text>$t</text>
<text>$b</text>
<text placement="attribution">Goofish Watcher</text>
</binding>
</visual>
<audio silent="true"/>
</toast>
"@
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($xmlText)
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
$appIds = @(
'{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe',
'Microsoft.PowerShell',
'GoofishWatcher'
)
foreach ($id in $appIds) {
try {
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($id).Show($toast)
$shown = $true
break
} catch { }
}
} catch { }
if ($Sound -gt 0) {
for ($i = 0; $i -lt $Sound; $i++) {
try { [System.Media.SystemSounds]::Exclamation.Play() } catch { }
if ($i -lt $Sound - 1) { Start-Sleep -Milliseconds 500 }
}
}
if (-not $shown) {
try {
$sh = New-Object -ComObject WScript.Shell
$null = $sh.Popup("$Title`n$Body", 8, "Goofish Watcher", 64)
} catch { }
}