PowerShell Lessons Learned from Building an Automated SQL Installation and Patch Management Implementation Presented by: Fany Carolina Vargas, Microsoft.

Slides:



Advertisements
Similar presentations
Week 6: Chapter 6 Agenda Automation of SQL Server tasks using: SQL Server Agent Scheduling Scripting Technologies.
Advertisements

® IBM Software Group © 2010 IBM Corporation What’s New in Profiling & Code Coverage RAD V8 April 21, 2011 Kathy Chan
$$ Idea Updatable help system Method overload discovery HelpUri attribute support HelpFile property on FunctionInfo Simplified Where and.
Module 5: Creating and Configuring Group Policy
System Center Configuration Manager Push Software By, Teresa Behm.
Jason Himmelstein, MVP Senior Technical Director,
Automating SQL Buildouts With Hyper-V and SQL Server 2008 R2 Robert L Davis, Sr. DBA, Microsoft Corp.
James Boother Blog: INTRODUCTION TO POWERSHELL.
James Boother Blog: AUTOMATING ADMIN TASKS WITH POWERSHELL.
Great people, great experience, great passion Administering SharePoint with Windows PowerShell Go Beyond the Management Shell with SharePoint and Windows.
70-290: MCSE Guide to Managing a Microsoft Windows Server 2003 Environment Chapter 12: Managing and Implementing Backups and Disaster Recovery.
70-290: MCSE Guide to Managing a Microsoft Windows Server 2003 Environment, Enhanced Chapter 10: Server Administration.
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration Chapter 7 Configuring File Services in Windows Server 2008.
Slide 1 of 9 Presenting 24x7 Scheduler The art of computer automation Press PageDown key or click to advance.
VMware vCenter Server Module 4.
Connect with life L.Srividya Architect Advisor | Microsoft India.
Enterprise Reporting with Reporting Services SQL Server 2005 Donald Farmer Group Program Manager Microsoft Corporation.
System Center 2012 R2 Windows Azure Pack Service Management Automation 101.
Adding a Module The Import-Module cmdlet  Can be used to load any external module into PowerShell.  Uses the following syntax to add the ActiveDirectory.
70-290: MCSE Guide to Managing a Microsoft Windows Server 2003 Environment, Enhanced Chapter 12: Managing and Implementing Backups and Disaster Recovery.
FileSecure Implementation Training Patch Management Version 1.1.
Module 1: Installing Active Directory Domain Services
Hands-On Microsoft Windows Server 2008 Chapter 1 Introduction to Windows Server 2008.
Automate Microsoft Azure Ross Sponholtz Mark Ghazai.
Deploying and Managing Windows Server 2012
MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Database Security and Auditing: Protecting Data Integrity and Accessibility Chapter 3 Administration of Users.
70-290: MCSE Guide to Managing a Microsoft Windows Server 2003 Environment, Enhanced Chapter 12: Managing and Implementing Backups and Disaster Recovery.
Microsoft ® Official Course Module XA Using Windows PowerShell ®
SharePoint 2010 Development Environment A Guide to Setup SharePoint 2010 Development Environment on Windows 7 Machine.
Terry Henry IS System Manager, SharePoint SME Micron Technology Inc.
PowerShell Basics. o PowerShell is a great way to manipulate server and/or workstation components o It’s geared toward system administrators by creating.
Appendix A Starting Out with Windows PowerShell™ 2.0.
Section 1: Introducing Group Policy What Is Group Policy? Group Policy Scenarios New Group Policy Features Introduced with Windows Server 2008 and Windows.
Microsoft Application Virtualization 5.0: Introduction Mohnish Chaturvedi & Ian Bartlett Premier Field Engineer WCL312.
Security Planning and Administrative Delegation Lesson 6.
Microsoft SharePoint Server 2010 for the Microsoft ASP.NET Developer Yaroslav Pentsarskyy
Good Morning and Thank You!.  Have some Fun!  Learn at least one thing new!  Make myself available to you So please …  Ask questions and enjoy!
Module 1: Configuring Windows Server Module Overview Describe Windows Server 2008 roles Describe Windows Server 2008 features Describe Windows Server.
Module 13 Implementing Business Continuity. Module Overview Protecting and Recovering Content Working with Backup and Restore for Disaster Recovery Implementing.
CERN - IT Department CH-1211 Genève 23 Switzerland t DB Development Tools Benthic SQL Developer Application Express WLCG Service Reliability.
Managing System Center 2012 Configuration Manager with Windows PowerShell MEMUG August 23 rd 2013.
Module 5: Creating and Configuring Group Policies.
Microsoft Management Seminar Series SMS 2003 Change Management.
Virtual techdays INDIA │ 9-11 February 2011 Monitoring and managing remote Windows 7 desktops with Windows PowerShell Ravikanth C │ Dell Inc.
Windows PowerShell Introduction 1 Morgan Simonsen Ementor.
Service Management Automation: 3,2,1, Automate!
Mirek Sztajno SQL Server Security PM
 An alias is an alternative name assigned to a Cmdlet.  Aliases allow users to quickly interact with the shell.  The Cmdlet get-alias is.
