タスクバーに通知を表示するPowerShellスクリプト
小ネタ
以下のようなメッセージを簡単に表示できるPowerShellスクリプト。
表示はそれぞれWIn 8.1 Win10
PowerShellスクリプト
本体。適当な場所に「○○.ps1」として保存する。
param( [string]$Prompt = 'メッセージ', [string]$Title = '通知', $CallBack = '' ) Add-Type -AssemblyName System.Windows.Forms, System.Drawing function Show-NotifyIcon { param( [string]$Prompt = 'メッセージ', [string]$Title = '通知', [scriptblock]$CallBack = {} ) [Windows.Forms.NotifyIcon]$notifyIcon = New-Object -TypeName Windows.Forms.NotifyIcon -Property @{ BalloonTipIcon = [Windows.Forms.ToolTipIcon]::Info BalloonTipText = $Prompt BalloonTipTitle = $Title Icon = [Drawing.SystemIcons]::Information Text = $Title Visible = $true } # イベント定義 $notifyIcon.add_BalloonTipClicked( $CallBack ) [int]$timeout = 3 # sec [DateTimeOffset]$finishTime = [DateTimeOffset]::UtcNow.AddSeconds( $timeout ) $notifyIcon.ShowBalloonTip( $timeout ) # そのままだとイベントが走らない&すぐに消えてしまうので適当wait while ( [DateTimeOffset]::UtcNow -lt $finishTime ) { Start-Sleep -Milliseconds 1 } $notifyIcon.Dispose() } $parameters = $MyInvocation.BoundParameters $parameters.CallBack = [scriptblock]::Create( $CallBack ) Show-NotifyIcon @parameters
使い方
引数1:表示するメッセージ
引数2:タイトル
引数3:クリックされたときのコールバック処理
を文字列で渡す。
バッチ
powershell.exe -Sta -NoProfile -WindowStyle Hidden -ExecutionPolicy RemoteSigned -File 上記のps1ファイル メッセージ タイトル コールバック処理
VBA
VBAならWindows API使えば?という話はさておく。
Sub ShowNotifySample() Const NotifyScriptPath = "保存したスクリプト(ps1ファイル)の保存場所" Const PsCommandLineBase = "powershell.exe -Sta -NoProfile -WindowStyle Hidden -ExecutionPolicy RemoteSigned -File """ Const WQuoteSpaceWQuote = """ """ '表示するメッセージ及びタイトル Dim notifyMessage As String notifyMessage = "めっせーじ" Dim notifyTitle As String notifyTitle = "たいとる" 'クリックされた時の処理(PowerShellスクリプト) 'エクスプローラーでエクセルの場所を開く Dim callBackPsScript As String callBackPsScript = "explorer.exe " & Excel.Application.Path Dim execCmd As String execCmd = PsCommandLineBase & NotifyScriptPath & WQuoteSpaceWQuote & _ notifyMessage & WQuoteSpaceWQuote & _ notifyTitle & WQuoteSpaceWQuote & _ callBackPsScript & """" Call VBA.Shell(execCmd, vbHide) End Sub