Skip to content

mbentham/sql-server-scripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQL Server Scripts

License: MIT PowerShell SQL Server

A collection of production-ready SQL Server administration and automation scripts in PowerShell and T-SQL. Built for DBAs and platform engineers managing SQL Server estates, covering backup verification, security hardening, CDC, monitoring, and migration.

Getting Started

  1. Clone the repo or download individual scripts
  2. Each script contains full documentation in its header — prerequisites, parameters, and examples
  3. For PowerShell scripts: Get-Help .\ScriptName.ps1 -Full
  4. For T-SQL scripts: read the comment block at the top of the file

Scripts

Backup & Restore

Script Description
Test-SqlBackupRestore.ps1 Restores backups from Azure Blob Storage, runs DBCC CHECKDB, and logs results

CDC

Script Description
Export-SqlCdcChanges.ps1 Exports CDC change data to Azure Blob Storage as compressed JSON
ResolveCDCSchemaDrift.sql Detects and resolves schema drift by recycling CDC capture instances
SQLServerCDCDemo.sql End-to-end CDC tutorial: setup, querying, PII redaction, and schema drift

Migration

Script Description
Get-SqlEFMigrationHistory.ps1 Discovers EF Core migration history across multiple instances
Import-AccessDatabase.ps1 Bulk-imports all tables from a Microsoft Access database into SQL Server

Monitoring

Script Description
ConfigureDbaAlerts.sql Configures Database Mail, DBA operator, and SQL Agent alerts (severity 17-25, I/O errors)
Send-FailedTaskNotification.ps1 Sends HTML email notifications for failed or missed Task Scheduler jobs
Test-SqlPatchCompliance.ps1 Checks SQL Server CU compliance and reports via table and/or email

Security

Script Description
ConfigureLoginAudit.sql Creates a SQL Server Audit for login success/failure tracking
CopyUserAccess.sql Generates T-SQL to clone one user's permissions to another
CreateMcpLogin.sql Creates a least-privilege login for AI-driven DBA monitoring via MCP
Find-WeakSqlPasswords.ps1 Checks SQL logins against a list of known compromised passwords
GetOrphanedUsers.sql Detects orphaned database users across all databases
Test-CISSqlServerBenchmark.ps1 Runs ~40 CIS SQL Server 2019 v1.4.0 benchmark checks

Utilities

Script Description
CalculateWorkingHours.sql Calculates elapsed working hours between two timestamps (excludes weekends/holidays)

Prerequisites

Common dependencies across scripts — each script documents its own specific requirements in its header.

  • PowerShell 5.1+ or PowerShell 7+
  • dbatools v2.0+ — PowerShell scripts that interact with SQL Server
  • Az modules (Az.Accounts, Az.Storage, Az.KeyVault) — scripts that use Azure Key Vault or Blob Storage
  • SQL Server 2012+ — minimum version varies by script

Contributing

Contributions are welcome via pull request.

  • Each script should include comment-based help: .SYNOPSIS, .DESCRIPTION, .PREREQUISITES, .PARAMETER, .EXAMPLE, .LICENSE
  • Follow the existing naming convention: verb-first PascalCase (e.g. Test-SqlPatchCompliance.ps1, ConfigureDbaAlerts.sql)
  • Place scripts in the appropriate category folder

License

This project is licensed under the MIT License.

About

Various useful powershell and t-sql scripts for SQL Server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors