PowerShellでトースト通知やバルーンチップを扱う

私はよく水を飲むのを忘れるため、文明の利器を使って解決する。これぞ人類。

トースト

参考

https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/adaptive-interactive-toasts
https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/toast-xml-schema
https://hazzy.techanarchy.net/posh/powershell/win-10-notification-toasting-for-your-daily-bread/
https://gist.github.com/altrive/72594b8427b2fff16431
https://qiita.com/kiduki/items/0a00798a6251abd9de82
https://qiita.com/akiqsinco/items/0c0d5ae4abc756a594b3

PowerShellスクリプト

Shift_JISじゃないと日本語は文字化けするよ

Function Toast {
    #AppID調べる:Get-StartApps
    $AppId = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe"

    #ロード済み一覧:[System.AppDomain]::CurrentDomain.GetAssemblies() | % { $_.GetName().Name }
    #WinRTAPIを呼び出す:[-Class-,-Namespace-,ContentType=WindowsRuntime]
    $null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
    $null = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
    
    #XmlDocumentクラスをインスタンス化
    $xml = New-Object Windows.Data.Xml.Dom.XmlDocument
    #LoadXmlメソッドを呼び出し、変数templateをWinRT型のxmlとして読み込む
    $xml.LoadXml($template)

    #ToastNotificationクラスのCreateToastNotifierメソッドを呼び出し、変数xmlをトースト
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId).Show($xml)
}

#トーストテンプレート
$template = @"
<toast>
    <visual>
        <binding template="ToastText02">
            <text id="1">レイシア</text>
            <text id="2">オーナー、宜しければ一緒にお茶をいかがですか?</text>
        </binding>
    </visual>
</toast>
"@

Toast
exit

イコン画像と通知音をつける

#トーストテンプレート
$template = @"
<toast>
    <visual>
        <binding template="ToastGeneric">
            <text>レイシア</text>
            <text>オーナー、宜しければ一緒にお茶をいかがですか?</text>
            <image placement="appLogoOverride" "hint-crop="circle" hint-align="center" src="C:\DTV\racia\00285_toast.png"/>
        </binding>
    </visual>
    <audio silent="true" src="ms-winsoundevent:Notification.Default" />
</toast>

大きい画像を付けて5秒間表示する

#トーストテンプレート
$template = @"
<toast duration="5">
    <visual>
        <binding template="ToastGeneric">
            <text>レイシア</text>
            <text>オーナー、宜しければ一緒にお茶をいかがですか?</text>
            <image placement="hero" src="C:\DTV\racia\00285_toast.png"/>
        </binding>
    </visual>
</toast>
"@

再通知とかできるやつ

#トーストテンプレート
$template = @"
<toast duration="long">
    <visual>
        <binding template="ToastText02">
            <text id="1">レイシア</text>
            <text id="2">オーナー、宜しければ一緒にお茶をいかがですか?</text>
        </binding>
    </visual>
  <actions>
  <input id="snoozeTime" type="selection" defaultInput="15">
    #5つまで
    <selection id="3" content="3 minutes" />
    <selection id="5" content="5 minutes" />
    <selection id="10" content="10 minutes" />
    <selection id="15" content="15 minutes" />
    <selection id="30" content="30 minutes" />
  </input>
  <action activationType="system" arguments="snooze" hint-inputId="snoozeTime" content=""/>
  <action activationType="system" arguments="dismiss" content=""/>
  </actions>
</toast>
"@

バルーンチップ

Windows10だと似たような見た目だったり。

参考

https://mcpmag.com/articles/2017/09/07/creating-a-balloon-tip-notification-using-powershell.aspx
https://deploywindows.com/2015/12/01/powershell-can-i-use-balloons-toasts-and-notifications/
https://github.com/proxb/PowerShell_Scripts/blob/master/Invoke-BalloonTip.ps1
https://social.technet.microsoft.com/Forums/Lync/en-US/6c469413-1716-490d-b02d-3ca9398fc6a9/powershell-balloon-tip-in-system-context

PowerShellスクリプト

Function Balloon {
    #System.Windows.FormsクラスをPowerShellセッションに追加
    Add-Type -AssemblyName System.Windows.Forms
    #NotifyIconクラスをインスタンス化
    $balloon = New-Object System.Windows.Forms.NotifyIcon
    #powershellのアイコンを抜き出す
    $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe')
    #特定のTipIconのみを使用可
    #[System.Windows.Forms.ToolTipIcon] | Get-Member -Static -Type Property
    $balloon.BalloonTipIcon  = [System.Windows.Forms.ToolTipIcon]::Error
    #表示するメッセージ
    $balloon.BalloonTipText  = "Lentinula edodes"
    #表示するタイトル
    $balloon.BalloonTipTitle = 'シイタケ'
    #タスクトレイアイコン表示
    $balloon.Visible = $True
    #1000ミリ秒表示
    $balloon.ShowBalloonTip(1000)
    #1秒待ってからタスクトレイアイコン非表示
    Start-Sleep -Seconds 1
    $balloon.Visible = $False
}

Balloon
Exit

タスクスケジューラで扱う

最小化で実行する

VBScriptを使用。拡張子.vbsで保存。

CreateObject("WScript.Shell").Run "powershell -NoProfile -ExecutionPolicy RemoteSigned C:\DTV\bin\posttoast.ps1", 0

タスクスケジューラの設定

1.taskschd.msc->タスクの作成
2.トリガー:スタートアップ時、繰り返し間隔30分間、無期限
3.操作:プログラムの開始、vbsのフルパスを指定