Azure Automation Tao Yang & Pete Zerger ARC311 Microsoft Ignite 2015
POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access.
20409A 7: Installing and Configuring System Center 2012 R2 Virtual Machine Manager Module 7 Installing and Configuring System Center 2012 R2 Virtual.
 It is Microsoft's new task-based command- line shell and scripting language designed especially for system administration.  It helps Information Technology.
Module 14: Advanced Topics and Troubleshooting. Microsoft ® Windows ® Small Business Server (SBS) 2008 Management Console (Advanced Mode) Managing Windows.
 Version 1.0[edit]edit  Version 1.0 was released in 2006 for Windows XP SP2/SP3, Windows Server 2003,
Integrating and Extending Workflow 8 AA301 Carl Sykes Ed Heaney.
Planning Server Deployments Chapter 1. Server Deployment When planning a server deployment for a large enterprise network, the operating system edition.
Introduction to SQL Server Automation with Powershell by Chris Sommer.
Ravikanth C.
PowerShell 5 & Windows 10. What are we covering today? What is PowerShell? Why is PowerShell important? Some simple demos on Windows 10.
Start-SPPowerShell – Introduction to PowerShell for SharePoint Admins and Developers Paul BAker.
Managing, Storing, and Executing DTS Packages
Implementing Update Management
Deploying and Configuring SSIS Packages
Azure Automation and Logic Apps:
SharePoint Saturday Omaha April 2016
Microsoft Ignite NZ October 2016 SKYCITY, Auckland.
20409A 7: Installing and Configuring System Center 2012 R2 Virtual Machine Manager Module 7 Installing and Configuring System Center 2012 R2 Virtual.
PowerShell Best Practices for SQL DBA’s
Presentation transcript:

PowerShell Lessons Learned from Building an Automated SQL Installation and Patch Management Implementation Presented by: Fany Carolina Vargas, Microsoft Corp., Sr. PFE, SQL Dedicated Support Blog:

Agenda Not covered in this session: PowerShell how-to and syntax details Recommend Microsoft Virtual Academy JumpStart videos for this: courses/getting-started-with-powershell-3-0-jump- start courses/getting-started-with-powershell-3-0-jump- start PowerShell: Understanding Why Using PowerShell for SQL Tasks Lessons Learned from Automating SQL Deployments

PowerShell: Understanding Why

4 User feedback about system management experience Admin experience should be consistent –Command line tools are not always consistent in syntax and behavior –Often requires combination of GUI and command tools –Many of these tools must be downloaded separately from different places and have various version requirements and pre-requisites Difficult to interact between tools since most return strings and success/fail error codes VBScript often used for more complex logic, however it does not provide interactive sessions.NET programs often used to tap into needed APIs, but also not interactive session and requires creating Exes/Dlls History

5 Command Line Shell & Object-Oriented Scripting Language Designed for system administrators Built on.NET framework Built-in commands called “cmdlets” which may accept object parameters Verb-Noun naming convention Can return objects back to the shell for additional manipulation Have a consistent syntax (and many common parameters) Allows direct interaction and manipulation of.NET objects (like a.NET program) PowerShell Capabilities

