mirror of
https://github.com/freedbygrace/WindowsNotifications.git
synced 2026-08-12 11:56:54 +00:00
Add PowerShell cmdlets and update project structure
This commit is contained in:
@@ -1,125 +1,44 @@
|
||||
# Example: Countdown and Deadline Notification
|
||||
# This script demonstrates how to create a notification with a countdown timer and deadline action
|
||||
# This script demonstrates how to show a notification with a countdown timer and deadline
|
||||
|
||||
# Load the WindowsNotifications assembly
|
||||
# Import the module
|
||||
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath "..\PowerShell\WindowsNotifications.psd1"
|
||||
Import-Module $modulePath -Force
|
||||
|
||||
# Initialize the notification system
|
||||
$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
|
||||
}
|
||||
Initialize-WindowsNotifications -DllPath $dllPath
|
||||
|
||||
# Create a notification manager
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
# Set a deadline 2 minutes from now
|
||||
$deadline = (Get-Date).AddMinutes(2)
|
||||
|
||||
# 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)
|
||||
# Show a countdown notification
|
||||
$result = Show-Notification -Title "System Maintenance Required" -Message "Your system needs to restart for maintenance. Please save your work." -DeadlineTime $deadline -ShowCountdown -Buttons "Restart Now", "Remind Me Later"
|
||||
|
||||
# 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)"
|
||||
# If the notification was interacted with, show the details
|
||||
if ($result.Activated) {
|
||||
Write-Host "Notification was activated"
|
||||
if ($result.ClickedButtonId) {
|
||||
Write-Host "Button clicked: $($result.ClickedButtonText) (ID: $($result.ClickedButtonId))"
|
||||
|
||||
# Handle button clicks
|
||||
if ($result.ClickedButtonText -eq "Restart Now") {
|
||||
Write-Host "User chose to restart now (simulated)"
|
||||
# In a real script, you would restart the computer here
|
||||
# Restart-Computer -Force
|
||||
} elseif ($result.ClickedButtonText -eq "Remind Me Later") {
|
||||
Write-Host "User chose to be reminded later"
|
||||
# In a real script, you would schedule a reminder here
|
||||
}
|
||||
}
|
||||
} elseif ($result.Dismissed) {
|
||||
Write-Host "Notification was dismissed"
|
||||
} elseif ($result.DeadlineReached) {
|
||||
Write-Host "Deadline was reached at $($result.DeadlineReachedTime)"
|
||||
Write-Host "Taking automatic action (simulated)"
|
||||
# In a real script, you would take the automatic action here
|
||||
}
|
||||
|
||||
Write-Host "`nLog file: $logFile"
|
||||
|
||||
Reference in New Issue
Block a user