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,84 @@
|
||||
# Example: Asynchronous Notification
|
||||
# This script demonstrates how to show an asynchronous notification and check its status later
|
||||
|
||||
# Load the WindowsNotifications assembly
|
||||
$dllPath = Join-Path -Path $PSScriptRoot -ChildPath "..\WindowsNotifications\bin\Release\WindowsNotifications.dll"
|
||||
if (Test-Path $dllPath) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($dllPath)
|
||||
$assembly = [System.Reflection.Assembly]::Load($bytes)
|
||||
}
|
||||
else {
|
||||
Write-Error "WindowsNotifications.dll not found at $dllPath. Please build the solution first."
|
||||
exit
|
||||
}
|
||||
|
||||
# Create a notification manager
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
|
||||
# Create custom notification options
|
||||
$options = New-Object WindowsNotifications.Models.NotificationOptions
|
||||
$options.Title = "Background Task Running"
|
||||
$options.Message = "A background task is running. You can continue working."
|
||||
$options.Async = $true # Run asynchronously
|
||||
$options.PersistState = $true # Save state to database
|
||||
|
||||
# Add buttons
|
||||
$viewButton = New-Object WindowsNotifications.Models.NotificationButton("View Progress", "view")
|
||||
$cancelButton = New-Object WindowsNotifications.Models.NotificationButton("Cancel Task", "cancel")
|
||||
$options.Buttons.Add($viewButton)
|
||||
$options.Buttons.Add($cancelButton)
|
||||
|
||||
# Show the notification
|
||||
Write-Host "Showing asynchronous notification..."
|
||||
$result = $notificationManager.ShowNotification($options)
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# Simulate doing some work
|
||||
Write-Host "Performing background 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 = $notificationManager.GetNotificationResult($result.NotificationId)
|
||||
|
||||
if ($currentResult -ne $null -and ($currentResult.Activated -or $currentResult.Dismissed)) {
|
||||
if ($currentResult.ClickedButtonId -eq "cancel") {
|
||||
Write-Host "User canceled the task!"
|
||||
break
|
||||
}
|
||||
elseif ($currentResult.ClickedButtonId -eq "view") {
|
||||
Write-Host "User viewed the progress!"
|
||||
|
||||
# Show a new notification with updated progress
|
||||
$progressOptions = New-Object WindowsNotifications.Models.NotificationOptions
|
||||
$progressOptions.Title = "Task Progress"
|
||||
$progressOptions.Message = "Progress: $i/5 steps completed"
|
||||
$progressOptions.Tag = "progress" # Group with the same tag
|
||||
$notificationManager.ShowNotification($progressOptions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Background work completed"
|
||||
|
||||
# Show a completion notification
|
||||
$completionOptions = New-Object WindowsNotifications.Models.NotificationOptions
|
||||
$completionOptions.Title = "Task Completed"
|
||||
$completionOptions.Message = "The background task has finished."
|
||||
$completionOptions.Tag = "progress" # Replace previous notification with same tag
|
||||
$notificationManager.ShowNotification($completionOptions)
|
||||
|
||||
# Get the final notification result
|
||||
$finalResult = $notificationManager.GetNotificationResult($result.NotificationId)
|
||||
if ($finalResult -ne $null) {
|
||||
Write-Host "Final notification state:"
|
||||
Write-Host " Activated: $($finalResult.Activated)"
|
||||
Write-Host " Dismissed: $($finalResult.Dismissed)"
|
||||
if ($finalResult.ClickedButtonId) {
|
||||
Write-Host " Button clicked: $($finalResult.ClickedButtonText)"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
# Example: Countdown and Deadline Notification
|
||||
# This script demonstrates how to create a notification with a countdown timer and deadline action
|
||||
|
||||
# Load the WindowsNotifications assembly
|
||||
$dllPath = Join-Path -Path $PSScriptRoot -ChildPath "..\WindowsNotifications\bin\Release\WindowsNotifications.dll"
|
||||
if (Test-Path $dllPath) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($dllPath)
|
||||
$assembly = [System.Reflection.Assembly]::Load($bytes)
|
||||
}
|
||||
else {
|
||||
Write-Error "WindowsNotifications.dll not found at $dllPath. Please build the solution first."
|
||||
exit
|
||||
}
|
||||
|
||||
# Create a notification manager
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
|
||||
# Create custom notification options
|
||||
$options = New-Object WindowsNotifications.Models.NotificationOptions
|
||||
$options.Title = "System Maintenance Required"
|
||||
$options.Message = "Your system needs to restart for maintenance. Please save your work."
|
||||
$options.PersistState = $true
|
||||
$options.EnableLogging = $true
|
||||
|
||||
# Set up logging
|
||||
$logFile = Join-Path -Path $PSScriptRoot -ChildPath "notification_log.txt"
|
||||
$options.LogAction = {
|
||||
param($logEntry)
|
||||
Add-Content -Path $logFile -Value $logEntry
|
||||
Write-Host $logEntry
|
||||
}
|
||||
|
||||
# Set deadline (5 minutes from now)
|
||||
$options.DeadlineTime = (Get-Date).AddMinutes(5)
|
||||
$options.ShowCountdown = $true
|
||||
|
||||
# Create deadline action (restart computer)
|
||||
$deadlineAction = [WindowsNotifications.Models.DeadlineAction]::ExecuteScript("Write-Host 'System would restart now (simulated)'; Start-Sleep -Seconds 5")
|
||||
$options.DeadlineAction = $deadlineAction
|
||||
|
||||
# Add buttons
|
||||
$restartButton = New-Object WindowsNotifications.Models.NotificationButton("Restart Now", "restart")
|
||||
$deferButton = New-Object WindowsNotifications.Models.NotificationButton("Defer", "defer")
|
||||
$options.Buttons.Add($restartButton)
|
||||
$options.Buttons.Add($deferButton)
|
||||
|
||||
# Configure deferral options
|
||||
$options.DeferralOptions = New-Object WindowsNotifications.Models.DeferralOptions
|
||||
$options.DeferralOptions.DeferButtonText = "Defer Restart"
|
||||
$options.DeferralOptions.DeferralPrompt = "Postpone until:"
|
||||
$options.DeferralOptions.MaxDeferrals = 2
|
||||
$options.DeferralOptions.EnforceMaxDeferrals = $true
|
||||
$options.DeferralOptions.ScheduleReminder = $true
|
||||
|
||||
# Clear existing deferral choices and add custom ones
|
||||
$options.DeferralOptions.DeferralChoices.Clear()
|
||||
$options.DeferralOptions.DeferralChoices.Add((New-Object WindowsNotifications.Models.DeferralOption("1 minute", [TimeSpan]::FromMinutes(1), "1min")))
|
||||
$options.DeferralOptions.DeferralChoices.Add((New-Object WindowsNotifications.Models.DeferralOption("2 minutes", [TimeSpan]::FromMinutes(2), "2min")))
|
||||
$options.DeferralOptions.DeferralChoices.Add((New-Object WindowsNotifications.Models.DeferralOption("3 minutes", [TimeSpan]::FromMinutes(3), "3min")))
|
||||
|
||||
# Set up event handlers
|
||||
$options.OnActivated = {
|
||||
param($result)
|
||||
$message = "Notification was activated with button: $($result.ClickedButtonText)"
|
||||
Write-Host $message
|
||||
Add-Content -Path $logFile -Value $message
|
||||
|
||||
if ($result.ClickedButtonId -eq "restart") {
|
||||
Write-Host "User chose to restart now (simulated)"
|
||||
# In a real script, you would restart the computer here
|
||||
# Restart-Computer -Force
|
||||
}
|
||||
}
|
||||
|
||||
$options.OnTimeout = {
|
||||
param($result)
|
||||
$message = "Notification timed out"
|
||||
Write-Host $message
|
||||
Add-Content -Path $logFile -Value $message
|
||||
}
|
||||
|
||||
$options.OnError = {
|
||||
param($result)
|
||||
$message = "Error occurred: $($result.ErrorMessage)"
|
||||
Write-Host $message -ForegroundColor Red
|
||||
Add-Content -Path $logFile -Value $message
|
||||
}
|
||||
|
||||
# Show the notification
|
||||
Write-Host "Showing countdown notification with deadline..."
|
||||
$result = $notificationManager.ShowNotification($options)
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# Wait for the deadline to pass
|
||||
Write-Host "Waiting for user interaction or deadline to pass..."
|
||||
$waitTime = [int]([Math]::Ceiling(($options.DeadlineTime - (Get-Date)).TotalSeconds)) + 5
|
||||
Start-Sleep -Seconds $waitTime
|
||||
|
||||
# Get the final result
|
||||
$finalResult = $notificationManager.GetNotificationResult($result.NotificationId)
|
||||
if ($finalResult -ne $null) {
|
||||
Write-Host "`nFinal notification state:"
|
||||
Write-Host " Activated: $($finalResult.Activated)"
|
||||
Write-Host " Dismissed: $($finalResult.Dismissed)"
|
||||
Write-Host " Deferred: $($finalResult.Deferred)"
|
||||
|
||||
if ($finalResult.Deferred) {
|
||||
Write-Host " Deferred until: $($finalResult.DeferredUntil)"
|
||||
Write-Host " Deferral reason: $($finalResult.DeferralReason)"
|
||||
}
|
||||
|
||||
if ($finalResult.DeadlineReached) {
|
||||
Write-Host " Deadline reached at: $($finalResult.DeadlineReachedTime)"
|
||||
Write-Host " Deadline action: $($finalResult.DeadlineAction)"
|
||||
}
|
||||
|
||||
if ($finalResult.ClickedButtonId) {
|
||||
Write-Host " Button clicked: $($finalResult.ClickedButtonText)"
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "`nLog file: $logFile"
|
||||
@@ -0,0 +1,105 @@
|
||||
# Example: Custom Branded Notification
|
||||
# This script demonstrates how to create a notification with custom branding
|
||||
|
||||
# Load the WindowsNotifications assembly
|
||||
$dllPath = Join-Path -Path $PSScriptRoot -ChildPath "..\WindowsNotifications\bin\Release\WindowsNotifications.dll"
|
||||
if (Test-Path $dllPath) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($dllPath)
|
||||
$assembly = [System.Reflection.Assembly]::Load($bytes)
|
||||
}
|
||||
else {
|
||||
Write-Error "WindowsNotifications.dll not found at $dllPath. Please build the solution first."
|
||||
exit
|
||||
}
|
||||
|
||||
# Create a notification manager
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
|
||||
# Create custom notification options with branding
|
||||
$options = New-Object WindowsNotifications.Models.NotificationOptions
|
||||
|
||||
# Set basic notification properties
|
||||
$options.Title = "IT Department Notification"
|
||||
$options.Message = "Your system has been selected for a security update. Please review the details below."
|
||||
|
||||
# Set branding properties
|
||||
$options.BrandingText = "Contoso IT Department"
|
||||
$options.BrandingColor = "#0078D7" # Blue
|
||||
$options.AccentColor = "#E81123" # Red
|
||||
$options.UseDarkTheme = $true
|
||||
|
||||
# Set custom images
|
||||
# Note: Replace these paths with actual image paths on your system
|
||||
$logoPath = Join-Path -Path $PSScriptRoot -ChildPath "Images\company-logo.png"
|
||||
if (Test-Path $logoPath) {
|
||||
$options.LogoImagePath = $logoPath
|
||||
}
|
||||
else {
|
||||
# Use a URL as fallback
|
||||
$options.LogoImagePath = "https://www.contoso.com/images/logo.png"
|
||||
}
|
||||
|
||||
$heroPath = Join-Path -Path $PSScriptRoot -ChildPath "Images\banner.png"
|
||||
if (Test-Path $heroPath) {
|
||||
$options.HeroImagePath = $heroPath
|
||||
}
|
||||
|
||||
# Add custom buttons with images
|
||||
$updateButton = New-Object WindowsNotifications.Models.NotificationButton("Install Update", "install")
|
||||
$updateButton.BackgroundColor = "#107C10" # Green
|
||||
$updateButton.TextColor = "#FFFFFF" # White
|
||||
|
||||
$deferButton = New-Object WindowsNotifications.Models.NotificationButton("Defer", "defer")
|
||||
$deferButton.BackgroundColor = "#5A5A5A" # Gray
|
||||
$deferButton.TextColor = "#FFFFFF" # White
|
||||
|
||||
$moreInfoButton = New-Object WindowsNotifications.Models.NotificationButton("More Info", "info")
|
||||
$moreInfoButton.IsContextMenu = $true
|
||||
|
||||
# Add buttons to the notification
|
||||
$options.Buttons.Add($updateButton)
|
||||
$options.Buttons.Add($deferButton)
|
||||
$options.Buttons.Add($moreInfoButton)
|
||||
|
||||
# Configure audio
|
||||
$options.AudioSource = "ms-winsoundevent:Notification.Default"
|
||||
$options.SilentMode = $false
|
||||
|
||||
# Show the notification
|
||||
Write-Host "Showing custom branded notification..."
|
||||
$result = $notificationManager.ShowNotification($options)
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# If the notification was displayed, wait for interaction
|
||||
if ($result.Displayed) {
|
||||
Write-Host "Waiting for user interaction..."
|
||||
$result = $notificationManager.WaitForNotification($result.NotificationId)
|
||||
|
||||
if ($result -ne $null) {
|
||||
Write-Host "Notification was activated: $($result.Activated)"
|
||||
Write-Host "Notification was dismissed: $($result.Dismissed)"
|
||||
|
||||
if ($result.ClickedButtonId) {
|
||||
Write-Host "Button clicked: $($result.ClickedButtonText) (ID: $($result.ClickedButtonId))"
|
||||
|
||||
# Take action based on which button was clicked
|
||||
switch ($result.ClickedButtonId) {
|
||||
"install" {
|
||||
Write-Host "User chose to install the update"
|
||||
# In a real script, you would initiate the update here
|
||||
}
|
||||
"defer" {
|
||||
Write-Host "User chose to defer the update"
|
||||
# In a real script, you would schedule a reminder here
|
||||
}
|
||||
"info" {
|
||||
Write-Host "User requested more information"
|
||||
# In a real script, you would open a help page or display more details
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
# Example: Load Assembly from Base64
|
||||
# This script demonstrates how to load the WindowsNotifications assembly from a Base64 string
|
||||
# This is useful for embedding the assembly directly in a script
|
||||
|
||||
# In a real scenario, you would replace this with the actual Base64 string of the assembly
|
||||
# For demonstration purposes, we'll load the DLL and convert it to Base64
|
||||
$dllPath = Join-Path -Path $PSScriptRoot -ChildPath "..\WindowsNotifications\bin\Release\WindowsNotifications.dll"
|
||||
if (Test-Path $dllPath) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($dllPath)
|
||||
$base64 = [Convert]::ToBase64String($bytes)
|
||||
|
||||
Write-Host "Assembly loaded and converted to Base64"
|
||||
Write-Host "Base64 string length: $($base64.Length) characters"
|
||||
|
||||
# In a real script, the base64 string would be hardcoded here
|
||||
# $base64 = "YOUR_BASE64_STRING_HERE"
|
||||
|
||||
# Load the assembly from the Base64 string
|
||||
$assemblyBytes = [Convert]::FromBase64String($base64)
|
||||
$assembly = [System.Reflection.Assembly]::Load($assemblyBytes)
|
||||
|
||||
Write-Host "Assembly loaded successfully: $($assembly.FullName)"
|
||||
|
||||
# Create a notification manager and show a simple notification
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
$result = $notificationManager.ShowSimpleNotification(
|
||||
"Loaded from Base64",
|
||||
"This notification was shown from an assembly loaded from a Base64 string"
|
||||
)
|
||||
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
}
|
||||
else {
|
||||
Write-Error "WindowsNotifications.dll not found at $dllPath. Please build the solution first."
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
# Example: Notification with Buttons
|
||||
# This script demonstrates how to show a notification with buttons
|
||||
|
||||
# Load the WindowsNotifications assembly
|
||||
$dllPath = Join-Path -Path $PSScriptRoot -ChildPath "..\WindowsNotifications\bin\Release\WindowsNotifications.dll"
|
||||
if (Test-Path $dllPath) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($dllPath)
|
||||
$assembly = [System.Reflection.Assembly]::Load($bytes)
|
||||
}
|
||||
else {
|
||||
Write-Error "WindowsNotifications.dll not found at $dllPath. Please build the solution first."
|
||||
exit
|
||||
}
|
||||
|
||||
# Create a notification manager
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
|
||||
# Show a notification with buttons
|
||||
Write-Host "Showing notification with buttons..."
|
||||
$result = $notificationManager.ShowNotificationWithButtons(
|
||||
"Action Required",
|
||||
"Please select an option below:",
|
||||
"Approve",
|
||||
"Reject",
|
||||
"Defer"
|
||||
)
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# If the notification was displayed, wait for interaction
|
||||
if ($result.Displayed) {
|
||||
Write-Host "Waiting for user interaction..."
|
||||
$result = $notificationManager.WaitForNotification($result.NotificationId)
|
||||
|
||||
if ($result -ne $null) {
|
||||
Write-Host "Notification was activated: $($result.Activated)"
|
||||
Write-Host "Notification was dismissed: $($result.Dismissed)"
|
||||
|
||||
if ($result.ClickedButtonId) {
|
||||
Write-Host "Button clicked: $($result.ClickedButtonText) (ID: $($result.ClickedButtonId))"
|
||||
|
||||
# Take action based on which button was clicked
|
||||
switch ($result.ClickedButtonText) {
|
||||
"Approve" {
|
||||
Write-Host "User approved the action"
|
||||
}
|
||||
"Reject" {
|
||||
Write-Host "User rejected the action"
|
||||
}
|
||||
"Defer" {
|
||||
Write-Host "User deferred the action"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
# Example: Using the WindowsNotifications PowerShell Module
|
||||
# This script demonstrates how to use the WindowsNotifications PowerShell module
|
||||
|
||||
# Import the module
|
||||
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath "..\PowerShell"
|
||||
Import-Module $modulePath -Force
|
||||
|
||||
# Initialize the module
|
||||
# Note: The DLL must be in the same directory as the module files
|
||||
$dllPath = Join-Path -Path $PSScriptRoot -ChildPath "..\WindowsNotifications\bin\Release\WindowsNotifications.dll"
|
||||
if (Test-Path $dllPath) {
|
||||
Write-Host "Initializing WindowsNotifications module with DLL at: $dllPath"
|
||||
Initialize-WindowsNotifications -AssemblyPath $dllPath
|
||||
}
|
||||
else {
|
||||
Write-Error "WindowsNotifications.dll not found at $dllPath. Please build the solution first."
|
||||
exit
|
||||
}
|
||||
|
||||
# Check if running as SYSTEM
|
||||
$isSystem = Test-SystemContext
|
||||
Write-Host "Running as SYSTEM: $isSystem"
|
||||
|
||||
# Get interactive user sessions
|
||||
$sessions = Get-InteractiveUserSessions
|
||||
Write-Host "Interactive user sessions found: $($sessions.Count)"
|
||||
foreach ($session in $sessions) {
|
||||
Write-Host " $session"
|
||||
}
|
||||
|
||||
# Show a simple notification
|
||||
Write-Host "`nShowing simple notification..."
|
||||
$result = Show-Notification -Title "Simple Notification" -Message "This is a simple notification from the PowerShell module"
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# Show a notification with buttons
|
||||
Write-Host "`nShowing notification with buttons..."
|
||||
$result = Show-Notification -Title "Action Required" -Message "Please select an option:" -Buttons "Approve", "Reject", "Defer"
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
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" }
|
||||
}
|
||||
}
|
||||
|
||||
# Show a reboot notification
|
||||
Write-Host "`nShowing reboot notification..."
|
||||
$result = Show-Notification -Title "System Reboot Required" -Message "Your system needs to be rebooted to complete updates." -RebootButtonText "Reboot Now" -DeferButtonText "Defer"
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
if ($result.ClickedButtonId -eq "reboot") {
|
||||
Write-Host "User chose to reboot now"
|
||||
# In a real script, you would initiate a reboot here
|
||||
# Restart-Computer -Force
|
||||
}
|
||||
elseif ($result.Deferred) {
|
||||
Write-Host "User deferred the reboot until: $($result.DeferredUntil)"
|
||||
Write-Host "Deferral reason: $($result.DeferralReason)"
|
||||
}
|
||||
|
||||
# Show an asynchronous notification
|
||||
Write-Host "`nShowing asynchronous notification..."
|
||||
$result = Show-Notification -Title "Background Task" -Message "A background task is running" -Async -PersistState
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# Simulate doing some work
|
||||
Write-Host "Performing background 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 -ne $null -and ($currentResult.Activated -or $currentResult.Dismissed)) {
|
||||
if ($currentResult.ClickedButtonId) {
|
||||
Write-Host "User clicked: $($currentResult.ClickedButtonText)"
|
||||
}
|
||||
else {
|
||||
Write-Host "User interacted with the notification"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Background work completed"
|
||||
|
||||
# Get notification history
|
||||
Write-Host "`nGetting notification history..."
|
||||
$history = Get-NotificationHistory
|
||||
Write-Host "Found $($history.Count) notifications in history"
|
||||
|
||||
foreach ($item in $history) {
|
||||
Write-Host " ID: $($item.NotificationId), Created: $($item.CreatedTime), Activated: $($item.Activated), Dismissed: $($item.Dismissed)"
|
||||
}
|
||||
|
||||
# Clean up notification history
|
||||
Write-Host "`nCleaning up notification history..."
|
||||
$result = Remove-NotificationHistory
|
||||
Write-Host "Notification history cleared: $result"
|
||||
@@ -0,0 +1,68 @@
|
||||
# Example: Reboot Notification with Deferrals
|
||||
# This script demonstrates how to show a reboot notification with deferral options
|
||||
|
||||
# Load the WindowsNotifications assembly
|
||||
$dllPath = Join-Path -Path $PSScriptRoot -ChildPath "..\WindowsNotifications\bin\Release\WindowsNotifications.dll"
|
||||
if (Test-Path $dllPath) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($dllPath)
|
||||
$assembly = [System.Reflection.Assembly]::Load($bytes)
|
||||
}
|
||||
else {
|
||||
Write-Error "WindowsNotifications.dll not found at $dllPath. Please build the solution first."
|
||||
exit
|
||||
}
|
||||
|
||||
# Create a notification manager
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
|
||||
# Create custom notification options for a reboot
|
||||
$options = New-Object WindowsNotifications.Models.NotificationOptions
|
||||
$options.Title = "System Reboot Required"
|
||||
$options.Message = "Your system needs to be rebooted to complete important updates. Please save your work."
|
||||
$options.PersistState = $true
|
||||
|
||||
# Add reboot button
|
||||
$rebootButton = New-Object WindowsNotifications.Models.NotificationButton("Reboot Now", "reboot")
|
||||
$options.Buttons.Add($rebootButton)
|
||||
|
||||
# Configure deferral options
|
||||
$options.DeferralOptions = New-Object WindowsNotifications.Models.DeferralOptions
|
||||
$options.DeferralOptions.DeferButtonText = "Defer Reboot"
|
||||
$options.DeferralOptions.DeferralPrompt = "Postpone until:"
|
||||
$options.DeferralOptions.MaxDeferrals = 3
|
||||
|
||||
# Clear existing deferral choices and add custom ones
|
||||
$options.DeferralOptions.DeferralChoices.Clear()
|
||||
$options.DeferralOptions.DeferralChoices.Add((New-Object WindowsNotifications.Models.DeferralOption("1 hour", [TimeSpan]::FromHours(1), "1hour")))
|
||||
$options.DeferralOptions.DeferralChoices.Add((New-Object WindowsNotifications.Models.DeferralOption("4 hours", [TimeSpan]::FromHours(4), "4hours")))
|
||||
$options.DeferralOptions.DeferralChoices.Add((New-Object WindowsNotifications.Models.DeferralOption("End of day", [TimeSpan]::FromHours(8), "endofday")))
|
||||
$options.DeferralOptions.DeferralChoices.Add((New-Object WindowsNotifications.Models.DeferralOption("Tomorrow", [TimeSpan]::FromDays(1), "tomorrow")))
|
||||
|
||||
# Show the notification
|
||||
Write-Host "Showing reboot notification..."
|
||||
$result = $notificationManager.ShowNotification($options)
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# If the notification was displayed, wait for interaction
|
||||
if ($result.Displayed) {
|
||||
Write-Host "Waiting for user interaction..."
|
||||
$result = $notificationManager.WaitForNotification($result.NotificationId)
|
||||
|
||||
if ($result -ne $null) {
|
||||
Write-Host "Notification was activated: $($result.Activated)"
|
||||
Write-Host "Notification was dismissed: $($result.Dismissed)"
|
||||
|
||||
if ($result.ClickedButtonId -eq "reboot") {
|
||||
Write-Host "User chose to reboot now"
|
||||
# In a real script, you would initiate a reboot here
|
||||
# Restart-Computer -Force
|
||||
}
|
||||
elseif ($result.Deferred) {
|
||||
Write-Host "User deferred the reboot until: $($result.DeferredUntil)"
|
||||
Write-Host "Deferral reason: $($result.DeferralReason)"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
# Example: Simple Notification
|
||||
# This script demonstrates how to show a simple notification
|
||||
|
||||
# Load the WindowsNotifications assembly
|
||||
$dllPath = Join-Path -Path $PSScriptRoot -ChildPath "..\WindowsNotifications\bin\Release\WindowsNotifications.dll"
|
||||
if (Test-Path $dllPath) {
|
||||
$bytes = [System.IO.File]::ReadAllBytes($dllPath)
|
||||
$assembly = [System.Reflection.Assembly]::Load($bytes)
|
||||
}
|
||||
else {
|
||||
Write-Error "WindowsNotifications.dll not found at $dllPath. Please build the solution first."
|
||||
exit
|
||||
}
|
||||
|
||||
# Create a notification manager
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
|
||||
# Check if running as SYSTEM
|
||||
$isSystem = $notificationManager.IsRunningAsSystem()
|
||||
Write-Host "Running as SYSTEM: $isSystem"
|
||||
|
||||
# Get interactive user sessions
|
||||
$sessions = $notificationManager.GetInteractiveUserSessions()
|
||||
Write-Host "Interactive user sessions found: $($sessions.Count)"
|
||||
foreach ($session in $sessions) {
|
||||
Write-Host " $session"
|
||||
}
|
||||
|
||||
# Show a simple notification
|
||||
Write-Host "Showing simple notification..."
|
||||
$result = $notificationManager.ShowSimpleNotification("Simple Notification", "This is a simple notification from PowerShell")
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# If the notification was displayed, wait for interaction
|
||||
if ($result.Displayed) {
|
||||
Write-Host "Waiting for user interaction..."
|
||||
$result = $notificationManager.WaitForNotification($result.NotificationId, 30000)
|
||||
|
||||
if ($result -ne $null) {
|
||||
Write-Host "Notification was activated: $($result.Activated)"
|
||||
Write-Host "Notification was dismissed: $($result.Dismissed)"
|
||||
}
|
||||
else {
|
||||
Write-Host "Timed out waiting for user interaction"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user