18
The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

The Dirty Dozen: PowerShell Scripts for the Busy DBA

DBA-237

Aaron Nelson, @SQLvariant

Page 2: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

About Me:

• I’ve been working with databases for over 10 years

• I’m, a Second Generation DBA

• I blog at SQLvariant.com

[email protected]

• All of the scripts shown here are available on my blog.

• Please download the scripts and follow along if you have your laptop with you.

Page 3: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

Why PowerShell?

“It’s always useful to keep in mind that PowerShell is not “just” a shell or scripting language.

Its primary purpose is to be an automation tool for managing Microsoft Windows.” – Bruce Payette

Windows PowerShell in Action Second Edition

Page 4: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

PowerShell Bolt-Ons or “Expansion Packs”

In addition to the out of the box PowerShell features there are some ‘expansion packs’ in the form of • Snap-Ins and • Modules

The Snap-Ins for SQL Server come with SSMS 2008+ and they are • SqlServerCmdletSnapin100 • SqlServerProviderSnapin100

Page 5: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

THE Module for SQL Server is SQLPSX

http://SQLPSX.CodePlex.com

Page 6: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

How Many of You Like Repetitively Clicking Around SSMS?

Page 7: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

How Many People Here Are Already Using PowerShell?

Page 8: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

How Many People Here Are Developers?

Page 9: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

Please Fill Out the Survey Form!!

Page 10: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

What’s this Methods & Properties Business?

Methods are just verbs: Like Drop Table

Properties are adjectives: Like Auto-Shrink Enabled

NEVER DO THIS

Page 11: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

Deciphering PowerShell

• $SomeTable is a variable

• Drop-Database (Verb-Noun) is how cmdlets (and most functions) are named

• SQLSERVER:\sql\D2\R2\DATABASES\ Provider Path

• $SomeTable.Script() This is a Method being used on an object (in this case the object is a variable)

Page 12: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

Variables in PowerShell Get “Expanded”

DECLARE

@DataBaseName VARCHAR(128) = 'ReportServer',

@SQL VARCHAR(2000)

SET @SQL = 'SELECT *

FROM '+@DataBaseName+'.INFORMATION_SCHEMA.COLUMNS'

$DataBaseName= "ReportServer"

invoke-sqlcmd -query "SELECT * FROM $DataBaseName.INFORMATION_SCHEMA.COLUMNS" `

-ServerInstance D2 -database master

Page 13: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

Deciphering PowerShell

This is a .Net thingie

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")

This is another a .Net thingie

[Microsoft.SqlServer.Management.Smo.SmoApplication]::EnumAvailableSqlServers()

Page 14: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

Deciphering PowerShell

So tell me about yourself:

Get-Help is how you find out more about a cmdlet or a function (that has help available)

Get-Member is not just a bad Austin Powers joke the Dev team left laying around.

It’s how you find out all of the Methods and Properties of an object

Page 15: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

If you need help please reach out on twitter and use the #PoShHelp hashtagAlso, the #PowerShell tag is great for finding out news about PowerShell. It is VERY widely followed.

Page 16: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

If you want to see more slides you better leave now because it’s all scripts from here

Page 17: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

DBA 2378 • The Dirty Dozen: PowerShell

Complete the Evaluation Form to Win!

Win a Dell Mini Netbook – every day – just for handing in your completed form. Each session evaluation form represents a chance to win.

Pick up your evaluation form:

• In each presentation room• At the PASS Booth near registration

Drop off your completed form:

• Near the exit of each presentation room• At the PASS Booth near registration

Sponsored by Dell

Page 18: The Dirty Dozen: PowerShell Scripts for the Busy DBA DBA-237 Aaron Nelson, @SQLvariant

for attending this session and the 2010 PASS Summit in Seattle