Created
March 6, 2019 09:38
-
-
Save raulunzue/127fcf2147f1776434f88e67f2025e92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################## | |
############## | |
############# Script Raúl para el Blog de Negu - 24062018 | |
############# https://www.maquinasvirtuales.eu/script-matar-procesos-libreoffice/ | |
############ Parar procesos con más de 60 segundos en memoria | |
############################################################################## | |
# Clear-Host | |
# Cargamos una variable con la hora menos 60 segundos | |
$60Segundos = (Get-Date).AddSeconds(-60) | |
# Cargamos variable con el cmd más antiguo en memoria | |
$Procs = get-process -name soffice.bin -ErrorAction SilentlyContinue | Sort StartTime | Select -First 1 | |
# Empezamos a escribir el log | |
# Start-Transcript ("c:\script\log\matar-procesos{0:yyyyMMdd-HHmmss}.log" -f (Get-Date)) | |
# Si existen procesos en memoria los comprueba | |
if ($Procs -ne $null) | |
{ | |
# Revisamos los IDs | |
ForEach ($ProcID in $Procs.ID) | |
{ | |
# Vemos la diferencia de tiempo | |
$diferencia = $60Segundos - $Procs.StartTime | |
# Si la diferencia es igual o mayor que 1 minuto | |
if ($diferencia.Minutes -ge '1') | |
{ | |
$hora1 = Get-Date | |
# Matamos los IDs de los procesos de más o igual a 1 minuto | |
stop-process -id $ProcID -Force | |
# Exportamos todo a un fichero log | |
$log1 = Write-Output "Forzamos cierre proceso $ProcID, el dia $hora1, llevaba $diferencia minutos" | |
Add-Content $log1 -Path "c:\script\log\matar-procesos.log" | |
Write-Output "##############PROCESOS CERRADOS###############" | |
Write-Output "Forzamos cierre proceso $ProcID, el dia $hora1, llevaba $diferencia minutos" | |
Write-Output "##########FIN PROCESOS CERRADOS###############" | |
} | |
# Si hay procesos soffice pero no tienen más de o igual de 1 minuto | |
else | |
{ | |
$hora2 = Get-Date | |
# Exportamos todo a un fichero log | |
$log2 = Write-Output "$hora2, no hay procesos de mas de 60seg que cerrar" | |
Add-Content $log2 -Path "c:\script\log\matar-procesos.log" | |
Write-Output "##############NO HAY PROCESOS DE 60SEG###############" | |
Write-Output "$hora2, no hay procesos de mas de 60seg que cerrar" | |
Write-Output "##########FIN PROCESOS CERRADOS###############" | |
} | |
} | |
} | |
# Si no existen procesos soffice en memoria | |
else | |
{ | |
$hora3 = Get-Date | |
# Exportamos todo a un fichero log | |
$log3 = Write-Output "$hora3, no hay procesos que cerrar" | |
Add-Content $log3 -Path "c:\script\log\matar-procesos.log" | |
Write-Output "##############NO HAY PROCESOS A CERRAR########" | |
Write-Output "$hora3, no hay procesos que cerrar" | |
Write-Output "##########FIN PROCESOS CERRADOS###############" | |
#Liberamos variables para nueva ejecucion | |
Remove-Variable 60segundos | |
Remove-Variable Procs | |
} | |
# Parar la escritura del log | |
# Stop-Transcript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment