mirror of
https://github.com/freedbygrace/WindowsNotifications.git
synced 2026-07-26 12:08:14 +00:00
3.7 KiB
3.7 KiB
Windows Notifications PowerShell Module
This PowerShell module provides a simple interface to the Windows Notifications library, allowing you to display notifications in user context from SYSTEM.
Installation
-
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)
-
Make sure the
WindowsNotifications.dllfile is in the same directory as the module files. -
Import the module:
Import-Module WindowsNotifications
Usage
Initializing the Module
# Initialize with default settings
Initialize-WindowsNotifications
# Initialize with custom database path
Initialize-WindowsNotifications -DatabasePath "C:\Path\To\Notifications.db"
Showing Notifications
Simple Notification
Show-Notification -Title "Hello" -Message "This is a simple notification"
Notification with Buttons
$result = Show-Notification -Title "Action Required" -Message "Please select an option:" -Buttons "Approve", "Reject", "Defer"
# Check which button was clicked
if ($result.ClickedButtonId) {
Write-Host "Button clicked: $($result.ClickedButtonText)"
}
Reboot Notification
$result = Show-Notification -Title "System Reboot Required" -Message "Your system needs to be rebooted." -RebootButtonText "Reboot Now" -DeferButtonText "Defer"
# Check if the reboot button was clicked
if ($result.ClickedButtonId -eq "reboot") {
# Reboot the system
Restart-Computer -Force
}
Asynchronous Notification
$result = Show-Notification -Title "Background Task" -Message "A background task is running..." -Async
# Do some work
# ...
# Check if the notification has been interacted with
$currentResult = Get-NotificationResult -NotificationId $result.NotificationId
if ($currentResult.Activated) {
Write-Host "User clicked the notification"
}
Notification with Deadline
$deadline = (Get-Date).AddMinutes(5)
$result = Show-Notification -Title "System Maintenance" -Message "Your system needs maintenance." -DeadlineTime $deadline -ShowCountdown
# Check if the deadline was reached
if ($result.DeadlineReached) {
Write-Host "Deadline reached at $($result.DeadlineReachedTime)"
}
Managing Notification Results
# Get a specific notification result
$result = Get-NotificationResult -NotificationId "12345678-1234-1234-1234-123456789012"
# Wait for a notification to complete
$result = Wait-Notification -NotificationId "12345678-1234-1234-1234-123456789012" -TimeoutInSeconds 30
# Get all notification results
$results = Get-AllNotificationResults
# Remove a notification result
Remove-NotificationResult -NotificationId "12345678-1234-1234-1234-123456789012"
# Clear all notification results
Clear-AllNotificationResults
Utility Functions
# Check if running as SYSTEM
$isSystem = Test-SystemContext
# Get interactive user sessions
$sessions = Get-InteractiveUserSessions
Functions
Initialize-WindowsNotifications- Initializes the Windows Notifications moduleShow-Notification- Shows a notificationGet-NotificationResult- Gets the result of a notificationWait-Notification- Waits for a notification to completeGet-AllNotificationResults- Gets all notification resultsRemove-NotificationResult- Removes a notification resultClear-AllNotificationResults- Clears all notification resultsTest-SystemContext- Checks if running as SYSTEMGet-InteractiveUserSessions- Gets interactive user sessions