mirror of
https://github.com/freedbygrace/WindowsNotifications.git
synced 2026-07-27 04:20:24 +00:00
Add PowerShell cmdlets and update project structure
This commit is contained in:
@@ -1,84 +1,60 @@
|
||||
# Example: Asynchronous Notification
|
||||
# This script demonstrates how to show an asynchronous notification and check its status later
|
||||
# This script demonstrates how to show an asynchronous notification
|
||||
|
||||
# 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
|
||||
# Show an asynchronous notification
|
||||
$result = Show-Notification -Title "Background Task" -Message "A background task is running..." -Buttons "Cancel", "View Details" -Async
|
||||
|
||||
# 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
|
||||
# Display the initial result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# Simulate doing some work
|
||||
Write-Host "Performing background work..."
|
||||
# Simulate a background task
|
||||
Write-Host "Performing background task..."
|
||||
for ($i = 1; $i -le 5; $i++) {
|
||||
Write-Host " Working... ($i/5)"
|
||||
Write-Host "Working... ($i/5)"
|
||||
Start-Sleep -Seconds 2
|
||||
|
||||
# Check if the notification has been interacted with
|
||||
$currentResult = $notificationManager.GetNotificationResult($result.NotificationId)
|
||||
$currentResult = Get-NotificationResult -NotificationId $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!"
|
||||
if ($currentResult -and ($currentResult.Activated -or $currentResult.Dismissed)) {
|
||||
Write-Host "User interacted with the notification"
|
||||
|
||||
if ($currentResult.ClickedButtonId) {
|
||||
Write-Host "Button clicked: $($currentResult.ClickedButtonText) (ID: $($currentResult.ClickedButtonId))"
|
||||
|
||||
# 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)
|
||||
# Handle button clicks
|
||||
if ($currentResult.ClickedButtonText -eq "Cancel") {
|
||||
Write-Host "User cancelled the task"
|
||||
break
|
||||
} elseif ($currentResult.ClickedButtonText -eq "View Details") {
|
||||
Write-Host "User wants to view details"
|
||||
# In a real script, you would show details here
|
||||
}
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Background work completed"
|
||||
Write-Host "Background task 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) {
|
||||
# Get the final result
|
||||
$finalResult = Get-NotificationResult -NotificationId $result.NotificationId
|
||||
if ($finalResult) {
|
||||
Write-Host "Final notification state:"
|
||||
Write-Host " Activated: $($finalResult.Activated)"
|
||||
Write-Host " Dismissed: $($finalResult.Dismissed)"
|
||||
|
||||
if ($finalResult.ClickedButtonId) {
|
||||
Write-Host " Button clicked: $($finalResult.ClickedButtonText)"
|
||||
Write-Host " Button clicked: $($finalResult.ClickedButtonText) (ID: $($finalResult.ClickedButtonId))"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -1,105 +1,62 @@
|
||||
# Example: Custom Branded Notification
|
||||
# This script demonstrates how to create a notification with custom branding
|
||||
# This script demonstrates how to show a notification with custom branding
|
||||
|
||||
# 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
|
||||
|
||||
# Create custom notification options with branding
|
||||
# Create custom notification options
|
||||
$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."
|
||||
$options.Message = "Your system has been selected for a security update."
|
||||
|
||||
# Set branding properties
|
||||
$options.BrandingText = "Contoso IT Department"
|
||||
$options.BrandingColor = "#0078D7" # Blue
|
||||
$options.AccentColor = "#E81123" # Red
|
||||
$options.UseDarkTheme = $true
|
||||
$options.Attribution = "Contoso IT Department"
|
||||
|
||||
# Set custom images
|
||||
# Note: Replace these paths with actual image paths on your system
|
||||
$logoPath = Join-Path -Path $PSScriptRoot -ChildPath "Images\company-logo.png"
|
||||
# Set custom images (file paths or URLs)
|
||||
$logoPath = Join-Path -Path $PSScriptRoot -ChildPath "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"
|
||||
$heroPath = Join-Path -Path $PSScriptRoot -ChildPath "banner.png"
|
||||
if (Test-Path $heroPath) {
|
||||
$options.HeroImagePath = $heroPath
|
||||
}
|
||||
|
||||
# Add custom buttons with images
|
||||
# Add custom buttons
|
||||
$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
|
||||
$laterButton = New-Object WindowsNotifications.Models.NotificationButton("Later", "later")
|
||||
$options.Buttons.Add($updateButton)
|
||||
$options.Buttons.Add($deferButton)
|
||||
$options.Buttons.Add($moreInfoButton)
|
||||
|
||||
# Configure audio
|
||||
$options.AudioSource = "ms-winsoundevent:Notification.Default"
|
||||
$options.SilentMode = $false
|
||||
$options.Buttons.Add($laterButton)
|
||||
|
||||
# Show the notification
|
||||
Write-Host "Showing custom branded notification..."
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
$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 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))"
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
# Handle button clicks
|
||||
if ($result.ClickedButtonId -eq "install") {
|
||||
Write-Host "User chose to install the update (simulated)"
|
||||
# In a real script, you would install the update here
|
||||
} elseif ($result.ClickedButtonId -eq "later") {
|
||||
Write-Host "User chose to install later"
|
||||
# In a real script, you would schedule a reminder here
|
||||
}
|
||||
}
|
||||
} elseif ($result.Dismissed) {
|
||||
Write-Host "Notification was dismissed"
|
||||
}
|
||||
|
||||
+25
-31
@@ -1,35 +1,29 @@
|
||||
# Example: Load Assembly from Base64
|
||||
# Example: Load 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
|
||||
# First, convert the DLL to a Base64 string
|
||||
# In a real scenario, you would have this string pre-generated
|
||||
$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."
|
||||
$bytes = [System.IO.File]::ReadAllBytes($dllPath)
|
||||
$base64 = [Convert]::ToBase64String($bytes)
|
||||
|
||||
# Now, load the assembly from the Base64 string
|
||||
$assemblyBytes = [Convert]::FromBase64String($base64)
|
||||
$assembly = [System.Reflection.Assembly]::Load($assemblyBytes)
|
||||
|
||||
# Create a notification manager
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
|
||||
# Show a simple notification
|
||||
$result = $notificationManager.ShowSimpleNotification("Loaded from Base64", "This notification was shown using an assembly loaded from a Base64 string.")
|
||||
|
||||
# Display the result
|
||||
Write-Host "Notification displayed: $($result.Displayed)"
|
||||
Write-Host "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# If the notification was interacted with, show the details
|
||||
if ($result.Activated) {
|
||||
Write-Host "Notification was activated"
|
||||
} elseif ($result.Dismissed) {
|
||||
Write-Host "Notification was dismissed"
|
||||
}
|
||||
|
||||
@@ -1,58 +1,27 @@
|
||||
# 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
|
||||
}
|
||||
# Import the module
|
||||
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath "..\PowerShell\WindowsNotifications.psd1"
|
||||
Import-Module $modulePath -Force
|
||||
|
||||
# Create a notification manager
|
||||
$notificationManager = New-Object WindowsNotifications.NotificationManager
|
||||
# Initialize the notification system
|
||||
$dllPath = Join-Path -Path $PSScriptRoot -ChildPath "..\WindowsNotifications\bin\Release\WindowsNotifications.dll"
|
||||
Initialize-WindowsNotifications -DllPath $dllPath
|
||||
|
||||
# Show a notification with buttons
|
||||
Write-Host "Showing notification with buttons..."
|
||||
$result = $notificationManager.ShowNotificationWithButtons(
|
||||
"Action Required",
|
||||
"Please select an option below:",
|
||||
"Approve",
|
||||
"Reject",
|
||||
"Defer"
|
||||
)
|
||||
$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 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
# 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))"
|
||||
}
|
||||
} elseif ($result.Dismissed) {
|
||||
Write-Host "Notification was dismissed"
|
||||
}
|
||||
|
||||
@@ -1,21 +1,13 @@
|
||||
# Example: Using the WindowsNotifications PowerShell Module
|
||||
# This script demonstrates how to use the WindowsNotifications PowerShell module
|
||||
# Example: PowerShell Module
|
||||
# This script demonstrates how to use the PowerShell module
|
||||
|
||||
# Import the module
|
||||
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath "..\PowerShell"
|
||||
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath "..\PowerShell\WindowsNotifications.psd1"
|
||||
Import-Module $modulePath -Force
|
||||
|
||||
# Initialize the module
|
||||
# Note: The DLL must be in the same directory as the module files
|
||||
# Initialize the notification system
|
||||
$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
|
||||
}
|
||||
Initialize-WindowsNotifications -DllPath $dllPath
|
||||
|
||||
# Check if running as SYSTEM
|
||||
$isSystem = Test-SystemContext
|
||||
@@ -23,94 +15,29 @@ Write-Host "Running as SYSTEM: $isSystem"
|
||||
|
||||
# Get interactive user sessions
|
||||
$sessions = Get-InteractiveUserSessions
|
||||
Write-Host "Interactive user sessions found: $($sessions.Count)"
|
||||
Write-Host "Interactive user sessions: $($sessions.Count)"
|
||||
foreach ($session in $sessions) {
|
||||
Write-Host " $session"
|
||||
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"
|
||||
$result = Show-Notification -Title "PowerShell Module" -Message "This notification was shown using 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" }
|
||||
}
|
||||
# If the notification was interacted with, show the details
|
||||
if ($result.Activated) {
|
||||
Write-Host "Notification was activated"
|
||||
} elseif ($result.Dismissed) {
|
||||
Write-Host "Notification was dismissed"
|
||||
}
|
||||
|
||||
# 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"
|
||||
# Get all notification results
|
||||
$results = Get-AllNotificationResults
|
||||
Write-Host "Total notification results: $($results.Count)"
|
||||
|
||||
# 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"
|
||||
# Clear all notification results
|
||||
Clear-AllNotificationResults
|
||||
Write-Host "All notification results cleared"
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
# Example: PowerShell Module
|
||||
# This script demonstrates how to use the PowerShell module
|
||||
|
||||
# Import the module
|
||||
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath "..\PowerShell\WindowsNotifications.psd1"
|
||||
Import-Module $modulePath -Force
|
||||
|
||||
# Initialize the notification system
|
||||
Initialize-WindowsNotifications
|
||||
|
||||
# Check if running as SYSTEM
|
||||
$isSystem = Test-SystemContext
|
||||
Write-Output "Running as SYSTEM: $isSystem"
|
||||
|
||||
# Get interactive user sessions
|
||||
$sessions = Get-InteractiveUserSessions
|
||||
Write-Output "Interactive user sessions: $($sessions.Count)"
|
||||
foreach ($session in $sessions) {
|
||||
Write-Output " - $session"
|
||||
}
|
||||
|
||||
# Show a simple notification
|
||||
$result = Show-Notification -Title "PowerShell Module" -Message "This notification was shown using the PowerShell module."
|
||||
|
||||
# Display the result
|
||||
Write-Output "Notification displayed: $($result.Displayed)"
|
||||
Write-Output "Notification ID: $($result.NotificationId)"
|
||||
|
||||
# If the notification was interacted with, show the details
|
||||
if ($result.Activated) {
|
||||
Write-Output "Notification was activated"
|
||||
} elseif ($result.Dismissed) {
|
||||
Write-Output "Notification was dismissed"
|
||||
}
|
||||
|
||||
# Get all notification results
|
||||
$results = Get-AllNotificationResults
|
||||
Write-Output "Total notification results: $($results.Count)"
|
||||
|
||||
# Clear all notification results
|
||||
Clear-AllNotificationResults
|
||||
Write-Output "All notification results cleared"
|
||||
@@ -0,0 +1,10 @@
|
||||
# Examples
|
||||
|
||||
This directory contains example scripts demonstrating how to use the Windows Notifications system.
|
||||
|
||||
## Examples
|
||||
|
||||
- `SimpleNotification.ps1` - Shows a simple notification
|
||||
- `NotificationWithButtons.ps1` - Shows a notification with buttons
|
||||
- `CountdownNotification.ps1` - Shows a notification with a countdown timer
|
||||
- `SystemContextNotification.ps1` - Shows a notification from SYSTEM context
|
||||
@@ -1,68 +1,36 @@
|
||||
# Example: Reboot Notification with Deferrals
|
||||
# Example: Reboot Notification
|
||||
# This script demonstrates how to show a reboot notification with deferral options
|
||||
|
||||
# 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
|
||||
|
||||
# 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)
|
||||
# Show a 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 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 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.ClickedButtonId -eq "reboot") {
|
||||
Write-Host "User chose to reboot now"
|
||||
# In a real script, you would initiate a reboot here
|
||||
Write-Host "User chose to reboot now (simulated)"
|
||||
# In a real script, you would reboot the computer here
|
||||
# Restart-Computer -Force
|
||||
}
|
||||
elseif ($result.Deferred) {
|
||||
Write-Host "User deferred the reboot until: $($result.DeferredUntil)"
|
||||
Write-Host "Deferral reason: $($result.DeferralReason)"
|
||||
}
|
||||
}
|
||||
} elseif ($result.Dismissed) {
|
||||
Write-Host "Notification was dismissed"
|
||||
} elseif ($result.Deferred) {
|
||||
Write-Host "Notification was deferred until: $($result.DeferredUntil)"
|
||||
}
|
||||
|
||||
@@ -1,49 +1,24 @@
|
||||
# Example: Simple Notification
|
||||
# This script demonstrates how to show a simple notification
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
# 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"
|
||||
}
|
||||
Initialize-WindowsNotifications -DllPath $dllPath
|
||||
|
||||
# Show a simple notification
|
||||
Write-Host "Showing simple notification..."
|
||||
$result = $notificationManager.ShowSimpleNotification("Simple Notification", "This is a simple notification from PowerShell")
|
||||
$result = Show-Notification -Title "Simple Notification" -Message "This is a simple notification." -TimeoutInSeconds 10
|
||||
|
||||
# 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"
|
||||
}
|
||||
# If the notification was interacted with, show the details
|
||||
if ($result.Activated) {
|
||||
Write-Host "Notification was activated"
|
||||
} elseif ($result.Dismissed) {
|
||||
Write-Host "Notification was dismissed"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user