T
T
trushko2020-10-14 15:01:24
PowerShell
trushko, 2020-10-14 15:01:24

How can I automatically turn off the PC, with the ability to cancel this action by the user?

Good afternoon. Please tell me how to implement so that at 21.00 all PCs turn off, while so that if the user is working, then he can cancel the PC off in the pop-up window? I tried through gpo, but there is no way for the user to cancel the shutdown. The script also wrote, but only it warns that after the n-th number of minutes it will turn off, without the possibility of choice. Thank you, I apologize for the possibly stupid question.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
I
Ilya bow, 2020-10-14
@8889996

Hm. Maybe try creating a shortcut on the desktop "I'm a workaholic" and using the shutdown /a command?
What's the script?

Z
Zzzz9, 2020-10-14
@Zzzz9

shutdown /f /t 60 run at 20:59
if you have time shutdown /a cancel shutdown

A
Andrew AT, 2020-10-15
@AAT666

Not mine.
Copied somewhere.
I did not understand very well, corrected it for my Wishlist and - in the scheduler on the director's PC.
I'm sure you can compose it more optimally, but it's not interesting.

[void][Reflection.Assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][Reflection.Assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][Reflection.Assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')

function Main {
  Param ([String]$Commandline)
  if((Call-MainForm_psf) -eq 'OK') {	}
  $global:ExitCode = 0
}

function Call-MainForm_psf
{
  [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')

  [System.Windows.Forms.Application]::EnableVisualStyles()
  $MainForm = New-Object 'System.Windows.Forms.Form'
  $panel2 = New-Object 'System.Windows.Forms.Panel'
  $ButtonCancel = New-Object 'System.Windows.Forms.Button'
  $ButtonSchedule = New-Object 'System.Windows.Forms.Button'
  $ButtonRestartNow = New-Object 'System.Windows.Forms.Button'
  $panel1 = New-Object 'System.Windows.Forms.Panel'
  $labelITSystemsMaintenance = New-Object 'System.Windows.Forms.Label'
  $labelSecondsLeftToRestart = New-Object 'System.Windows.Forms.Label'
  $labelTime = New-Object 'System.Windows.Forms.Label'
  $labelInOrderToApplySecuri = New-Object 'System.Windows.Forms.Label'
  $timerUpdate = New-Object 'System.Windows.Forms.Timer'
  $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'

  $TotalTime = 300 # секунды
  
  $MainForm_Load={
    $labelTime.Text = "{0:D2}" -f $TotalTime 
    $script:StartTime = (Get-Date).AddSeconds($TotalTime)
    $timerUpdate.Start()
  }
  
  
  $timerUpdate_Tick={
    [TimeSpan]$span = $script:StartTime - (Get-Date)
    $labelTime.Text = "{0:N0}" -f $span.TotalSeconds
    $timerUpdate.Start()
    if ($span.TotalSeconds -le 0)
    {
      $timerUpdate.Stop()
      Restart-Computer -Force
    }
  }
  
  $ButtonRestartNow_Click = { Restart-Computer -Force }
  $ButtonCancel_Click     = { $MainForm.Close() }
  
  $Form_StateCorrection_Load = { $MainForm.WindowState = $InitialFormWindowState }
  
  $Form_Cleanup_FormClosed=
  {
    try
    {
      $ButtonCancel.remove_Click($buttonCancel_Click)
      $ButtonSchedule.remove_Click($ButtonSchedule_Click)
      $ButtonRestartNow.remove_Click($ButtonRestartNow_Click)
      $panel2.remove_Paint($panel2_Paint)
      $labelITSystemsMaintenance.remove_Click($labelITSystemsMaintenance_Click)
      $labelTime.remove_Click($labelTime_Click)
      $MainForm.remove_Load($MainForm_Load)
      $timerUpdate.remove_Tick($timerUpdate_Tick)
      $MainForm.remove_Load($Form_StateCorrection_Load)
      $MainForm.remove_Closing($Form_StoreValues_Closing)
      $MainForm.remove_FormClosed($Form_Cleanup_FormClosed)
    }
    catch [Exception]
    { }
  }

  $MainForm.SuspendLayout()
  $panel2.SuspendLayout()
  $panel1.SuspendLayout()

  $MainForm.Controls.Add($panel2)
  $MainForm.Controls.Add($panel1)
  $MainForm.Controls.Add($labelSecondsLeftToRestart)
  $MainForm.Controls.Add($labelTime)
  $MainForm.Controls.Add($labelInOrderToApplySecuri)
  $MainForm.AutoScaleDimensions = '6, 13'
  $MainForm.AutoScaleMode = 'Font'
  $MainForm.BackColor = 'White'
  $MainForm.ClientSize = '373, 279'
  $MainForm.MaximizeBox = $False
  $MainForm.MinimizeBox = $False
  $MainForm.Name = 'MainForm'
  $MainForm.ShowIcon = $False
  $MainForm.ShowInTaskbar = $False
  $MainForm.StartPosition = 'CenterScreen'
  $MainForm.Text = 'Обслуживание ОС'
  $MainForm.TopMost = $True
  $MainForm.add_Load($MainForm_Load)

  $panel2.Controls.Add($ButtonCancel)
  $panel2.Controls.Add($ButtonRestartNow)
  $panel2.BackColor = 'ScrollBar'
  $panel2.Location = '0, 205'
  $panel2.Name = 'panel2'
  $panel2.Size = '378, 80'
  $panel2.TabIndex = 9
  $panel2.add_Paint($panel2_Paint)

  $ButtonCancel.Location = '250, 17'
  $ButtonCancel.Name = 'ButtonCancel'
  $ButtonCancel.Size = '100, 45'
  $ButtonCancel.TabIndex = 7
  $ButtonCancel.Text = 'Отменить перезагрузку'
  $ButtonCancel.UseVisualStyleBackColor = $True
  $ButtonCancel.add_Click($buttonCancel_Click)

  $ButtonRestartNow.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
  $ButtonRestartNow.ForeColor = 'DarkRed'
  $ButtonRestartNow.Location = '33, 17'
  $ButtonRestartNow.Name = 'ButtonRestartNow'
  $ButtonRestartNow.Size = '100, 45'
  $ButtonRestartNow.TabIndex = 0
  $ButtonRestartNow.Text = 'Перезагрузить сейчас!'
  $ButtonRestartNow.UseVisualStyleBackColor = $True
  $ButtonRestartNow.add_Click($ButtonRestartNow_Click)

  $panel1.Controls.Add($labelITSystemsMaintenance)
  $panel1.BackColor = '0, 114, 198'
  $panel1.Location = '0, 0'
  $panel1.Name = 'panel1'
  $panel1.Size = '375, 67'
  $panel1.TabIndex = 8

  $labelITSystemsMaintenance.Font = 'Microsoft Sans Serif, 14.25pt'
  $labelITSystemsMaintenance.ForeColor = 'White'
  $labelITSystemsMaintenance.Location = '11, 18'
  $labelITSystemsMaintenance.Name = 'labelITSystemsMaintenance'
  $labelITSystemsMaintenance.Size = '290, 23'
  $labelITSystemsMaintenance.TabIndex = 1
  $labelITSystemsMaintenance.Text = 'Обслуживание АСУ'
  $labelITSystemsMaintenance.TextAlign = 'MiddleLeft'
  $labelITSystemsMaintenance.add_Click($labelITSystemsMaintenance_Click)

  $labelSecondsLeftToRestart.AutoSize = $True
  $labelSecondsLeftToRestart.Font = 'Microsoft Sans Serif, 9pt, style=Bold'
  $labelSecondsLeftToRestart.Location = '87, 176'
  $labelSecondsLeftToRestart.Name = 'labelSecondsLeftToRestart'
  $labelSecondsLeftToRestart.Size = '155, 15'
  $labelSecondsLeftToRestart.TabIndex = 5
  $labelSecondsLeftToRestart.Text = 'Рестарт через(сек.):'

  $labelTime.AutoSize = $True
  $labelTime.Font = 'Microsoft Sans Serif, 9pt, style=Bold'
  $labelTime.ForeColor = '192, 0, 0'
  $labelTime.Location = '237, 176'
  $labelTime.Name = 'labelTime'
  $labelTime.Size = '43, 15'
  $labelTime.TabIndex = 3
  $labelTime.Text = '00:60'
  $labelTime.TextAlign = 'MiddleCenter'
  $labelTime.add_Click($labelTime_Click)

  $labelInOrderToApplySecuri.Font = 'Microsoft Sans Serif, 9pt'
  $labelInOrderToApplySecuri.Location = '22, 94'
  $labelInOrderToApplySecuri.Name = 'labelInOrderToApplySecuri'
  $labelInOrderToApplySecuri.Size = '350, 83'
  $labelInOrderToApplySecuri.TabIndex = 2
  $labelInOrderToApplySecuri.Text = 'Для установки обновлений системы требуется перезагрузка компьютера !'

  $timerUpdate.add_Tick($timerUpdate_Tick)
  $panel1.ResumeLayout()
  $panel2.ResumeLayout()
  $MainForm.ResumeLayout()

  $InitialFormWindowState = $MainForm.WindowState
  $MainForm.add_Load($Form_StateCorrection_Load)
  $MainForm.add_FormClosed($Form_Cleanup_FormClosed)
  $MainForm.add_Closing($Form_StoreValues_Closing)
  return $MainForm.ShowDialog()
}

Main ($CommandLine)

M
MaxKozlov, 2020-10-15
@MaxKozlov

for PSv5.1
https://gist.github.com/MVKozlov/c46b2d6d162e00185...

O
Oleg Volkov, 2020-10-14
@voleg4u

Set GPO to sleep if you do not use, say, an hour. Unless, of course, the meaning of the task is to save electricity.

A
AlexNemets, 2020-10-22
@AlexNemets

poweroff can be set to anything.... warns before exiting with a pop-up window and a sound signal....
https://www.softportal.com/software-1036-poweroff.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question