参考サイトで面白いスクリプト(Powershellでトースト通知)を発見。
その中で、Windows RTの機能( Windows UWP 名前空間 – Windows UWP applications | Microsoft Docs)の中でアプリケーションIDが必要なクラスがあり、使い方に困っていたので非常に参考になった。
以下のソース中で通知のために使っている『ToastNotificationManager クラス (Windows.UI.Notifications) – Windows UWP applications | Microsoft Docs』では、 実行のためにAppUserModelID が必要な仕様となっている。
こうしたパターンがWindows RTのクラスの中に他にもあったため、Powershellからどうやって実行させればよいのかなーと悩んでいたので、助かりました。
function global:Toast {
param ([Parameter(Mandatory)][String] $title,[Parameter(Mandatory)][String] $msg,[String] $picturePath,[String] $bigPicturePath )
if($picturePath -ne "") {$p1=Resolve-Path $picturePath;}
if($bigPicturePath -ne "") {$p2=Resolve-Path $bigPicturePath;}
$AppId = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe";
$template = @"
<toast><visual><binding template="ToastGeneric"><text>$($title)</text><text>$($msg)</text>
<image placement="appLogoOverride" hint-crop="circle" src="$($p1)"/><image src="$($p2)"/>
</binding></visual></toast>
"@;
[void][Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime];
$xml=new-object Windows.Data.Xml.Dom.XmlDocument;
$xml.loadXml($template);
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::CreateToastNotifier($AppId).Show($xml);
}
Toast -title "papanda925" -msg "Windows Info"
アプリケーションIDの探し方
ここでのアプリケーションIDは『 $AppId = “{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe”;』
このIDだが、Powershellから、Get-StartAppコマンドを実行することで確認できる。
試しにWindows11環境下でGet-StartAppを実行した場合のサンプル
PS C:\Users\papanda925> Get-StartApps | Where-Object {($_ -like "*powershell*") } | Select-Object AppID
AppID
-----
Microsoft.AutoGenerated.{778F4D5A-A757-C4A9-2757-3B497E9D085A}
{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe
{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\PowerShell_ISE.exe
{D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}\WindowsPowerShell\v1.0\powershell.exe
{D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}\WindowsPowerShell\v1.0\PowerShell_ISE.exe
この結果より、上記サンプルで指定しているアプリケーションIDは、Get-StartApps で確認することが可能。
参考サイト
ASCII.jp:Windows PowerShellからスクリプトの完了をトースト通知で知らせる (1/2)
ASCII.jp:Windows PowerShellからスクリプトの完了をトースト通知で知らせる (2/2)
インストール済みアプリのアプリケーション ユーザー モデル ID の検索 – Configure Windows | Microsoft Docs


コメント