SQL Server And PowerShell - Let's Get Serious

Post on 21-Jan-2018

4,312 views 0 download

transcript

SQL Server and PowerShellLet’s Get Serious

1/15/2017

1/15/2017 2

About me

dfurgiuele@igsenergy.com

@pittfurg

http://www.port1433.com

http://www.genesface.com

Contact InfoI’m a Senior SQL Server DBA at IGS Energy in Dublin, Ohio. I’ve been using SQL Server since SQL Server 2000 and I love it. I also enjoy DevOps, release management, and PowerShell.

AGENDA

1/15/2017 3

1. Why using PowerShell will make your life more awesome

2. SQL Server PowerShell Provider

3. Code, code, and more code!

Using PowerShell Will Make Your Life Awesome!

“I don’t want to learn PowerShell because I don’t have time.”

“Why would I waste time writing this in PowerShell when I can already do it in T-SQL?”

1. Understanding PowerShell in general increases your worth.

2. Helps you interact with System Administrators better.

3. Useful for deploying code or system changes.

4. Saves you a ton of time (automate all the things!).

5. Core Servers laugh at your puny GUI.

6. Let me know how you plan on automating things in Azure.

1/15/2017 4

The SQL Server PowerShell Provider

• SQLPS Module

• It’s where the awesome lives

• Provides a directory hierarchy for interacting with SQL Server

• NOT perfect, but:

• SSMS 2016 fixes A LOT of problems

• Be careful with multiple versions of SSMS/SQL Server installed on same machine

• Good news!

• Microsoft is finally working hard to get “proper” SQL Server PowerShell integration and cmdlets.

• New cmdlets already added, more coming.

• For more information, including cmdlets and SMO objects: https://msdn.microsoft.com/en-us/library/cc281947(v=sql.130).aspx

1/15/2017 5

Style Points

• Write scripts, not code

• Return objects where possible

• Let the provider do the work!• Gross:

• Less Gross:

• Huh?

• Butter:

1/15/2017 6

Import-Module SQLPS$Databases = Get-ChildItem -Path "SQLSERVER:\SQL\LOCALHOST\DEFAULT\DATABASES"

Add-Type -AssemblyName"Microsoft.SqlServer.SMO,Version=13.0.0.0,Culture=neutral,PublicKeyToken=89845dcd8080cc91“$SQLSvr = “localhost”$ServerObject = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLSvr$Databases = $ServerObject.Databases

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")$SQLSvr = "."$ServerObject = new-object "Microsoft.SqlServer.Management.Smo.Server" $SQLSvr$Databases = $ServerObject.Databases

$databases = Invoke-SQLCmd -ServerInstance localhost -Database master -Query "SELECT * FROM sys.Databases"$databases.Name

Let’s Just Do it!

1/15/2017 7

Real-World Example: Export Database Objects (not just tables) for DR purposes• Why backup an entire reporting replica when you can just grab the good bits?

• Perfect for reporting environments

1/15/2017 8

Real-World Example: Dynamically Copy Database Tables From One Database to Another• PowerShell makes it easy to iterate over a Database/Schema and script out objects while

also copying the data.

• What about Foreign Keys?

• What about Indexes?

• Permissions?

1/15/2017 9

Real-World Example: Managing Backups

• Managing backups in SQL server isn’t necessarily difficult, but being consistent can be.

• Using PowerShell you can keep your scripts unified across all servers for better naming conventions

• And you can use it to restore a database to a point in time by gathering all your log files together, too!

1/15/2017 10

Real-World Example: Auditing SQL Server GroupPermissions• Just who has access to that database?

• In an active directory environment, guess what? There’s a tool (and module!) for that…

• Great example about how you can marry multiple products together

1/15/2017 11

Wrapping Up

• PowerShell is a great tool that you should be using, or trying to use

• Not saying it should solve/replace all your tasks, just supplement them

• Helps blur the line between a SQL Server Professional/DevOps/Iteration Management/etc

• A more robust you = a more in-demand you

1/15/2017 12

Would you like to know more?

MSDN Resources:

• SQL Server PowerShell Provider: https://msdn.microsoft.com/en-us/library/cc281947.aspx

• Windows PowerShell Reference: https://technet.microsoft.com/en-us/library/ms714469(v=vs.85).aspx

• SQL Server PowerShell cmdlet reference: https://technet.microsoft.com/en-us/library/mt683368(v=sql.120).aspx

People/Blogs:

• Hey, Scripting Guy! http://blogs.technet.com/b/heyscriptingguy/

• Aaron Nelson (@sqlvariant): http://sqlvariant.com

• Chrissy LaMaire (@cl): https://blog.netnerds.net/

• Mike Fal (@mike_fal): http://www.mikefal.net/

Projects:

• dbatools: https://dbatools.io/

1/15/2017 13

PASS PowerShell Virtual Chapter

1/15/2017 14

• One of the many PASS virtual chapters you can join (for free!)

• Broadcasts 3rd Wednesday of every month at noon (EST).

• http://powershell.sqlpass.org• Always looking for

presenters…

Questions?

1/15/2017 15

1/15/2017 16

My Resources

dfurgiuele@igsenergy.com

@pittfurg

http://www.port1433.com

http://www.genesface.com

Contact InfoI’m a Senior SQL Server DBA at IGS Energy in Dublin, Ohio. I’ve been using SQL Server since SQL Server 2000 and I love it. I also enjoy DevOps, release management, and PowerShell.

https://github.com/drewfurgiuele?tab=repositories

http://www.port1433.com

http://www.genesface.com

https://groupby.org/go/session2

About me