powershell script for system reboot

Rakesh Kumar 461 Reputation points
2023-08-01T13:15:47.6266667+00:00

Dear All,

we are creating a script which will check if any system has not been rebooted in last 20 days and give a popup to logged in user with 3 buttons [Restart, snooze, dismiss]. created below script but not sure what i'm missing as i'm not getting popup. any help will be highly appreciated.

===========================================

Get the WMI object for the operating system

$os = Get-WmiObject -Class Win32_OperatingSystem

Convert the last boot up time and the current time to datetime objects

$lastBootUpTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($os.LastBootUpTime)

$currentTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($os.LocalDateTime)

Calculate the uptime in days

$uptime = ($currentTime - $lastBootUpTime).Days

Check if the uptime is more than 20 days

if ($uptime -gt 20) {

Import the BurntToast module for creating toast notifications

Import-Module BurntToast

Create a toast notification with a title, a message, an image and three buttons

New-BurntToastNotification -Text "System Restart Required", "Your system has been running for more than 20 days. Please save your work and close all applications. The system will restart in 10 minutes." -AppLogo "C:\Windows\System32\oobe\info\logo.png" -Button @(

New-BTButton -Content "Restart Now" -Arguments "RestartNow" -ActivationType Protocol,

New-BTButton -Content "Snooze" -Arguments "Snooze" -ActivationType Protocol,

New-BTButton -Content "Dismiss" -Arguments "Dismiss" -ActivationType Protocol

)

Register an event handler for the button click

Register-EngineEvent -SourceIdentifier "Microsoft.PowerShell.BurntToastNotification" -Action {

Get the button argument from the event

$button = $event.SourceEventArgs.Argument

If the button argument is "RestartNow", restart the system

if ($button -eq "RestartNow") {

Restart-Computer -Force

}

If the button argument is "Snooze", schedule a shutdown in 10 minutes

elseif ($button -eq "Snooze") {

shutdown /s /t 600

}

If the button argument is "Dismiss", do nothing

elseif ($button -eq "Dismiss") {

No action required

}

}

}

============================================

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2023-08-02T08:43:20.2533333+00:00

    Hi,

    Please see if this works.

    New-BurntToastNotification -Text "System Restart Required", "Your system has been running for more than 20 days. Please save your work and close all applications. The system will restart in 10 minutes." -AppLogo "C:\Windows\System32\oobe\info\logo.png" -Button @(
    $(New-BTButton -Content "Restart Now" -Arguments "RestartNow" -ActivationType Protocol),
    $(New-BTButton -Content "Snooze" -Arguments "Snooze" -ActivationType Protocol),
    $(New-BTButton -Content "Dismiss" -Arguments "Dismiss" -ActivationType Protocol)
    )
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Limitless Technology 44,746 Reputation points
    2023-08-02T10:05:35.89+00:00
    Hello Rakesh,
    
    Thank you for your question and for reaching out with your question today.
    
    The script you have provided looks mostly correct, but it seems that there is an issue with the placement of the event registration block. The event registration and event handler should be set up before creating the notification. Here's the corrected version of the script:
    
    ```powershell
    # Get the WMI object for the operating system
    $os = Get-WmiObject -Class Win32_OperatingSystem
    
    # Convert the last boot up time and the current time to datetime objects
    $lastBootUpTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($os.LastBootUpTime)
    $currentTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($os.LocalDateTime)
    
    # Calculate the uptime in days
    $uptime = ($currentTime - $lastBootUpTime).Days
    
    # Check if the uptime is more than 20 days
    if ($uptime -gt 20) {
        # Import the BurntToast module for creating toast notifications
        Import-Module BurntToast
    
        # Register an event handler for the button click
        Register-EngineEvent -SourceIdentifier "Microsoft.PowerShell.BurntToastNotification" -Action {
            # Get the button argument from the event
            $button = $event.SourceEventArgs.Argument
    
            # If the button argument is "RestartNow", restart the system
            if ($button -eq "RestartNow") {
                Restart-Computer -Force
            }
            # If the button argument is "Snooze", schedule a shutdown in 10 minutes
            elseif ($button -eq "Snooze") {
                shutdown /s /t 600
            }
            # If the button argument is "Dismiss", do nothing
            elseif ($button -eq "Dismiss") {
                # No action required
            }
        }
    
        # Create a toast notification with a title, a message, an image and three buttons
        New-BurntToastNotification -Text "System Restart Required", "Your system has been running for more than 20 days. Please save your work and close all applications. The system will restart in 10 minutes." -AppLogo "C:\Windows\System32\oobe\info\logo.png" -Button @(
            New-BTButton -Content "Restart Now" -Arguments "RestartNow" -ActivationType Protocol,
            New-BTButton -Content "Snooze" -Arguments "Snooze" -ActivationType Protocol,
            New-BTButton -Content "Dismiss" -Arguments "Dismiss" -ActivationType Protocol
        )
    }
    

    With this corrected version, the event registration is done before the notification is created, so the event handler will be triggered correctly when the user clicks on the buttons in the notification.

    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

    If the reply was helpful, please don’t forget to upvote or accept as answer.

    Best regards.

    
    

  3. Vishpendra singh 0 Reputation points
    2023-09-19T13:45:49.4+00:00

    Hello Rakesh,

    Did you figure out the script yet? I have the same requirement.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.