6 Allows interactive commands and easy interaction with other executables (like a shell) Provides consistent navigation of various data stores (get-PSProvider, get- PSDRIVE) –Registry, SQL,File System can be enumerated via DIR,CD commands Extensible interface Can load providers via import-module Integrated with most Microsoft products (SQL,Hyper-V,Exhange, System Center Suite, SharePoint,etc) Integrated into Windows for local and remote management Key for Windows Server Core environments Common management framework PowerShell Capabilities

Using PowerShell for SQL Tasks

8 SQL2008 SQLPS.exe: limited shell, not all OS functionality SQL 2012 and higher No longer just a mini-shell. You can instead import SQL modules for access to pre-packaged SQL cmdlets Import-module SQLPS –This loads all SQL SMO classes, cmdlets and assemblies –Advanced users could optionally choose to import only needed assemblies (via.NET assembly load capabilities) SQL PowerShell Environment

9 Accessing “start PowerShell” option within via management studio starts “Program Files(x86)\Microsoft SQL Server\110\Tools\Binn\sqlps.exe” Sqlps always loads base pre-requisite powershell version for the SQL instance –SQL 2012 and SQL 2014 loads PowerShell 2.0 (can verify via get-host or $PSVersionTable) – SQL PowerShell Environment

10 General PSDrives: Allow object enumeration similar to file system –Get-PSDRIVE Discovery –Get-Help: learn how to use a commandlet –Get-Command: to find commands –Get-Member: enumerate properties and methods of an object Quick Concepts and Cmdlets Pipelines –Series of commands connected by the pipeline operator “|” Each command sends its results to the next command in the pipeline Example: Get-Process notepad | stop-Process SQL Invoke-SQLCmd invoke-PolicyEvaluation (PBM)

11 Bridge gap between SQL and objects external to SQL instance Collect inventory details about other services, service accounts, server info, instance info Smart Backup/Restore scripts –Check for old files : get-childitem \\Server1\share1\ServerName\*.bak | where-object { $_.LastWriteTime -ge (Get-Date).AddDays(-10) } | Select name, LastWriteTime\\Server1\share1\ServerName\*.bak –Tie drive space and file age logic into your backup scripts Enumerate and assign mount point permissions When to use PowerShell for SQL Tasks

12 Tie into Active Directory (example find + collect AD user properties for a SQL user and import details into a SQLtable) Tie in.NET objects to SQL objects easily –Example generate random password for a new SQLlogin –[Reflection.Assembly]::LoadWithPartialName(”System.Web” ) [System.Web.Security.Membership]::GeneratePassword(…….) Tie PBM xml files + PBM cmdlets for central Policy Based Management evaluation Automate SQL deployments –Write upgrade/Install scripts with logic to configure service accounts, folder permissions –Use PowerShell remoting capabilities for remote SQL deployments When to use PowerShell for SQL Tasks

13 Always On Availability Groups setup Extract cluster properties and IP addresses, feed that into configuration scripts Monitoring and Auditing Parse SQL errorlog Utilize WMI commandlets for server management –get-wmiobject Win32_QuickFixEngineering | findstr KB12345 Allow SQL agent jobs to execute complex logic Use PowerShell subsystem (be aware this is the more limited PowerShell environment) When to use PowerShell for SQL Tasks

14 Make use of other pre-packaged APIs, extensions, and cmdlets Example: codeplex.com has various powershell extensions for SQL Use SMO to script SQL objects Manipulate object properties directly and at a granular level Example: Instead of simply listing indexes, can extract and manipulate each index object directly Can minimize amount of code needed Work with variables more easily(remove the need for dynamic building of T-SQL statements) Utilize PowerShell remoting and parallelism capabilities (Jobs/Workflows) When to use PowerShell for SQL Tasks

Lessons Learned from Automating SQL Deployments

16 PowerShell to script SQL installations and configuration Call into pre-existing T-SQL scripts via invoke-sqlcmd Make use of command line SQL setup options, configuration files, custom XML configuration files System Center suite Orchestrator to create run-books and workflows Virtual Machine Manager to configure VMs Deployment Tools Used

