I have this script, I'm bringing it to you and I want to add to the shutdown alert message that is displayed a timer that shows how much time is left to shutdown
Without touching the script itself with something else unrelated
Add Windows API definitions for mouse movement monitoring
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
}
public struct POINT {
public int X;
public int Y;
}
"@
$lastPosition = New-Object POINT
function Reset-IdleTime {
$idleStopwatch.Restart()
}
function Check-MouseMovement {
currentPosition=New−ObjectPOINT[Win32]::GetCursorPos([ref]currentPosition) | Out-Null
$currentTime = Get-Date
code
if (($currentTime - $script:startTime).TotalSeconds -ge $stabilizationTime) {
if ($currentPosition.X -ne $lastPosition.X -or $currentPosition.Y -ne $lastPosition.Y) {
Write-Log "Mouse movement detected"
Reset-IdleTime
$global:lastPosition = $currentPosition
}
} else {
Reset-IdleTime
}
To add a countdown timer to the shutdown alert message in your script, you can modify the Show-ShutdownWarning function to update the label text every second with the remaining time until shutdown. Below is the updated function with the necessary changes:
Define warningDuration: Introduce a $warningDuration variable to represent the total duration of the warning period, making it clearer and more consistent.
powershell
# Set the warning duration
$warningDuration = [TimeSpan]::FromMinutes(5)
Note: The rest of your script remains unchanged, ensuring that no unrelated parts are affected.
Final Script with Changes Incorporated:
Combine the modified Show-ShutdownWarning function with the rest of your script. Make sure to replace the existing Show-ShutdownWarning function with the updated one provided above. Here's how the updated part fits into your script:
powershell
# ... [rest of your script above remains unchanged] ...
function Show-ShutdownWarning {
# [Updated function as shown above]
}
# ... [rest of your script below remains unchanged] ...
By making these changes, the shutdown alert message will now display a countdown timer that updates every second, showing the user exactly how much time is left before the system shuts down.