View
220
Download
0
Embed Size (px)
Citation preview
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
• 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.
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
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
DBA 2378 • The Dirty Dozen: PowerShell
THE Module for SQL Server is SQLPSX
http://SQLPSX.CodePlex.com
DBA 2378 • The Dirty Dozen: PowerShell
How Many of You Like Repetitively Clicking Around SSMS?
DBA 2378 • The Dirty Dozen: PowerShell
How Many People Here Are Already Using PowerShell?
DBA 2378 • The Dirty Dozen: PowerShell
How Many People Here Are Developers?
DBA 2378 • The Dirty Dozen: PowerShell
Please Fill Out the Survey Form!!
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
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)
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
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()
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
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.
DBA 2378 • The Dirty Dozen: PowerShell
If you want to see more slides you better leave now because it’s all scripts from here
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
for attending this session and the 2010 PASS Summit in Seattle