Le but de cette fonction Powershell est d'afficher un message à l'utilisateur pendant l'exécution d'un script.
Suivant le système d'exploitation, le résultat sera différent :
La fonction utilise une variable externe, déclarée au niveau du script, pour stocker l'objet "NotifyIcon" utilisé pour l'affichage. Cette variable doit être initialisée à $null avant le premier appel à la fonction (vu qu'il n'est pas possible de passer en paramètre une variable valorisée à $null).
function Show-Notification {
<#
.SYNOPSIS
Display a notification message in the notification area.
.DESCRIPTION
Displays a notification message, with customizable text, title, system tray icon and message icon.
.PARAMETER Message
Text to display
.PARAMETER Title
The title for the message balloon.
.PARAMETER MessageType
The type of message. This value determines what type of icon to display.
Valid values are Info, Warning, Error, None.
.PARAMETER SysTrayIcon
The path to a file that you will use as the system tray icon.
Default is the PowerShell ISE icon.
.PARAMETER Duration
Message display duration, in milliseconds, default 10000 (10s)
.NOTES
A script variable $notifyIcon needs to be defined beforehand, initialized at $null before the first call.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
$Message,
[Parameter(Mandatory = $true)]
$Title,
[Parameter(Mandatory = $false)]
$IconPath,
[Parameter(Mandatory = $false)]
[ValidateSet('None', 'Info', 'Warning', 'Error')]
$MessageType = 'Info',
[Parameter(Mandatory = $false)]
$Timeout = 10000
)
Add-Type -AssemblyName System.Windows.Forms
if ($null -eq $script:notifyIcon) {
$script:notifyIcon = New-Object System.Windows.Forms.NotifyIcon
}
if ($null -eq $IconPath) {
$IconPath = Get-Process -id $pid | Select-Object -ExpandProperty Path
}
$notifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($IconPath)
$notifyIcon.BalloonTipIcon = $MessageType
$notifyIcon.BalloonTipText = $Message
$notifyIcon.BalloonTipTitle = $Title
$notifyIcon.Visible = $true
$notifyIcon.ShowBalloonTip($Timeout)
}