Professional PowerShell-based SQL Server SMO Recipes

code
This page is an index of all recipes found within the series of Professional PowerShell-based SQL Server SMO Recipes. Click on the category links to see the associated recipes, and associated commentary.

This index includes nearly 40 recipes that I created and use extensively throughout my SQL Management scripts, which you can find on ScriptCenter. These SMO recipes are particularly useful because of the extensive automation being performed. For instance, in the restore recipe, file structures are automatically determined using information contained within the given .bak file.

Each recipe was created with performance and readability in mind. Here’s a sample

Import CMS SQL Servers and Group Names from CSV

Create a CSV (copy/paste headers in comment of code). The CSV allows you to set the server name, group, registered instance name, and server description.

Note that if you want to create servers within subgroups, just use the following format: FirstGroup\Subgroup\OtherSubGroup, etc. If a group does not exist, it will be created.

csv

Then run the code

And voila!

regservers

These scripts were created and tested on Windows 8.1 with PowerShell v4 and SQL Server 2014, though most recipes should work with earlier versions of SQL Server. PowerShell v3 and above is required for many of the recipes. If you have a recipe request, leave a comment and I’ll see what I can do. This cookbook will be continuously built, as I work more with SMO.

Connecting to SQL Server

  • Connect / Test Connection / Disconnect from SQL Server
  • Connect to SQL Server on a specific port
  • Connecting to SQL Server Configuration Manager (SqlWmiManagement)
  • Connecting to a SQL Server Central Management Server
  • Is SMO installed on the local computer?

SQL Administration

  • Programmatically change SQL Server startup parameters
  • Get SQL Server Default Paths
  • Get disk free for all drives, including mounted drives and clustered drives
  • Limit RAM usage if max is not set using Jonathan Kehayias‘s suggested configuration
  • Find Blocking processes and associated SQL statements
  • Is executing user a sysadmin on SQL Server?
  • Get NetBIOS name
  • Stop SQL Server and SQL Agent on a remote computer
  • Start SQL Server and SQL Agent on a remote computer
  • Is SQL Server FILESTREAM enabled?
  • Force Drop Database
  • Set Database to single user and back
  • Get info similar to SQL Server Management Studio Activity Monitor Processes tab
  • Get Server Configuration (sp_cofigure)
  • Iterate server list on a SQL Server Central Management Server

SQL Agent

  • Is SQL Server Agent running?
  • Find failed jobs
  • Create Scheduled T-SQL based SQL Server Agent Job
  • Create credential, proxy account, and job to run local PowerShell scripts

Backup/Restore

  • Find databases that have never been backed up or have outdated backups
  • Make FULL copy-only backup with progress bar
  • Get database file structure from a FULL backup file
  • Restore FULL backup to server default data and log locations. Progress bar included.
  • Restore FULL backup, reusing previous file structure. Progress bar included.
  • Restore FULL/DIFF/LOGS from Hallengren-style default directory structure

Detach/Attach

  • Detach Database
  • Attach database to server from files contained within server default data and log locations.
  • Attach Database, reusing previous file structure
  • Enumerate file structure within detached MDF database file

Central Management Server

  • Connecting to a SQL Server Central Management Server
  • Create SQL Servers and Group Names from CSV
  • Get simple list of servernames from CMS
  • Get CMS Servers and Groups
  • Migrate Central Management Server
  • Delete all objects in CMS

General

  • Return query with just one result
  • Echo your connection string
  • Get list of all user (non-system) databases
  • Parse SMO URN
  • Populate Dynamic Parameter with database names
  • Populate Dynamic Parameters with database and login names

If you have a recipe request, leave a comment and I’ll see what I can do. This cookbook will be continuously built, as I work more with SMO.

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Leave a Reply

Your email address will not be published. Required fields are marked *

*