Saltar al contenido
Microsoft scripts

Scripts de Intune

Programar reinicio con aviso al usuario

Programa un reinicio en 4 horas con shutdown.exe y un mensaje visible. Si ya hay un reinicio programado, lo deja como está.

Antes de correrloReinicia equipos y no tiene simulación; quien no guarde su trabajo lo pierde. El mensaje al usuario está en inglés y dice "4 horas" fijo aunque cambies $DelaySeconds: edítalo. Microsoft recomienda no poner reinicios en scripts de Remediaciones.

Ejemplo en México

Agencia aduanal en Reynosa, 40 equipos con turnos de oficina

Los equipos tienen parches pendientes y no pueden reiniciarse durante el horario de despachos.

remediate-reboot-pending.ps1

Los equipos se reinician 4 horas después, al terminar el turno, y quedan parchados.

Cómo implementarlo

  1. En el centro de administración de Microsoft Intune ve a Dispositivos > Administrar dispositivos > Scripts y correcciones (Scripts and remediations) > Remediations > Crear paquete de scripts.
  2. En Configuración sube detect-reboot-pending.ps1 como script de detección y remediate-reboot-pending.ps1 como script de corrección. Ajusta antes $DelaySeconds y traduce el mensaje.
  3. Deja desactivadas "Ejecutar con las credenciales del usuario" y "Aplicar verificación de firma", asígnalo primero a un grupo piloto y elige la frecuencia.
  4. Revisa en el paquete los equipos con reinicio programado y avisa a los usuarios; pueden cancelarlo con shutdown /a.
Ver el código completo de remediate-reboot-pending.ps1
<#
.TITLE
    Reboot Pending Remediation Script

.SYNOPSIS
    Schedules a restart with a visible user warning on devices that have a pending reboot.

.DESCRIPTION
    Schedules a system restart after a configurable delay (default 4 hours) using
    shutdown.exe with a warning message, giving the user time to save work. If a
    restart is already scheduled the script leaves it in place. Paired with
    detect-reboot-pending.ps1 which triggers only for devices that carry a pending
    reboot beyond the uptime threshold.

.TAGS
    Remediation,Action

.REMEDIATIONTYPE
    Remediation

.PAIRSCRIPT
    detect-reboot-pending.ps1

.PLATFORM
    Windows

.MINROLE
    Intune Service Administrator

.PERMISSIONS
    DeviceManagementManagedDevices.ReadWrite.All

.AUTHOR
    Ugur Koc

.VERSION
    1.0

.CHANGELOG
    1.0 - Initial release

.LASTUPDATE
    2026-07-20

.EXAMPLE
    .\remediate-reboot-pending.ps1
    Schedules a restart in 4 hours with a user-visible warning

.NOTES
    - Runs in SYSTEM context via Intune Remediations
    - Adjust $DelaySeconds to fit maintenance windows; users see the Windows restart warning immediately
    - A user or admin can cancel the scheduled restart with: shutdown /a
#>

$ErrorActionPreference = "Stop"

# Delay before the restart fires (default 4 hours)
$DelaySeconds = 14400
$Message = "Your IT department scheduled a restart to finish installing updates. Save your work. The device restarts automatically in 4 hours. "

try {
    # If a shutdown is already scheduled, shutdown.exe returns error 1190;
    # leave the existing schedule untouched
    $process = Start-Process -FilePath "$env:SystemRoot\System32\shutdown.exe" -ArgumentList "/r /t $DelaySeconds /c `"$Message`"" -Wait -PassThru -NoNewWindow

    if ($process.ExitCode -eq 0) {
        Write-Output "Restart scheduled in $([math]::Round($DelaySeconds / 3600, 1)) hours."
        exit 0
    }
    elseif ($process.ExitCode -eq 1190) {
        Write-Output "A restart is already scheduled on this device - leaving it in place."
        exit 0
    }
    else {
        Write-Output "shutdown.exe returned exit code $($process.ExitCode)"
        exit 1
    }
}
catch {
    Write-Error $_
    exit 1
}

# Copia sin cambios de https://github.com/ugurkocde/IntuneAutomation (commit 797bb2e).
# MIT License
#
# Copyright (c) 2025 Ugur Koc | Microsoft MVP
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

Relacionados