SQL Server and PowerShell Let’s Get Serious

Slides:



Advertisements
Similar presentations
Learningcomputer.com SQL Server 2008 – Administration, Maintenance and Job Automation.
Advertisements

Master Data Management & Microsoft Master Data Services Presented By: Jeff Prom Data Architect MCTS - Business Intelligence (2008), Admin (2008), Developer.
Migrating Data to SQL Azure Arunraj Chandrasekaran Twitter June 21, 2011.
Rob Sewell Making PowerShell Useful Real-Life Examples of Powershell in Action Slides available here -
Introduction to SQL Server Automation with Powershell by Chris Sommer.
Others Talk, We Listen. Managing Database Projects in Visual Studio 2013.
SQL Database Management
Start-SPPowerShell – Introduction to PowerShell for SharePoint Admins and Developers Paul BAker.
Backups for Azure SQL Databases and SQL Server instances running on Azure Virtual Machines Session on backup to Azure feature (manual and managed) in SQL.
IT06 – HAVE YOUR OWN DYNAMICS NAV TEST ENVIRONMENT IN 90 MINUTES
Visual Studio Database Tools (aka SQL Server Data Tools)
Justin Randall SQLintersection Session: Friday, 10:00am-11:15pm Automating SQL Server Administration Using SQLCMD Justin Randall.
Automated Enterprise-wide SQL Server Auditing
TECH TRACK: RHEV Backup AND Recovery
Shared Services with Spotfire
Achieve more in less time using the new SQL PowerShell
PowerShell is Happening FOR REAL THIS TIME!!!
Exam in just 24 hours!!! Pass your exam in first attempt by the help of our latest braindumps
Fun with Reporting Services Tools
Upgrading to SQL Server 2016
Simplifying XEvents Management with dbatools
Build /21/2018 © 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION.
DevOps Database Administration
Transactional Replication A Deeper Dive Drew Furgiuele, Senior DBA IGS
Azure Automation and Logic Apps:
SharePoint Saturday Omaha April 2016
dbatools! The reason to finally start learning and using Powershell
SQL Server and PowerShell Let’s Get Serious
Making PowerShell Useful
SQL Server and PowerShell Let’s Get Serious
DevOps Database Administration
SQL Saturday #654 - Omaha.
Making PowerShell Useful
What’s new in SQL Server 2016 Availability Groups
Use PowerShell & dbatools to Manage your SQL Server Environment
Chrissy LeMaire, MVP & Rob Sewell, MVP
dbatools - PowerShell and SQL Server Working Together
PowerShell for Data Professionals
SQL Server and PowerShell Let’s Get Serious
Visual Studio Database Tools (aka SQL Server Data Tools)
SQL Saturday #662 - Sioux Falls, SD Hosted by (605) SQL
Getting started with Powershell for the DBA
Please thank our sponsors!
The Dirty Dozen: Windows PowerShell Scripts for the Busy DBA
dbatools! The reason to finally start learning and using Powershell
Reliable, Repeatable, Configurable & Automated Validation with
Making PowerShell Useful
Making PowerShell Useful
Cross-Platform, Cloud and On-Premise Database Tool
PowerShell & PowerBi Reducing DBAs Context Switching
Commands for SQL Server
PowerShell SQL Server I will be tweeting links as we go
EXPLORING THE SQL POWERSHELL MODULE
Presented by : Chirag Dani & Dhaval Shah
Test and Verify Instances with DBAchecks
Grow Your Script From Simple to Module
Cross-Platform, Cloud and On-Premise Database Tool
Use PowerShell & dbatools to Manage your SQL Server Environment
Mass Hunting and exploitation with powershell
NAVIGATING THE MINEFIELD
Gold Sponsors.
dbatools! The reason to finally start learning and using Powershell
Michael Wall Senior DBA, Great Western Malting
Automated Testing Strategies and Dynamics 365 Performance Management
Michelle Haarhues Keeping up with SSMS.
Disaster Recovery Done Dirt Cheap Founder Curnutt Data Solutions
What it is and why you should use it
Samuel Kastberg Scripting a BizTalk Server installation
Environment Automation
Life Hacks: dbatools Edition
Presentation transcript:

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? Start backup runner? Check scripts for hostname, instances, and restore point in time demos 11/18/2018

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. dfurgiuele@igsenergy.com @pittfurg http://www.port1433.com http://www.genesface.com 11/18/2018

AGENDA Why using PowerShell will make your life more awesome SQL Server PowerShell Provider Code, code, and more code! 11/18/2018

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/18/2018

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 Import the module Show the path Show databases, use get-member Show names and sizes 11/18/2018

Style Points Write scripts, not code Return objects where possible Let the provider do the work! Gross: Less Gross: Huh? 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=13.0.0.0,Culture=neutral,PublicKeyToken=89845dcd8080cc91“ $SQLSvr = “localhost” $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 Import-Module SQLPS $Databases = Get-ChildItem -Path "SQLSERVER:\SQL\LOCALHOST\DEFAULT\DATABASES" 11/18/2018

Let’s Just Do it! 11/18/2018

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 –instanceName sql2016 -databaseName AdventureWorks2014 -saveTo File -fileName C:\temp\Adventureworks2014_Objects.sql –Verbose To Database: .\Get-DatabaseObjects.ps1 -serverName localhost -instanceName sql2016 -databaseName AdventureWorks2014 -saveTo Database -repoServerName localhost -repoInstanceName sql2016 –repoDatabaseName Admin -repoSchemaName Repo -repoTableName ObjectsRepository –Verbose Create TWO new databases when you’re done, and run the generated script! 11/18/2018

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/18/2018

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! .\Start-SQLRestore.ps1 -servername localhost -instanceName sql2016 -databaseName AdventureWorks2014 -fullBackupFileLocation C:\Temp\backups\localhost\AdventureWorks2014\Full -differentialBackupFileLocation C:\Temp\backups\localhost\AdventureWorks2014\Differential -logBackupFileLocation C:\Temp\backups\localhost\AdventureWorks2014\Log -toPointInTime "08/06/2016 3:00 PM" -Verbose -ErrorAction Stop Show how it detects broken chains 11/18/2018

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/18/2018

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/18/2018

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/ Mike Fal (@mike_fal): http://www.mikefal.net/ Chrissy LaMaire (@cl): https://blog.netnerds.net/ Aaron Nelson (@sqlvariant): http://sqlvariant.com Projects: dbatools: https://dbatools.io/ 11/18/2018

PASS PowerShell Virtual Chapter 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… 11/18/2018

Questions? 11/18/2018

Thank you, sponsors! 11/18/2018

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. dfurgiuele@igsenergy.com @pittfurg http://www.port1433.com http://www.genesface.com 11/18/2018