mirror of
https://github.com/freedbygrace/WindowsNotifications.git
synced 2026-07-26 12:08:14 +00:00
First Commit
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
# WindowsNotifications PowerShell Module
|
||||
|
||||
This PowerShell module provides a convenient way to use the Windows Notifications library from PowerShell scripts.
|
||||
|
||||
## Installation
|
||||
|
||||
1. Copy the module files to one of the following locations:
|
||||
- `%UserProfile%\Documents\WindowsPowerShell\Modules\WindowsNotifications` (for the current user)
|
||||
- `%ProgramFiles%\WindowsPowerShell\Modules\WindowsNotifications` (for all users)
|
||||
|
||||
2. Make sure the `WindowsNotifications.dll` file is in the same directory as the module files.
|
||||
|
||||
3. Import the module:
|
||||
```powershell
|
||||
Import-Module WindowsNotifications
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Initialize the Module
|
||||
|
||||
Before using the module, you need to initialize it:
|
||||
|
||||
```powershell
|
||||
# Initialize with default settings
|
||||
Initialize-WindowsNotifications
|
||||
|
||||
# Or initialize with a custom assembly path
|
||||
Initialize-WindowsNotifications -AssemblyPath "C:\Path\To\WindowsNotifications.dll"
|
||||
|
||||
# Or initialize with a custom database path
|
||||
Initialize-WindowsNotifications -DatabasePath "C:\Path\To\Notifications.db"
|
||||
```
|
||||
|
||||
### Show Notifications
|
||||
|
||||
#### Simple Notification
|
||||
|
||||
```powershell
|
||||
Show-Notification -Title "Hello" -Message "This is a simple notification"
|
||||
```
|
||||
|
||||
#### Notification with Buttons
|
||||
|
||||
```powershell
|
||||
$result = Show-Notification -Title "Action Required" -Message "Please select an option:" -Buttons "Approve", "Reject", "Defer"
|
||||
|
||||
if ($result.ClickedButtonText -eq "Approve") {
|
||||
Write-Host "User approved the action"
|
||||
}
|
||||
```
|
||||
|
||||
#### Reboot Notification with Deferrals
|
||||
|
||||
```powershell
|
||||
$result = Show-Notification -Title "System Reboot Required" -Message "Your system needs to be rebooted." -RebootButtonText "Reboot Now" -DeferButtonText "Defer"
|
||||
|
||||
if ($result.ClickedButtonId -eq "reboot") {
|
||||
# Reboot the system
|
||||
Restart-Computer -Force
|
||||
}
|
||||
```
|
||||
|
||||
#### Advanced Options
|
||||
|
||||
```powershell
|
||||
Show-Notification -Title "Advanced Notification" -Message "This notification has advanced options" `
|
||||
-Async -PersistState -TimeoutInSeconds 30 -LogoImagePath "C:\Path\To\Logo.png" `
|
||||
-HeroImagePath "C:\Path\To\Hero.png" -ShowReminder -ReminderTimeInMinutes 5
|
||||
```
|
||||
|
||||
### Work with Notification Results
|
||||
|
||||
```powershell
|
||||
# Get the result of a notification
|
||||
$result = Get-NotificationResult -NotificationId "notification-id"
|
||||
|
||||
# Wait for a notification to complete
|
||||
$result = Wait-Notification -NotificationId "notification-id" -TimeoutInSeconds 30
|
||||
|
||||
# Get all notification history
|
||||
$history = Get-NotificationHistory
|
||||
|
||||
# Remove notification history
|
||||
Remove-NotificationHistory -NotificationId "notification-id"
|
||||
|
||||
# Remove all notification history
|
||||
Remove-NotificationHistory
|
||||
```
|
||||
|
||||
### System Information
|
||||
|
||||
```powershell
|
||||
# Check if running as SYSTEM
|
||||
$isSystem = Test-SystemContext
|
||||
|
||||
# Get interactive user sessions
|
||||
$sessions = Get-InteractiveUserSessions
|
||||
```
|
||||
|
||||
## Command Reference
|
||||
|
||||
| Command | Description |
|
||||
| ------- | ----------- |
|
||||
| `Initialize-WindowsNotifications` | Initializes the Windows Notifications module |
|
||||
| `Show-Notification` | Shows a notification with various options |
|
||||
| `Get-NotificationResult` | Gets the result of a notification |
|
||||
| `Wait-Notification` | Waits for a notification to complete |
|
||||
| `Get-NotificationHistory` | Gets all notification results from the database |
|
||||
| `Remove-NotificationHistory` | Deletes notification results from the database |
|
||||
| `Get-InteractiveUserSessions` | Gets all interactive user sessions |
|
||||
| `Test-SystemContext` | Checks if the current process is running as SYSTEM |
|
||||
|
||||
## Custom Branding
|
||||
|
||||
The module supports custom branding for notifications, allowing you to create notifications that match your organization's branding:
|
||||
|
||||
```powershell
|
||||
Show-Notification -Title "IT Department Notification" -Message "System update required" `
|
||||
-BrandingText "Contoso IT" `
|
||||
-BrandingColor "#0078D7" `
|
||||
-AccentColor "#E81123" `
|
||||
-LogoImagePath "C:\Path\To\Logo.png" `
|
||||
-HeroImagePath "C:\Path\To\Banner.png" `
|
||||
-UseDarkTheme
|
||||
```
|
||||
|
||||
### Supported Branding Options
|
||||
|
||||
| Option | Description |
|
||||
| ------ | ----------- |
|
||||
| `BrandingText` | Custom branding text (e.g., company or department name) |
|
||||
| `BrandingColor` | Custom branding color (hex format: #RRGGBB) |
|
||||
| `AccentColor` | Custom accent color for the notification (hex format: #RRGGBB) |
|
||||
| `UseDarkTheme` | Whether to use dark theme for the notification |
|
||||
| `LogoImagePath` | Path or URL to a logo image |
|
||||
| `HeroImagePath` | Path or URL to a hero/banner image |
|
||||
| `InlineImagePath` | Path or URL to an inline image |
|
||||
| `AppIconPath` | Path or URL to an app icon |
|
||||
| `BackgroundImagePath` | Path or URL to a background image |
|
||||
| `AudioSource` | Audio source for the notification |
|
||||
| `SilentMode` | Whether to show the notification in silent mode (no sound) |
|
||||
| `DeadlineTime` | Deadline time for the notification |
|
||||
| `ShowCountdown` | Whether to show a countdown timer on the notification |
|
||||
| `DeadlineActionCommand` | Command to execute when the deadline is reached |
|
||||
| `DeadlineActionArguments` | Arguments for the deadline command |
|
||||
| `DeadlineActionUrl` | URL to open when the deadline is reached |
|
||||
| `DeadlineActionScript` | PowerShell script to execute when the deadline is reached |
|
||||
| `EnableLogging` | Whether to enable logging for the notification |
|
||||
| `LogFilePath` | Path to the log file
|
||||
|
||||
## Examples
|
||||
|
||||
### Example 1: Simple Notification
|
||||
|
||||
```powershell
|
||||
Import-Module WindowsNotifications
|
||||
Initialize-WindowsNotifications
|
||||
|
||||
$result = Show-Notification -Title "Hello" -Message "This is a simple notification"
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
```
|
||||
|
||||
### Example 2: Notification with Buttons
|
||||
|
||||
```powershell
|
||||
Import-Module WindowsNotifications
|
||||
Initialize-WindowsNotifications
|
||||
|
||||
$result = Show-Notification -Title "Action Required" -Message "Please select an option:" -Buttons "Approve", "Reject", "Defer"
|
||||
|
||||
if ($result.ClickedButtonId) {
|
||||
Write-Host "Button clicked: $($result.ClickedButtonText)"
|
||||
|
||||
switch ($result.ClickedButtonText) {
|
||||
"Approve" { Write-Host "User approved the action" }
|
||||
"Reject" { Write-Host "User rejected the action" }
|
||||
"Defer" { Write-Host "User deferred the action" }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Example 3: Asynchronous Notification
|
||||
|
||||
```powershell
|
||||
Import-Module WindowsNotifications
|
||||
Initialize-WindowsNotifications
|
||||
|
||||
$result = Show-Notification -Title "Background Task" -Message "A background task is running" -Async
|
||||
|
||||
# Do some work
|
||||
for ($i = 1; $i -le 5; $i++) {
|
||||
Write-Host "Working... ($i/5)"
|
||||
Start-Sleep -Seconds 2
|
||||
|
||||
# Check if the notification has been interacted with
|
||||
$currentResult = Get-NotificationResult -NotificationId $result.NotificationId
|
||||
|
||||
if ($currentResult.Activated -or $currentResult.Dismissed) {
|
||||
Write-Host "User interacted with the notification"
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Work completed"
|
||||
```
|
||||
|
||||
### Example 4: Countdown and Deadline Notification
|
||||
|
||||
```powershell
|
||||
Import-Module WindowsNotifications
|
||||
Initialize-WindowsNotifications
|
||||
|
||||
# Create a notification with a deadline 5 minutes from now
|
||||
$result = Show-Notification -Title "System Maintenance" -Message "Your system needs to restart soon." `
|
||||
-Buttons "Restart Now", "Defer" `
|
||||
-DeadlineTime (Get-Date).AddMinutes(5) `
|
||||
-ShowCountdown `
|
||||
-DeadlineActionScript "Restart-Computer -Force" `
|
||||
-EnableLogging `
|
||||
-LogFilePath "C:\Logs\notifications.log"
|
||||
|
||||
Write-Host "Notification displayed with deadline: $($result.Displayed)"
|
||||
```
|
||||
@@ -0,0 +1,125 @@
|
||||
#
|
||||
# Module manifest for module 'WindowsNotifications'
|
||||
#
|
||||
|
||||
@{
|
||||
# Script module or binary module file associated with this manifest.
|
||||
RootModule = 'WindowsNotifications.psm1'
|
||||
|
||||
# Version number of this module.
|
||||
ModuleVersion = '1.0.0'
|
||||
|
||||
# Supported PSEditions
|
||||
CompatiblePSEditions = @('Desktop')
|
||||
|
||||
# ID used to uniquely identify this module
|
||||
GUID = '8a1b3c4d-5e6f-47a8-b9c0-d1e2f3a4b5c6'
|
||||
|
||||
# Author of this module
|
||||
Author = 'WindowsNotifications'
|
||||
|
||||
# Company or vendor of this module
|
||||
CompanyName = ''
|
||||
|
||||
# Copyright statement for this module
|
||||
Copyright = '(c) 2024. All rights reserved.'
|
||||
|
||||
# Description of the functionality provided by this module
|
||||
Description = 'PowerShell module for displaying Windows Toast Notifications from SYSTEM context to user sessions'
|
||||
|
||||
# Minimum version of the Windows PowerShell engine required by this module
|
||||
PowerShellVersion = '5.0'
|
||||
|
||||
# Name of the Windows PowerShell host required by this module
|
||||
# PowerShellHostName = ''
|
||||
|
||||
# Minimum version of the Windows PowerShell host required by this module
|
||||
# PowerShellHostVersion = ''
|
||||
|
||||
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
|
||||
DotNetFrameworkVersion = '4.7.2'
|
||||
|
||||
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
|
||||
CLRVersion = '4.0'
|
||||
|
||||
# Processor architecture (None, X86, Amd64) required by this module
|
||||
# ProcessorArchitecture = ''
|
||||
|
||||
# Modules that must be imported into the global environment prior to importing this module
|
||||
# RequiredModules = @()
|
||||
|
||||
# Assemblies that must be loaded prior to importing this module
|
||||
# RequiredAssemblies = @()
|
||||
|
||||
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
|
||||
# ScriptsToProcess = @()
|
||||
|
||||
# Type files (.ps1xml) to be loaded when importing this module
|
||||
# TypesToProcess = @()
|
||||
|
||||
# Format files (.ps1xml) to be loaded when importing this module
|
||||
# FormatsToProcess = @()
|
||||
|
||||
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
|
||||
# NestedModules = @()
|
||||
|
||||
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
|
||||
FunctionsToExport = @(
|
||||
'Initialize-WindowsNotifications',
|
||||
'Show-Notification',
|
||||
'Get-NotificationResult',
|
||||
'Wait-Notification',
|
||||
'Get-NotificationHistory',
|
||||
'Remove-NotificationHistory',
|
||||
'Get-InteractiveUserSessions',
|
||||
'Test-SystemContext'
|
||||
)
|
||||
|
||||
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
|
||||
CmdletsToExport = @()
|
||||
|
||||
# Variables to export from this module
|
||||
VariablesToExport = '*'
|
||||
|
||||
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
|
||||
AliasesToExport = @()
|
||||
|
||||
# DSC resources to export from this module
|
||||
# DscResourcesToExport = @()
|
||||
|
||||
# List of all modules packaged with this module
|
||||
# ModuleList = @()
|
||||
|
||||
# List of all files packaged with this module
|
||||
FileList = @(
|
||||
'WindowsNotifications.psm1',
|
||||
'WindowsNotifications.psd1',
|
||||
'WindowsNotifications.dll'
|
||||
)
|
||||
|
||||
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
|
||||
PrivateData = @{
|
||||
PSData = @{
|
||||
# Tags applied to this module. These help with module discovery in online galleries.
|
||||
Tags = @('Windows', 'Notifications', 'Toast', 'System')
|
||||
|
||||
# A URL to the license for this module.
|
||||
LicenseUri = 'https://github.com/freedbygrace/WindowsNotifications/blob/main/LICENSE'
|
||||
|
||||
# A URL to the main website for this project.
|
||||
ProjectUri = 'https://github.com/freedbygrace/WindowsNotifications'
|
||||
|
||||
# A URL to an icon representing this module.
|
||||
# IconUri = ''
|
||||
|
||||
# ReleaseNotes of this module
|
||||
ReleaseNotes = 'Initial release of the WindowsNotifications module'
|
||||
}
|
||||
}
|
||||
|
||||
# HelpInfo URI of this module
|
||||
# HelpInfoURI = ''
|
||||
|
||||
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
|
||||
# DefaultCommandPrefix = ''
|
||||
}
|
||||
@@ -0,0 +1,519 @@
|
||||
#
|
||||
# WindowsNotifications.psm1
|
||||
# PowerShell module for the Windows Notifications library
|
||||
#
|
||||
|
||||
# Module variables
|
||||
$script:NotificationAssembly = $null
|
||||
$script:NotificationManager = $null
|
||||
$script:DefaultDatabasePath = $null
|
||||
|
||||
#
|
||||
# Private functions
|
||||
#
|
||||
|
||||
function Initialize-NotificationAssembly {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$AssemblyPath
|
||||
)
|
||||
|
||||
try {
|
||||
# If assembly path is provided and exists, load from file
|
||||
if ($AssemblyPath -and (Test-Path $AssemblyPath)) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($AssemblyPath)
|
||||
$script:NotificationAssembly = [System.Reflection.Assembly]::Load($bytes)
|
||||
}
|
||||
# Otherwise, try to load from the module directory
|
||||
else {
|
||||
$moduleRoot = Split-Path -Parent $PSCommandPath
|
||||
$defaultPath = Join-Path -Path $moduleRoot -ChildPath "WindowsNotifications.dll"
|
||||
|
||||
if (Test-Path $defaultPath) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($defaultPath)
|
||||
$script:NotificationAssembly = [System.Reflection.Assembly]::Load($bytes)
|
||||
}
|
||||
else {
|
||||
throw "WindowsNotifications.dll not found. Please specify the path using the -AssemblyPath parameter."
|
||||
}
|
||||
}
|
||||
|
||||
# Create the notification manager
|
||||
$script:NotificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
$script:DefaultDatabasePath = $script:NotificationManager.GetDatabaseFilePath()
|
||||
|
||||
return $true
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to initialize WindowsNotifications assembly: $_"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
#
|
||||
# Public functions
|
||||
#
|
||||
|
||||
function Initialize-WindowsNotifications {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$AssemblyPath,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$DatabasePath
|
||||
)
|
||||
|
||||
# Initialize the assembly
|
||||
if (-not (Initialize-NotificationAssembly -AssemblyPath $AssemblyPath)) {
|
||||
return $false
|
||||
}
|
||||
|
||||
# If a custom database path is specified, create a new notification manager with that path
|
||||
if ($DatabasePath) {
|
||||
try {
|
||||
$script:NotificationManager = New-Object WindowsNotifications.NotificationManager($DatabasePath)
|
||||
$script:DefaultDatabasePath = $script:NotificationManager.GetDatabaseFilePath()
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to initialize NotificationManager with custom database path: $_"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "WindowsNotifications initialized successfully."
|
||||
Write-Host "Database path: $script:DefaultDatabasePath"
|
||||
|
||||
# Check if running as SYSTEM
|
||||
$isSystem = $script:NotificationManager.IsRunningAsSystem()
|
||||
Write-Host "Running as SYSTEM: $isSystem"
|
||||
|
||||
# Get interactive user sessions
|
||||
$sessions = $script:NotificationManager.GetInteractiveUserSessions()
|
||||
Write-Host "Interactive user sessions found: $($sessions.Count)"
|
||||
|
||||
return $true
|
||||
}
|
||||
|
||||
function Show-Notification {
|
||||
[CmdletBinding(DefaultParameterSetName="Simple")]
|
||||
param (
|
||||
[Parameter(Mandatory=$true, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$true, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$true, ParameterSetName="Reboot")]
|
||||
[string]$Title,
|
||||
|
||||
[Parameter(Mandatory=$true, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$true, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$true, ParameterSetName="Reboot")]
|
||||
[string]$Message,
|
||||
|
||||
[Parameter(Mandatory=$true, ParameterSetName="WithButtons")]
|
||||
[string[]]$Buttons,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$RebootButtonText = "Reboot Now",
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$DeferButtonText = "Defer",
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[switch]$Async,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[switch]$PersistState,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[int]$TimeoutInSeconds = 0,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$LogoImagePath,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$HeroImagePath,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$InlineImagePath,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$AppIconPath,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$BackgroundImagePath,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$BrandingText,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$BrandingColor,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$AccentColor,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[switch]$UseDarkTheme,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$AudioSource,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[switch]$SilentMode,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[switch]$ShowReminder,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[int]$ReminderTimeInMinutes = 60,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[DateTime]$DeadlineTime,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[switch]$ShowCountdown,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$DeadlineActionCommand,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$DeadlineActionArguments,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$DeadlineActionUrl,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$DeadlineActionScript,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[switch]$EnableLogging,
|
||||
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Simple")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="WithButtons")]
|
||||
[Parameter(Mandatory=$false, ParameterSetName="Reboot")]
|
||||
[string]$LogFilePath
|
||||
)
|
||||
|
||||
# Ensure the notification assembly is initialized
|
||||
if (-not $script:NotificationManager) {
|
||||
if (-not (Initialize-WindowsNotifications)) {
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
# Handle different parameter sets
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
"Simple" {
|
||||
# Create custom notification options for simple notification
|
||||
$options = New-Object WindowsNotifications.Models.NotificationOptions
|
||||
$options.Title = $Title
|
||||
$options.Message = $Message
|
||||
$options.Async = $Async
|
||||
$options.PersistState = $PersistState
|
||||
$options.TimeoutInSeconds = $TimeoutInSeconds
|
||||
$options.ShowReminder = $ShowReminder
|
||||
$options.ReminderTimeInMinutes = $ReminderTimeInMinutes
|
||||
|
||||
# Set image paths
|
||||
if ($LogoImagePath) { $options.LogoImagePath = $LogoImagePath }
|
||||
if ($HeroImagePath) { $options.HeroImagePath = $HeroImagePath }
|
||||
if ($InlineImagePath) { $options.InlineImagePath = $InlineImagePath }
|
||||
if ($AppIconPath) { $options.AppIconPath = $AppIconPath }
|
||||
if ($BackgroundImagePath) { $options.BackgroundImagePath = $BackgroundImagePath }
|
||||
|
||||
# Set branding options
|
||||
if ($BrandingText) { $options.BrandingText = $BrandingText }
|
||||
if ($BrandingColor) { $options.BrandingColor = $BrandingColor }
|
||||
if ($AccentColor) { $options.AccentColor = $AccentColor }
|
||||
if ($UseDarkTheme) { $options.UseDarkTheme = $UseDarkTheme }
|
||||
|
||||
# Set audio options
|
||||
if ($AudioSource) { $options.AudioSource = $AudioSource }
|
||||
if ($SilentMode) { $options.SilentMode = $SilentMode }
|
||||
|
||||
# Set deadline and countdown options
|
||||
if ($DeadlineTime -ne $null) { $options.DeadlineTime = $DeadlineTime }
|
||||
if ($ShowCountdown) { $options.ShowCountdown = $ShowCountdown }
|
||||
|
||||
# Set deadline action
|
||||
if ($DeadlineActionCommand) {
|
||||
$options.DeadlineAction = New-Object WindowsNotifications.Models.DeadlineAction($DeadlineActionCommand, $DeadlineActionArguments)
|
||||
}
|
||||
elseif ($DeadlineActionUrl) {
|
||||
$options.DeadlineAction = [WindowsNotifications.Models.DeadlineAction]::OpenUrl($DeadlineActionUrl)
|
||||
}
|
||||
elseif ($DeadlineActionScript) {
|
||||
$options.DeadlineAction = [WindowsNotifications.Models.DeadlineAction]::ExecuteScript($DeadlineActionScript)
|
||||
}
|
||||
|
||||
# Set logging options
|
||||
if ($EnableLogging) { $options.EnableLogging = $true }
|
||||
if ($LogFilePath) {
|
||||
$options.LogAction = {
|
||||
param($logEntry)
|
||||
Add-Content -Path $LogFilePath -Value $logEntry
|
||||
}
|
||||
}
|
||||
|
||||
$result = $script:NotificationManager.ShowNotification($options)
|
||||
}
|
||||
"WithButtons" {
|
||||
# Create custom notification options for notification with buttons
|
||||
$options = New-Object WindowsNotifications.Models.NotificationOptions
|
||||
$options.Title = $Title
|
||||
$options.Message = $Message
|
||||
$options.Async = $Async
|
||||
$options.PersistState = $PersistState
|
||||
$options.TimeoutInSeconds = $TimeoutInSeconds
|
||||
$options.ShowReminder = $ShowReminder
|
||||
$options.ReminderTimeInMinutes = $ReminderTimeInMinutes
|
||||
|
||||
# Set image paths
|
||||
if ($LogoImagePath) { $options.LogoImagePath = $LogoImagePath }
|
||||
if ($HeroImagePath) { $options.HeroImagePath = $HeroImagePath }
|
||||
if ($InlineImagePath) { $options.InlineImagePath = $InlineImagePath }
|
||||
if ($AppIconPath) { $options.AppIconPath = $AppIconPath }
|
||||
if ($BackgroundImagePath) { $options.BackgroundImagePath = $BackgroundImagePath }
|
||||
|
||||
# Set branding options
|
||||
if ($BrandingText) { $options.BrandingText = $BrandingText }
|
||||
if ($BrandingColor) { $options.BrandingColor = $BrandingColor }
|
||||
if ($AccentColor) { $options.AccentColor = $AccentColor }
|
||||
if ($UseDarkTheme) { $options.UseDarkTheme = $UseDarkTheme }
|
||||
|
||||
# Set audio options
|
||||
if ($AudioSource) { $options.AudioSource = $AudioSource }
|
||||
if ($SilentMode) { $options.SilentMode = $SilentMode }
|
||||
|
||||
# Set deadline and countdown options
|
||||
if ($DeadlineTime -ne $null) { $options.DeadlineTime = $DeadlineTime }
|
||||
if ($ShowCountdown) { $options.ShowCountdown = $ShowCountdown }
|
||||
|
||||
# Set deadline action
|
||||
if ($DeadlineActionCommand) {
|
||||
$options.DeadlineAction = New-Object WindowsNotifications.Models.DeadlineAction($DeadlineActionCommand, $DeadlineActionArguments)
|
||||
}
|
||||
elseif ($DeadlineActionUrl) {
|
||||
$options.DeadlineAction = [WindowsNotifications.Models.DeadlineAction]::OpenUrl($DeadlineActionUrl)
|
||||
}
|
||||
elseif ($DeadlineActionScript) {
|
||||
$options.DeadlineAction = [WindowsNotifications.Models.DeadlineAction]::ExecuteScript($DeadlineActionScript)
|
||||
}
|
||||
|
||||
# Set logging options
|
||||
if ($EnableLogging) { $options.EnableLogging = $true }
|
||||
if ($LogFilePath) {
|
||||
$options.LogAction = {
|
||||
param($logEntry)
|
||||
Add-Content -Path $LogFilePath -Value $logEntry
|
||||
}
|
||||
}
|
||||
|
||||
# Add buttons
|
||||
foreach ($buttonText in $Buttons) {
|
||||
$button = New-Object WindowsNotifications.Models.NotificationButton($buttonText)
|
||||
$options.Buttons.Add($button)
|
||||
}
|
||||
|
||||
$result = $script:NotificationManager.ShowNotification($options)
|
||||
}
|
||||
"Reboot" {
|
||||
# Use the built-in reboot notification method
|
||||
$result = $script:NotificationManager.ShowRebootNotification($Title, $Message, $RebootButtonText, $DeferButtonText)
|
||||
}
|
||||
}
|
||||
|
||||
return $result
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to show notification: $_"
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
function Get-NotificationResult {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$NotificationId
|
||||
)
|
||||
|
||||
# Ensure the notification assembly is initialized
|
||||
if (-not $script:NotificationManager) {
|
||||
if (-not (Initialize-WindowsNotifications)) {
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return $script:NotificationManager.GetNotificationResult($NotificationId)
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to get notification result: $_"
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
function Wait-Notification {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$NotificationId,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[int]$TimeoutInSeconds = -1
|
||||
)
|
||||
|
||||
# Ensure the notification assembly is initialized
|
||||
if (-not $script:NotificationManager) {
|
||||
if (-not (Initialize-WindowsNotifications)) {
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$timeoutMs = $TimeoutInSeconds -eq -1 ? -1 : ($TimeoutInSeconds * 1000)
|
||||
return $script:NotificationManager.WaitForNotification($NotificationId, $timeoutMs)
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to wait for notification: $_"
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
function Get-NotificationHistory {
|
||||
[CmdletBinding()]
|
||||
param ()
|
||||
|
||||
# Ensure the notification assembly is initialized
|
||||
if (-not $script:NotificationManager) {
|
||||
if (-not (Initialize-WindowsNotifications)) {
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return $script:NotificationManager.GetAllNotificationResults()
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to get notification history: $_"
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
function Remove-NotificationHistory {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$NotificationId
|
||||
)
|
||||
|
||||
# Ensure the notification assembly is initialized
|
||||
if (-not $script:NotificationManager) {
|
||||
if (-not (Initialize-WindowsNotifications)) {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if ($NotificationId) {
|
||||
return $script:NotificationManager.DeleteNotificationResult($NotificationId)
|
||||
}
|
||||
else {
|
||||
return $script:NotificationManager.DeleteAllNotificationResults()
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to remove notification history: $_"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Get-InteractiveUserSessions {
|
||||
[CmdletBinding()]
|
||||
param ()
|
||||
|
||||
# Ensure the notification assembly is initialized
|
||||
if (-not $script:NotificationManager) {
|
||||
if (-not (Initialize-WindowsNotifications)) {
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return $script:NotificationManager.GetInteractiveUserSessions()
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to get interactive user sessions: $_"
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
function Test-SystemContext {
|
||||
[CmdletBinding()]
|
||||
param ()
|
||||
|
||||
# Ensure the notification assembly is initialized
|
||||
if (-not $script:NotificationManager) {
|
||||
if (-not (Initialize-WindowsNotifications)) {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return $script:NotificationManager.IsRunningAsSystem()
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to check system context: $_"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
# Export public functions
|
||||
Export-ModuleMember -Function Initialize-WindowsNotifications, Show-Notification, Get-NotificationResult, Wait-Notification, Get-NotificationHistory, Remove-NotificationHistory, Get-InteractiveUserSessions, Test-SystemContext
|
||||
Reference in New Issue
Block a user