Back to code PowerShell

Revisar el espacio en disco y avisar antes de que se llene

Un reporte de todas las unidades con su porcentaje libre, que termina con error si alguna baja del umbral.

This post is not in your language yet; you are seeing it in Spanish. The code is the same.

The problem

Un servidor con el disco lleno deja de funcionar sin previo aviso. Hay que revisarlo todos los días y enterarse cuando queda poco, no cuando ya no queda.

PowerShell
$umbral = 15   # porcentaje libre mínimo

$discos = Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" | ForEach-Object {
    [pscustomobject]@{
        Unidad   = $_.DeviceID
        TotalGB  = [math]::Round($_.Size / 1GB, 1)
        LibreGB  = [math]::Round($_.FreeSpace / 1GB, 1)
        LibrePct = [math]::Round($_.FreeSpace / $_.Size * 100, 1)
    }
}

$discos | Format-Table -AutoSize

$criticos = $discos | Where-Object { $_.LibrePct -lt $umbral }
if ($criticos) {
    $criticos | ForEach-Object { Write-Warning "$($_.Unidad) tiene solo $($_.LibrePct)% libre" }
    exit 1
}
Write-Host "Todos los discos por encima del $umbral% libre."

Why it is written this way

  1. Get-CimInstance con DriveType=3 trae solo discos fijos: ni unidades de red ni lectoras.
  2. Cada disco se convierte en un objeto con columnas calculadas; PowerShell trabaja con objetos, no con texto.
  3. 1GB es una constante del lenguaje: no hay que escribir 1073741824.
  4. exit 1 es lo que permite que el Programador de tareas u otra herramienta se entere de que algo anda mal.
How to try it

Guardalo como discos.ps1 y corré: powershell -File discos.ps1. Subí el umbral a 99 para ver la alerta.

Watch out

Un porcentaje engaña en discos grandes: 15% de 4 TB son 600 GB libres. En esos casos conviene alertar por gigas libres, no por porcentaje.

Adapt it to your case with AI

The button copies this prompt with the code pasted below it. Replace the brackets with your own details and paste it into ChatGPT, Claude, Gemini or Copilot.

The prompt
Tengo este script de PowerShell que revisa espacio en disco. Adaptalo para revisar estos servidores remotos: [LISTA DE SERVIDORES], alertar por porcentaje o por gigas libres según el tamaño del disco, y enviar el aviso por [CANAL: correo SMTP, Teams, webhook] solo cuando hay críticos. Que deje un CSV histórico por día para ver la tendencia. Lo voy a correr con el Programador de tareas.

Example written and reviewed by hand.

Share

Comments

No comments yet. Yours can be the first.

Plain text only, no links. No email is requested. Comments are reviewed before they are published.

Did you adapt it to something of yours?

Tell me what you solved with it, or what it was missing.

Keep trying

More code

See all
Darinel Ortega Online · I reply during the day
Today
Hello. I'm not selling anything here: this is for exchanging knowledge about technology.
Write whatever you like — you can send text, images or documents. Messages reach my console and I reply from there.

An open conversation to share knowledge. Messages reach my console and I reply from there.

Let us book a conversation

Pick the day and time that work for you. Thirty minutes, no sales pitch.

Video call

For a video call, just ask for one here and I'll send you the session link.