17 Create scripts which can run locally, and then create a parent script for remoting. Pros: –Easier administration and troubleshooting (can run logic locally if needed) –Easier per server transactional processing Cons: –Additional complexity when passing variables to child scripts + all commands must be able to run from remote servers as well. –Error prone if PowerShell versions are not consistent across servers –Remote server needs to be able to execute the exact command (versus the –ComputerName option which may translate nicely to a different remote command) Lessons Learned

18 Beware of possible one-at-a-time processing with pipelines get-process | get-member != get-member -inputobject (get-process) If script requires access to registry, make sure proper bitness of powershell.exe is being used, otherwise you will incorrectly access 32-bit registry keys $osObj=get-wmiobject -Class Win32_Processor $constOSArchitecture =$osObj.Architecture #if running 32 bit powershell, but OS is 64 bit if($env:PROCESSOR_ARCHITECTURE -eq "x86" -and ( -not ($constOSArchitecture -eq 0) ) ){ Write-host("Running 32-bit powershell in 64-bit OS. Script cannot continue. Please restart powershell in 64-bit mode.") } Lessons Learned

19 Remember that while writing PowerShell scripts is very similar to.NET programming, it is also a shell and pipeline buffer is being built dynamically Problem may be specific to how data is passed to the shell –Example: Issue with different output formatting on older versions of PowerShell Lessons Learned

20 For easier administration: Store and execute scripts centrally (UNC share) Place script output centrally (UNC share) Use latest version of PowerShell on central management server Establish a standard PowerShell version within your environment Document the required minimum PowerShell version If script uses newer constructs, specify minimum version and indicate why that is the minimum required version in a comment: #requires -Version 2.0 #requires -Version 3.0 Lessons Learned

21 Running scripts from UNC share remotely requires PowerShell impersonation (access denied error otherwise) Client needs to specify list of machines which can delegate its credentials Enable-WSManCredSSP -role client -DelegateComputer $servernameArr Server specifies it intends to delegate (this requires elevation) Enable-WSManCredSSP -role server –Force (on the server itself) -or- Connect-WSMan $svr Set-Item WSMan:\$svr\Service\Auth\CredSSP -Value $true powershell-quot-second-hop-quot-functionality-with-credssp.aspx Lessons Learned

22 Prompt for passwords instead of storing within INI files Collect password via read-host AsSecureString and SecureStringToBSTR Internet downloaded files must be unblocked Use commandlet: unblock-file For W2012 or higher Use Mount-DiskImage to easily mount ISO files Lessons Learned

23 Automating SSDT and SSDT-BI installation Both are separate from SQL installation and require different params SSDT-BI command line install not well documented yet ting-the-installation-of-my-bi-demo-platform-with-data-explorer-geoflow- visual-studio-2012-bi-projects-and-database-projects-sql-server-2012-sp1- cu3.aspx SSDTBI_x86_ENU.exe /ACTION=INSTALL /FEATURES=SSDTBI,SNAC_SDK /Q /IACCEPTSQLSERVERLICENSETERMS Lessons Learned

24 SSDT (Visual Studio) command line install not documented (by design) documentation-on-admindeployment-xml Passing Variables to Invoke-Command script blocks can be tricky Invoke-Command -Session $s1 -ScriptBlock{param($arg2) &"\\Share1\scrip1.ps1" $arg2} -ArgumentList $arg1 Lessons Learned

25 Make use of classes (available in PowerShell 2.0 +) for cleaner Object Oriented code public class SQLPatch{public string DisplayName; public string LastUsedSource; public string PackageName; public string Installed; public string LocalPackage; Add-Type -TypeDefinition $def $SQLPatch = New-Object SQLPatch

Demo: Walkthrough PowerShell Script Snippet 26

27 PowerShell the SQL Server Way 10 Tips for the SQL Server PowerShell Scripter powershell-scripter.aspx powershell-scripter.aspx Technet Script Center Windows Powershell Blog Hey, Scripting Guy Blog Technet Script Center Windows PowerShell Quick Reference us/download/details.aspx?id=7097https:// us/download/details.aspx?id=7097 References