我正在尝试通过 Powershell 发送 toast 通知,并且在我的本地主机上运行它效果很好。我有两种方法来生成 toast:一种是使用 BurntToast 模块,另一种是使用 XML 和
UI.Notifications
。我对这两种方法都持公正态度,两种方法都有效,但也都给我带来了问题。最终目标是使用 PowerShell Universal 在不同的服务器上运行脚本,然后远程访问设备并生成 toast。
方法一:BurntToast
首先,这是 BurntToast 的代码:
[string]$Header = 'Read this toast'
[string]$Body = 'Click the button below to view more information'
[string]$RedirectURL = 'https://www.youtube.com/watch?v=8ybW48rKBME&t'
[string]$HeroImage = 'https://files.worldwildlife.org/wwfcmsprod/images/Sea_Turtle_Hol_Chan_Marine_Reserve_WW1105958/hero_small/5fv88fm3v6_Sea_Turtle_Hol_Chan_Marine_Reserve_WW1105958.jpg'
[string]$AppLogo = 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/small-sea-turtle-chelonioidea-swimming-inside-a-shallow-sea-joaquin-corbalan.jpg'
$Button = New-BTButton -Content 'Click here to view' -Arguments $RedirectURL
New-BurntToastNotification -Text $Header, $Body -Button $Button -AppLogo $AppLogo -HeroImage $HeroImage
当从我的本地主机运行时,它运行得很好,即使我将所有内容都包含在
Invoke-Command
中。
但是,当我从 PowerShell Universal 运行它时,没有任何图像会保留。我在我的机器名称上使用
Invoke-Command
。 Toast 已发送,按钮显示正确的链接,但 Toast 不显示图像。
方法2:XML和UI.Notifications
这是 UI.Notifications 的代码。我把它包裹在
Invoke-Command
中,因为它似乎不能很好地与 Powershell 7 配合使用。将括号内的所有内容复制/粘贴到 PS 5 终端效果很好。
Invoke-Command -ComputerName localhost -ScriptBlock {
[string]$Header = 'New Toast'
[string]$Body = 'Please click the button to view more information'
[string]$RedirectURL = 'https://www.youtube.com/watch?v=8ybW48rKBME&t'
[string]$HeroImage = 'https://files.worldwildlife.org/wwfcmsprod/images/Sea_Turtle_Hol_Chan_Marine_Reserve_WW1105958/hero_small/5fv88fm3v6_Sea_Turtle_Hol_Chan_Marine_Reserve_WW1105958.jpg'
[string]$AppLogo = 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/small-sea-turtle-chelonioidea-swimming-inside-a-shallow-sea-joaquin-corbalan.jpg'
$xml = @"
<toast>
<visual>
<binding template="ToastGeneric">
<image placement="appLogoOverride" hint-crop="circle" src="$AppLogo"/>
<text>$Header</text>
<text>$Body</text>
<image src="$HeroImage"/>
</binding>
</visual>
<actions>
<action content="View Page" activationType="protocol" arguments="$RedirectURL" />
</actions>
</toast>
"@
$xml = $xml.Replace('&','&')
$XmlDocument = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]::New()
$XmlDocument.loadXml($xml)
$AppId = 'Microsoft.WindowsTerminal_8wekyb3d8bbwe!App'
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::CreateToastNotifier($AppId).Show($XmlDocument)
}
这在本地主机和 PSU 上也能成功运行。该按钮工作正常,就像关闭它的 X 一样,但我注意到,当我单击图像或文本等其他任何内容时,它会提示 Windows 弹出窗口,询问终端是否可以对您的设备进行更改,需要管理员同意。当我在本地从 PSU 运行它时会发生这种情况。
简而言之,我正在寻找任一方法的解决方案,并解决以下问题之一:
过去有其他人在这两个方面取得过成功吗?
首先 - 噢。我已经被滚滚了。 :P
好的 - 我已尝试在我的环境(W10 22H2 / PowerShell 5.1)中复制此内容,并取得了一些进展和好坏参半的结果。
这是我根据您的代码得到的:
[string]$Header = 'New Toast'
[string]$Body = 'Please click the button to view more information'
[string]$RedirectURL = [System.Security.SecurityElement]::Escape('https://www.youtube.com/watch?v=8ybW48rKBME&t')
[string]$HeroImage = [System.Security.SecurityElement]::Escape('https://files.worldwildlife.org/wwfcmsprod/images/Sea_Turtle_Hol_Chan_Marine_Reserve_WW1105958/hero_small/5fv88fm3v6_Sea_Turtle_Hol_Chan_Marine_Reserve_WW1105958.jpg')
[string]$AppLogo = [System.Security.SecurityElement]::Escape('https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/small-sea-turtle-chelonioidea-swimming-inside-a-shallow-sea-joaquin-corbalan.jpg')
$appId = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
$null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$null = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
$xml = @"
<toast launch="chatLaunchArg">
<visual>
<binding template="ToastGeneric">
<image placement="appLogoOverride" hint-crop="circle" src="$AppLogo"/>
<text>$Header</text>
<text>$Body</text>
<image src="$HeroImage"/>
</binding>
</visual>
<actions>
<action content="View Page" activationType="protocol" arguments="$RedirectURL" />
</actions>
</toast>
"@
$toastXml = [Windows.Data.Xml.Dom.XmlDocument]::new()
$toastXml.LoadXml($xml)
$toast = [Windows.UI.Notifications.ToastNotification]::new($toastXml)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($appId).Show($toast)
请注意重定向 URL 等的“[System.Security.SecurityElement]::Escape()”部分。这会绕过这些实体的 XML 解析器。
此外,我未能成功地远程/实时下载图像。我认为这是 5.1 的限制,您的结果可能会有所不同。我建议考虑将图像与脚本一起定位,或者动态下载到脚本可以在本地找到和调用的位置。
最后,BurntToast - 我感觉它在功能上已被放弃,并且到目前为止已经 2 年没有在 github 上真正更新过。他们在过去一年中进行了 1-2 次预发布,但据我所知还没有正式发布。在生产环境中使用这样的模块可能有点冒险。
无论如何 - 我希望这有一定的价值:)