Messagebox and action if not pressed

Frequent Contributor

Hi,

 

I'm trying to develop a script to pop-up a messagebox to a user who has the computer connected at a certain hour of the day. If the user confirms he's using the computer by pressing a button of the messagebox then the process stops.
If the user does not press anything for 30m means he's not at the computer, and we shutdown the computer.

I can pop-up the messagebox with the message, but how can I check if the button has been pressed or give it a wait 30m and then if nothing happens, do something else?


Thanks

3 Replies

Hi @dmarquesgn,

 

I can script you a messagebox like this, but isnt this GPO more leading for your problem?

https://std.rocks/windows_grouppolicy_shutdown.html

 

Best regards,
Schnittlauch

"First, No system is safe. Second, Aim for the impossible. Third, no Backup, no Mercy" - Schnittlauch

My answer helped you? Don't forget to leave a like. Also mark the answer as solved when your problem is solved. :)

@Schnittlauch 

In the case of a GPO I think the drawback is that will shutdown the computers even if they are at home, and the goal is just to turn off the computers which are in the office.

 

Meanwhile I got some code to test, to check if it works for the goal. The code is this one:

# How long to display the dialog before it closes automatically.
$timeoutSecs = 3 * 60 # 30 minutes.
# Translate this into the time of day, to show to the user.
$timeoutTimeOfDay = (Get-Date).AddSeconds($timeoutSecs).ToString('T')

# Put up a message box with a timeout and OK / Cancel buttons.
$response = 
  (New-Object -ComObject WScript.Shell).Popup(
    @"
Your computer will shut down at $timeoutTimeOfDay.

Press OK to shut down now, or Cancel to cancel the shutdown.
"@, 
    $timeoutSecs, 
    'Pending shutdown', 
    49  # 48 + 1 == exclamation-point icon + OK / Cancel buttons
  )

if ($response -eq 2) { # Cancel pressed.
  Write-Warning 'Shutdown aborted by user request.'
  exit 2
}

# OK pressed ($response -eq 1) or timed out ($response -eq -1), 
# proceed with shutdown.
'Shutting down...' 
# Stop-Computer -Force

Hi @dmarquesgn ,

sorry for the late response!

You can specify the scope within a GPO only to computers in your company.

In my opinion is a powershell script a overkill, the time for managing it, rolling it out etc. could get difficult.

I'll check your code the days and write u back.

Sorry,

Schnittlauch