Introduction to SQL Server Automation with Powershell by Chris Sommer
Agenda Introduction Automation The PowerShell Language The SQL Server Module (SQLPS) Demos
Introduction
Chris Sommer SQL Server DBA at on Twitter Blog: LinkedIn:
Automation
What is automation? Automation is having computers perform repetitive tasks for us.
What problems does automation solve? The people problem! Automation brings Repeatability Consistency Reliability Scalability
What do I get out of automation? I don’t have to perform the mundane and repetitive tasks any more. It frees up my time so I can work on the fun stuff and learn new things. I allows me to sleep at night and gives me a sense of relief. I can look like a rockstar!
The PowerShell Language
PowerShell Programming Constructs Variables Conditional Statements Loops Functions Built-in Error Handling Modules
Variables Variables are a reference to data or information Supports your standard data types Identified by a leading “$” character Not a strongly typed language Special variables
Custom Objects Custom objects are a collection of related data Makes it easy to pass related data in a script Referenced similarly to a variable
Conditionals Check if a statement is true or false Comparison operators -eq -ne -gt- ge -lt -le -like -match -contains -and -or
Loops Allow us to repeat a block of code until a condition is met while do while do until for foreach
Functions Functions bring reusability to a script Functions should do 1 thing and do it well Functions return all output to the caller
Error Handling Not all errors are terminating Can control ErrorAction at many levels Script, Function, Cmdlet Try/Catch/Finally Exit codes
Modules Modules are collections of related cmdlets, providers, functions, variables, aliases. Module types Script Binary Manifest Dynamic Built-In, Custom and Vendor Modules
SQL Server Module (SQLPS)
What does SQLPS Include? The SQLSERVER provider SQL Management Object Libraries (SMO) 34 SQL Server Cmdlets
SQLSERVER: Provider Browse SQL Server like a directory structure Used in conjunction with SMO Used with Cmdlets
SQL Management Objects (SMO) SMO allows you to manage SQL Server SQL Instance settings Database settings and configuration Perform DDL tasks on databases Script database objects Manage SQL Agent jobs
SQL Server Cmdlets Cmdlets provide functionality to perform a number of SQL Server tasks. Invoke-Sqlcmd Backup-SqlDatabase Restore-SqlDatabase Manage AlwaysOn Availability Groups
Demos
Questions?
Image Credits