|
#
==================================================
# ALERTA DE
REINICIO PENDIENTE - PRODUCCIÓN
#
==================================================
# - Muestra
aviso WPF si hay reinicio pendiente
# - Sin
control de tiempo (sale siempre que haya reboot)
# - Evita
doble ejecución mediante Mutex (que no salga dos veces seguidas la alerta)
# - Pensado
para ejecución vía GPO / Tarea programada
#
==================================================
Add-Type
-AssemblyName PresentationFramework
#
--------------------------------------------------
# EVITAR
DOBLE EJECUCIÓN DEL SCRIPT
# (necesario
porque se lanza PowerShell oculto)#
==================================================
# ALERTA DE
REINICIO PENDIENTE - PRODUCCIÓN
#
==================================================
# - Muestra
aviso WPF si hay reinicio pendiente
# - Sin
control de tiempo (sale siempre que haya reboot)
# - Evita
doble ejecución mediante Mutex
# - Pensado
para ejecución vía GPO / Tarea programada
#
==================================================
Add-Type
-AssemblyName PresentationFramework
#
--------------------------------------------------
# EVITAR
DOBLE EJECUCIÓN DEL SCRIPT
# (necesario
porque se lanza PowerShell oculto)
#
--------------------------------------------------
$mutexName =
"Global\RebootPendingAlertMutex"
$createdNew = $false
$mutex = New-Object
System.Threading.Mutex($true, $mutexName, [ref]$createdNew)
if (-not
$createdNew) {
# Ya hay otra instancia ejecutándose
exit
}
#
--------------------------------------------------
# FUNCIÓN:
Detectar reinicio pendiente
#
--------------------------------------------------
function Test-PendingReboot {
if (Test-Path
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired") {
return $true
}
if (Test-Path
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based
Servicing\RebootPending") {
return $true
}
return $false
}
#
--------------------------------------------------
# FUNCIÓN:
Motivo del reinicio pendiente
#
--------------------------------------------------
function Get-PendingRebootReason {
if (Test-Path
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired") {
return "actualizaciones de
Windows"
}
if (Test-Path
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based
Servicing\RebootPending") {
return "actualizaciones
del sistema"
}
return "tareas de mantenimiento del
sistema"
}
#
--------------------------------------------------
# SI NO HAY
REINICIO PENDIENTE, SALIR
#
--------------------------------------------------
if (-not (Test-PendingReboot)) {
exit
}
#
--------------------------------------------------
# OBTENER MOTIVO DEL REINICIO
#
--------------------------------------------------
$rebootReason =
Get-PendingRebootReason
#
--------------------------------------------------
# VENTANA WPF
#
--------------------------------------------------
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="Miempresa. Reinicio
pendiente"
Height="240"
Width="540"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize"
Topmost="True">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition
Height="Auto"/>
<RowDefinition
Height="*"/>
<RowDefinition
Height="Auto"/>
</Grid.RowDefinitions>
<!-- BARRA SUPERIOR ROJA
-->
<Border Background="#B71C1C" Grid.Row="0"
Padding="10">
<TextBlock Text="Reinicio
pendiente."
Foreground="White"
FontSize="16"
FontWeight="Bold"
HorizontalAlignment="Center"/>
</Border>
<!-- TEXTO PRINCIPAL -->
<Border Grid.Row="1"
Padding="20">
<TextBlock
Text="El equipo necesita
reiniciarse para completar $rebootReason.
Por favor,
guarda tu trabajo y reinicia cuando te sea posible."
TextWrapping="Wrap"
TextAlignment="Center"
VerticalAlignment="Center"
FontSize="13"/>
</Border>
<!-- BOTÓN -->
<Button Name="AceptarBtn"
Grid.Row="2"
Width="120"
Height="35"
Margin="10"
HorizontalAlignment="Right"
Background="#D32F2F"
Foreground="White"
FontWeight="Bold"
Content="Aceptar"/>
</Grid>
</Window>
"@
$reader = New-Object
System.Xml.XmlNodeReader $xaml
$window =
[Windows.Markup.XamlReader]::Load($reader)
$button =
$window.FindName("AceptarBtn")
$button.Add_Click({
$window.Close() })
$window.ShowDialog() | Out-Null
#
--------------------------------------------------
$mutexName =
"Global\RebootPendingAlertMutex"
$createdNew = $false
$mutex = New-Object
System.Threading.Mutex($true, $mutexName, [ref]$createdNew)
if (-not
$createdNew) {
# Ya hay otra instancia ejecutándose
exit
}
#
--------------------------------------------------
# FUNCIÓN:
Detectar reinicio pendiente
#
--------------------------------------------------
function Test-PendingReboot {
if (Test-Path
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired") {
return $true
}
if (Test-Path
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based
Servicing\RebootPending") {
return $true
}
return $false
}
#
--------------------------------------------------
# FUNCIÓN:
Motivo del reinicio pendiente
#
--------------------------------------------------
function Get-PendingRebootReason {
if (Test-Path
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update\RebootRequired") {
return "actualizaciones de
Windows"
}
if (Test-Path
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based
Servicing\RebootPending") {
return "actualizaciones
del sistema"
}
return "tareas de mantenimiento del
sistema"
}
#
--------------------------------------------------
# SI NO HAY
REINICIO PENDIENTE, SALIR
#
--------------------------------------------------
if (-not (Test-PendingReboot)) {
exit
}
#
--------------------------------------------------
# OBTENER MOTIVO DEL REINICIO
#
--------------------------------------------------
$rebootReason =
Get-PendingRebootReason
#
--------------------------------------------------
# VENTANA WPF
#
--------------------------------------------------
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="Midominio.
Reinicio pendiente"
Height="240"
Width="540"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize"
Topmost="True">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition
Height="Auto"/>
<RowDefinition
Height="*"/>
<RowDefinition
Height="Auto"/>
</Grid.RowDefinitions>
<!-- BARRA SUPERIOR ROJA -->
<Border
Background="#B71C1C" Grid.Row="0"
Padding="10">
<TextBlock Text="Reinicio
pendiente."
Foreground="White"
FontSize="16"
FontWeight="Bold"
HorizontalAlignment="Center"/>
</Border>
<!-- TEXTO PRINCIPAL -->
<Border Grid.Row="1"
Padding="20">
<TextBlock
Text="El equipo
necesita reiniciarse para completar $rebootReason.
Por favor,
guarda tu trabajo y reinicia cuando te sea posible."
TextWrapping="Wrap"
TextAlignment="Center"
VerticalAlignment="Center"
FontSize="13"/>
</Border>
<!-- BOTÓN -->
<Button
Name="AceptarBtn"
Grid.Row="2"
Width="120"
Height="35"
Margin="10"
HorizontalAlignment="Right"
Background="#D32F2F"
Foreground="White"
FontWeight="Bold"
Content="Aceptar"/>
</Grid>
</Window>
"@
$reader = New-Object
System.Xml.XmlNodeReader $xaml
$window =
[Windows.Markup.XamlReader]::Load($reader)
$button =
$window.FindName("AceptarBtn")
$button.Add_Click({
$window.Close() })
$window.ShowDialog()
| Out-Null
|