Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Server and PowerShell Let’s Get Serious

Similar presentations


Presentation on theme: "SQL Server and PowerShell Let’s Get Serious"— Presentation transcript:

1 SQL Server and PowerShell Let’s Get Serious
Did you remember to? Start VM’s? Start SQL instances on local machine (both!) Start SSMS? Bring up web site to show? Open source code to show? Delete the .sql files? Create an empty “AnotherDatabase” for demos? 11/16/2018

2 About me Contact Info I’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. @pittfurg 11/16/2018

3 AGENDA Why using PowerShell will make your life more awesome
SQL Server PowerShell Provider Code, code, and more code! You can help me be better! Fill out an online evaluation here: 11/16/2018

4 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?” Understanding PowerShell in general increases your worth. Helps you interact with System Administrators better. Useful for deploying code or system changes. Saves you a ton of time (automate all the things!). Core Servers laugh at your puny GUI. Let me know how you plan on automating things in Azure. 11/16/2018

5 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 For more information, including cmdlets and SMO objects: Import the module Show the path Show databases, use get-member Show names and sizes 11/16/2018

6 Style Points Write scripts, not code Return objects where possible
Let the provider do the work! Gross: Less Gross: Butter: [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") $SQLSvr = "." $ServerObject = new-object "Microsoft.SqlServer.Management.Smo.Server" $SQLSvr $Databases = $ServerObject.Databases Add-Type -AssemblyName "Microsoft.SqlServer.SMO,Version= ,Culture=neutral,PublicKeyToken=89845dcd8080cc91“ $SQLSvr = “localhost” $ServerObject = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLSvr $Databases = $ServerObject.Databases Import-Module SQLPS $Databases = Get-ChildItem -Path "SQLSERVER:\SQL\LOCALHOST\DEFAULT\DATABASES" 11/16/2018

7 Let’s Just Do it! 11/16/2018

8 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 Talk about the .Script() method, maybe bring up MSDN page if able? To File: .\Get-DatabaseObjects.ps1 -serverName localhost -databaseName AdventureWorks2014 -saveTo File -fileName C:\temp\Adventureworks2014_Objects.sql –Verbose To Database: .\Get-DatabaseObjects.ps1 -serverName localhost -instanceName sql2014 -databaseName AdventureWorks2014 -saveTo Database -repoServerName localhost -repoInstanceName sql2014 –repoDatabaseName Admin -repoSchemaName Repo -repoTableName ObjectsRepository –Verbose Create TWO new databases when you’re done, and run the generated script! 11/16/2018

9 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? Good intro into PowerShell provider and SQL drive paths Refresh() method on tables… why? Create a new database .\Copy-DatabaseTables.ps1 -SourceServerName localhost -SourceInstanceName sql2014 -SourceDatabaseName AdventureWorks2014 -DestinationServerName localhost -DestinationDatabaseName Somedatabase -DestinationSchemaName Sales -WorkingDirectory C:\temp -noCheckConstraints –Verbose –WarningAction silentlycontinue 11/16/2018

10 Real-World Example: Copy SSIS Environments
Need to clone that SSIS environment locally? What about to another server? Or another instance? Because it’s a colossal pain it the ass! Mention “SSIS” instead of “SQL” path, how the provider lists all instances under the main hostname Another good example of .Refresh() .\Export-SSISEnvironment.ps1 -SourceServer localhost -SourceInstance sql2014 -SourceFolder SomeProject -sourceEnvironment QA -targetServer localhost -targetInstance sql2014 -targetFolder NewProject –Verbose 11/16/2018

11 Real-World Example: Deploying Extended Events
Want to shotgun a bunch of extended events to a lot of servers? PowerShell has got you covered! Show code, talk about how the provider can be fooled Talk about reflection loading (load with partial name) .\Export-SSISEnvironment.ps1 -SourceServer localhost -SourceInstance sql2014 -SourceFolder SomeProject -sourceEnvironment QA -targetServer localhost -targetInstance sql2014 -targetFolder NewProject –Verbose 11/16/2018

12 Real-World Example: Managing Backups
Managing backups in SQL server isn’t neccesarily 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! Show code, talk about how the provider can be fooled Talk about reflection loading (load with partial name) .\Export-SSISEnvironment.ps1 -SourceServer localhost -SourceInstance sql2014 -SourceFolder SomeProject -sourceEnvironment QA -targetServer localhost -targetInstance sql2014 -targetFolder NewProject –Verbose 11/16/2018

13 Real-World Example: Auditing SQL Server Group Permissions
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 Install-windowsfeature -name AD-Domain-Services –IncludeManagementTools .\Start-SQLPermissionsAudit.ps1 -servername localhost -databaseName AdventureWorks2014 -Verbose | Export-Csv Users.txt -NoTypeInformation 11/16/2018

14 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. 11/16/2018

15 Would you like to know more?
MSDN Resources: SQL Server PowerShell Provider: Windows PowerShell Reference: People/Blogs: Hey, Scripting Guy! Mike Fal Allen White Ben Miller 11/16/2018

16 Questions? 11/16/2018

17 Thank you, sponsors! 11/16/2018

18 About me Contact Info I’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. @pittfurg 11/16/2018


Download ppt "SQL Server and PowerShell Let’s Get Serious"

Similar presentations


Ads by Google