Azure SQL Maintenance Automation

A scheduled Azure Automation runbook that keeps SQL elastic pools healthy — shrinks wasted log space, resizes databases to demand, adjusts pool capacity, and emails a full audit trail after every run.

Azure SQL Elastic Pools Azure Automation PowerShell FinOps

Storage Drift Costs Money and Performance

Azure SQL elastic pools grow unevenly. Some databases balloon with unused log space; others need more room overnight. Manual resizing is slow, inconsistent, and easy to postpone.

Over-allocated databases waste cloud spend. Under-provisioned pools risk throttling or failed writes when workloads spike. Firewall access for maintenance workers is another manual step — open a rule, run the job, remember to close it. Without an audit trail, nobody can prove what changed overnight.

We needed a repeatable maintenance job that could run on Azure workers or hybrid workers, stay secure when VNet integration was unavailable, and leave operators with a clear report instead of a silent portal change.


One Runbook, Full Pool Hygiene

We built a PowerShell 7.2 Azure Automation script that assesses the elastic pool, remediates each database, manages temporary firewall access, and emails a structured HTML report.

What the job does

  • Checks elastic pool free space and resizes the pool when capacity drifts outside target bands
  • Shrinks database log files when unused space exceeds 20% of allocated storage
  • Grows databases that need headroom — targeting ~50% more space than current use
  • Adds a temporary firewall rule for the worker IP, then removes it when finished
  • Emails a summary of shrinks, resizes, pool changes, and errors

Why it matters

  • Performance — lean storage and right-sized files keep queries from fighting bloated allocations
  • Cost — pay for capacity you need, not leftover unused space
  • Scale — pools grow with the workload without overnight pager duty
  • Security — temporary firewall rules leave no permanent worker IP exposure
Azure Automation (Azure worker or hybrid worker) │ ├─ Az.Sql / Az.Resources / Az.Monitor ├─ Temporary SQL firewall rule (if no VNet) │ ▼ Elastic pool assessment ├─ Free space below min → grow pool └─ Free space above max → trim toward target │ ▼ Per-database loop ├─ DBCC / SQL space query → shrink if unused log > 20% └─ Resize allocated size toward usage + headroom │ ▼ Cleanup — remove temp firewall rule │ ▼ SMTP report — summary, DB changes, action log

Every Run Leaves an Audit Trail

Operators get an HTML email after each job — totals, per-database changes, and a step-by-step log of authentication, checks, and SQL operations.

HTML maintenance report showing summary of changes, database shrink and resize rows, and elastic pool storage adjustment log
Example report from a live run: 34 databases checked, one log shrink, one resize, zero errors — with timestamps for every action.

What We Used

Designed for Azure Automation Accounts with either Azure workers or hybrid workers — no custom portal dependency.

Platform & tooling

Azure Automation PowerShell 7.2 Azure SQL / Elastic Pools Az.Sql Az.Resources Az.Monitor SMTP reporting

Engineering approach

The runbook authenticates to Azure, optionally opens a temporary SQL firewall rule for the worker, adjusts the elastic pool first so individual database resizes have room to succeed, then walks each database with direct SQL for space accounting and shrink operations. All actions are recorded in structured collections that render into the email report — so a failed shrink or resize is visible in the same place as the successes.


Hands-Off Pool Hygiene

Routine storage maintenance no longer depends on someone remembering to check the portal. The pool stays within target free-space bands, databases reclaim wasted log space, and every change is emailed.

Auto
Elastic pool resize when free space drifts
20%
Unused log threshold triggers shrink
Temp
Firewall rule added & removed per run
Email
Full audit trail after every job
Elastic pools stay within configured free-space targets instead of drifting until someone notices a bill or an outage
Temporary firewall access for workers — no permanent open IP left behind after maintenance
Operators get shrink counts, resize amounts, pool deltas, and error totals in one email
Works with Azure Automation workers or hybrid workers — same script, flexible hosting

Automate Your Azure SQL Ops

If elastic pools are growing by hand, storage costs are climbing without explanation, or overnight maintenance still depends on a laptop and a calendar reminder — we build Azure Automation that keeps databases lean and leaves a paper trail.

Theme