25
Mike Fal - www.mikefal.net POWERSHELL JUMPSTART FOR SQL SERVER DBAS

Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Embed Size (px)

Citation preview

Page 1: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

POWERSHELL JUMPSTART FOR SQL SERVER DBAS

Page 2: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Page 3: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Don’t focus on the code, focus on the concepts.

Ask questions!

Ground Rules

Page 4: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

The What and Why of Powershell

Language Basics

Working with SQL Server

And Then What?

Get-Agenda

Page 5: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

What is Powershell?

Page 6: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Released as Powershell RC 1 – April 2006

Originally called Project “Monad”

Current available version: 4.0

(5.0 preview available)

The Facts

Page 7: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

But for what?

Page 8: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Why PowerShell?

PowerShell

SQL Server

File System

Operating System

Invoke-SqlCmd -ServerInstance $srvr -Query "BACKUP

DATABASE $db TO DISK=`'$backuplocation`'"

.NET Libraries

Page 9: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Pieces and Parts

System.Management.Automation

Powershell.exe

Powershell ISE

Hosts

Page 10: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Demo – The ISE

Page 11: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Fundamental unit of “getting stuff done”– Get-Help

– New-Item

– Remove-Module

cmdlets

Verb-Noun

Limited by Microsoft Unlimited values,

Should be descriptive

Page 12: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Learn Within Powershell

Get-Command

Get-Help

Get-Member

List Commands:*New*-Module SQLPS

Show help info:man, help-ShowWindow

Methods and properties:

Page 13: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Everything is a .Net object!

– Properties (attributes)

– Methods (functions, do things)

Object Oriented Thinking

[string]$Tea=‘Hot’

(System.String)

$Tea.Length = 3

$Tea.IndexOf(‘o’) = 2

Page 14: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

‘$’ indicates a variable

[string]$Tea = ‘Hot’

$Tea = ‘Hot’

$TeaTime = Get-Date

Variables

Page 15: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Demo – CMDLETS and Objects

Page 16: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Import-Module SQLPS (SQL 2012+ client)

The SQL Server Module

SQL Server cmdlets SQL Server Provider

Page 17: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

SQL 2008+

Job step type

Execute as ‘Run as’

Agent Jobs

Page 18: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Demo – Working with SQL Server

Page 19: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

So now what?

Page 20: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

• Powershell In A Month of Lunches

• Powershell in Depth, 2nd Edition

Books

Page 21: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

– Ben Miller(http://www.dbaduck.com/)

– Allen White(http://sqlblog.com/blogs/allen_white/)

– Kendal Van Dyke(http://www.kendalvandyke.com/)

– Laerte Junior(https://www.simple-talk.com/author/laerte-junior/)

Bloggers

Page 23: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

https://github.com/MikeFal

– Powershell repository (all my scripts, including WIP)

Other scripts

Page 24: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

Find tasks to automate

Manage the file system ONLY through Powershell

Rewrite a T-SQL or other task using Powershell

And so on…

Most of all, USE IT

Page 25: Powershell Jumpstart for SQL Server DBAs - Art of the DBA Jumpstart.pdf · Mike Fal- The What and Why of Powershell Language Basics Working with SQL Server And Then What? Get-Agenda

Mike Fal - www.mikefal.net

[email protected]

www.mikefal.net

@Mike_Fal

Questions