SlideShare a Scribd company logo
1 of 21
A DBA's first PowerShell
script: lessons learned.




David Cobb
SQL Consultant and Trainer
MCITP SQL 2008 DBA,Dev,BI
sql@davidcobb.net
About Dave
• Computer consultant since 1996
   • Background in technical support, web application design, network administration
   • Primarily Windows, SQL Server
   • Dabble in Linux, Opensource
• Microsoft Certified SQL Trainer since 2002
• MCITP, MCAD, MCSE 2000(I like taking tests.)
• Lead I.T. Engineer with CheckAlt Payment Solutions providing Check21 Remote
  Deposit Capture solutions.
• Enjoy helping my clients solve their I.T. problems
• PowerShell Student and Fan 
• http://daveslog.com
• sql@davidcobb.net I like emails.(..when they’re from people)
PowerShell NOOB
• Familiar with Windows and SQL
• Some Development Background, more on the Admin side
• Interest in PowerShell from reading SQL bloggers
• Just needed a problem to solve to learn on the job
• Let's walk through that problem and my first solution
• Comments and discussion welcome
The Problem
• "Write us a script to restore the latest backups from the network
  drive onto the development servers"
• Alright should be easy.
• Well...
Environment
• SQL 2012 on Win 2008 R2
• I have Powershell 2 and SQLPS
• I can install Powershell 3 for development, use the Integrated
  Scripting Environment /ISE
My Initial Approach
• Break the problem into pieces, solve each piece:
   • Set starting variables
   • Connect to network share
   • Repeat for each requested server:
      • Repeat for each requested database:
          • Find matching backups
          • Find latest backup
          • Restore that backup
   • Done! Super easy piece of cake. Thank you for coming!
Where do I start!!??
• I jumped in by reading blogs with PowerShell tutorials.
• Copied and pasted their code, and ran it, tweaked and ran again.
• Used the ISE to set breakpoints, look at objects in the PowerShell pipeline.



• Be on the lookout for my new book:

How to Copy and Paste your way to an I.T. Career!*


Credit: Joe Lopez, former boss and great guy
But there were the Gotchas…
• Network drives
• Restore and filenames
• PowerShell and SQL version differences on different servers
• Execution Policy
• SQL Cmdlet bug!
Gotcha 1:
Network drives don't work consistently
• Initially using script over network drive works. Man I am a GURU!
• Then I get errors…
• After a few hours of frustration, I discover PSDrive 



New-PSDrive -name "Y" -PSProvider FileSystem -Root $BackupRoot
Gotcha 2:
  Data and log files aren't in the same location in
  production and development

• Restoring a database when the paths for data and log are different means
  you need WITH MOVE command clause
• How do I move data files with RESTORE, I don’t know the logical name!
• My approach:
  See Code
• Next time: (From Get-Help Restore-SQLDatabase –examples)
 $RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("MyDB_Data",
 "c:MySQLServerMyDB.mdf") $RelocateLog = New-Object
 Microsoft.SqlServer.Management.Smo.RelocateFile("MyDB_Log", "c:MySQLServerMyDB.ldf")
 Restore-SqlDatabase -ServerInstance ComputerInstance -Database MyDB -BackupFile
 "sharefolderMyDB.trn" -RelocateFile @($RelocateData,$RelocateLog)
Client: "Oh can we have that in a SQL
Job“
Trickier that it looked at first
• SQL Jobs can run PowerShell scripts, but.. I have to paste in the script
  to the job step, hard to update on multiple machines
• I can run a script stored in a central file share, and execute it with a
  CmdExec SQL Agent Job Task...but ExecutionPolicy prevents this.
• My ‘Solution’:
   • If I use ByPass to circumvent execution policy, like so:
     powershell -version 2 -ExecutionPolicy ByPass -Command "&
     servershare$RestoreDBsFromSource.ps1"
   • Runs fine from SQL Service Account with read permission on the network
     share.
   • Anything bother you about that?
This raises a question!
• Hey, so I can run PowerShell scripts with BYPASS without Admin rights?
• Yep:
  http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/04/run-
  powershell-scripts-stored-on-a-central-file-share.aspx
 "...Interestingly, admin rights are not required to launch PowerShell in bypass
mode. ... If you want to restrict it from users, then you should use software
restriction policies. But keep in mind, that would preclude you from using Windows
PowerShell for your logon scripts and for many other administrative purposes...."
• This bears further investigation 

• But the script works, client happy, but soon..
Client:
“Can you make it work in 2008 R2 for our 'new' Dev
servers?”
Gotcha 3:
 • Problem:
     SQL 2008 R2 Can't use Import-Module SQLPS
 • Solution:
    • Michiel Wories and his Initialize-SqlPsEnvironment.ps1




    Let’s just take a moment to say…
PowerShell Community is Awesome 
• The amount and quality of help resources out there is amazing.
• Find them, read them, apply what you learn and thank them. 




• Thanks PowerShell People!
Gotcha 4:
 •   I can connect to SQL now, but…
 •   I run and backups timeout after 30 seconds. Worked in SQL 2012!
 •   Now Invoke-SqlCmd times out, why?
 •   Invoke-SqlCmd bug in sql 2008 R2, doesn't respect timeout parameter
 •   http://www.scarydba.com/2010/04/16/powershell-smo-problem/
 •   Chad Miller to the rescue with Invoke-SqlCmd2 (Thanks again!)
Client:
   “Can you make it work with a parameter with a list of Databases we want?”
• Actually easier than I thought..
  Change to the code:
param
([parameter(Mandatory = $true)]
[string[]]$DBList)

   And the call:
powershell -version 2 -ExecutionPolicy ByPass -Command "&
davepcscripts$RestoreDBsFromSourceV2.ps1" Northwind,Products,Foo


• For V3, I plan to create parameters with defaults for my other initial
  variables.
My Process
• Break your scripting problem down to small tasks
• Solve each one in turn
• Someone out there has probably solved your problem first
• Good enough for now is OK, make a note to refine it later
• Find, copy, paste, UNDERSTAND, modify, test, refine, repeat!
Resources
• Whatever it is you’re doing with PowerShell, someone has probably
  done it before and blogged about it!
• Take advantage of the excellent free resources out there for learning
  PowerShell.
• Read other people’s code, and adapt for your needs.
• Use the tools!
PowerShell Help
• Stackoverflow.com
 http://stackoverflow.com/questions/tagged/PowerShell
 (Hard questions, great answers, great explanations)
• PowerShell Community Resources
   • Technet PowerShell Communities
   • PowerShellCommunity.org
   • Florida PowerShell User Group - Resources



• Just search the web  many great bloggers, may great resources
References
• Dr Tobias Weltner’s Mastering PowerShell
  http://PowerShell.com/Mastering-PowerShell.pdf
• PowerShell V2 Owners Manual
  http://bit.ly/P0s0g4
• Windows PowerShell 3.0 and Server Manager Quick Reference Guides
  http://bit.ly/LaojTT
• Stairway to SQL PowerShell
  http://bit.ly/MkM62G
• Running PowerShell 2 and 3 side by side
  http://bit.ly/tPkfAq

More Related Content

What's hot

Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Librarieselliando dias
 
Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slideselliando dias
 
Social Connections 2015 CrossWorlds and Domino
Social Connections 2015 CrossWorlds and DominoSocial Connections 2015 CrossWorlds and Domino
Social Connections 2015 CrossWorlds and DominoPaul Withers
 
How NOT to get lost in the current JavaScript landscape
How NOT to get lost in the current JavaScript landscapeHow NOT to get lost in the current JavaScript landscape
How NOT to get lost in the current JavaScript landscapeRadosław Scheibinger
 
Saving Time By Testing With Jest
Saving Time By Testing With JestSaving Time By Testing With Jest
Saving Time By Testing With JestBen McCormick
 
Introduction to Play Framework
Introduction to Play FrameworkIntroduction to Play Framework
Introduction to Play FrameworkWarren Zhou
 
Using Play Framework 2 in production
Using Play Framework 2 in productionUsing Play Framework 2 in production
Using Play Framework 2 in productionChristian Papauschek
 
Untangling - fall2017 - week 8
Untangling - fall2017 - week 8Untangling - fall2017 - week 8
Untangling - fall2017 - week 8Derek Jacoby
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumNgoc Dao
 
Xitrum HOWTOs
Xitrum HOWTOsXitrum HOWTOs
Xitrum HOWTOsNgoc Dao
 
Leveraging Open Source for Database Development: Database Version Control wit...
Leveraging Open Source for Database Development: Database Version Control wit...Leveraging Open Source for Database Development: Database Version Control wit...
Leveraging Open Source for Database Development: Database Version Control wit...All Things Open
 
5 Common Mistakes You are Making on your Website
 5 Common Mistakes You are Making on your Website 5 Common Mistakes You are Making on your Website
5 Common Mistakes You are Making on your WebsiteAcquia
 
Node.js, toy or power tool?
Node.js, toy or power tool?Node.js, toy or power tool?
Node.js, toy or power tool?Ovidiu Dimulescu
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agilityelliando dias
 
SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...
SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...
SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...Sencha
 
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi   SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi Sencha
 
Adobe CQ5 for Developers - Introduction
Adobe CQ5 for Developers - IntroductionAdobe CQ5 for Developers - Introduction
Adobe CQ5 for Developers - IntroductionTekno Point
 

What's hot (20)

Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
 
Social Connections 2015 CrossWorlds and Domino
Social Connections 2015 CrossWorlds and DominoSocial Connections 2015 CrossWorlds and Domino
Social Connections 2015 CrossWorlds and Domino
 
How NOT to get lost in the current JavaScript landscape
How NOT to get lost in the current JavaScript landscapeHow NOT to get lost in the current JavaScript landscape
How NOT to get lost in the current JavaScript landscape
 
Saving Time By Testing With Jest
Saving Time By Testing With JestSaving Time By Testing With Jest
Saving Time By Testing With Jest
 
Introduction to Play Framework
Introduction to Play FrameworkIntroduction to Play Framework
Introduction to Play Framework
 
Advanced Zen
Advanced ZenAdvanced Zen
Advanced Zen
 
Day 4 - Models
Day 4 - ModelsDay 4 - Models
Day 4 - Models
 
Using Play Framework 2 in production
Using Play Framework 2 in productionUsing Play Framework 2 in production
Using Play Framework 2 in production
 
Untangling - fall2017 - week 8
Untangling - fall2017 - week 8Untangling - fall2017 - week 8
Untangling - fall2017 - week 8
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
 
Xitrum HOWTOs
Xitrum HOWTOsXitrum HOWTOs
Xitrum HOWTOs
 
Leveraging Open Source for Database Development: Database Version Control wit...
Leveraging Open Source for Database Development: Database Version Control wit...Leveraging Open Source for Database Development: Database Version Control wit...
Leveraging Open Source for Database Development: Database Version Control wit...
 
5 Common Mistakes You are Making on your Website
 5 Common Mistakes You are Making on your Website 5 Common Mistakes You are Making on your Website
5 Common Mistakes You are Making on your Website
 
Node.js, toy or power tool?
Node.js, toy or power tool?Node.js, toy or power tool?
Node.js, toy or power tool?
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...
SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...
SenchaCon 2016: A Look Ahead: Survey Next-Gen Modern Browser APIs - Shikhir S...
 
COScheduler
COSchedulerCOScheduler
COScheduler
 
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi   SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
 
Adobe CQ5 for Developers - Introduction
Adobe CQ5 for Developers - IntroductionAdobe CQ5 for Developers - Introduction
Adobe CQ5 for Developers - Introduction
 

Viewers also liked

Enter the Dragon - SQL 2014 on Server Core PASS Summit 2014 Edition
Enter the Dragon -  SQL 2014 on Server Core PASS Summit 2014 EditionEnter the Dragon -  SQL 2014 on Server Core PASS Summit 2014 Edition
Enter the Dragon - SQL 2014 on Server Core PASS Summit 2014 EditionMark Broadbent
 
Enter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto Edition
Enter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto EditionEnter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto Edition
Enter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto EditionMark Broadbent
 
SQL 2012 and Powershell for the Bleeding Edge DBA
SQL 2012 and Powershell for the Bleeding Edge DBASQL 2012 and Powershell for the Bleeding Edge DBA
SQL 2012 and Powershell for the Bleeding Edge DBAdpcobb
 
PowerShell Functions
PowerShell FunctionsPowerShell Functions
PowerShell Functionsmikepfeiffer
 
Power shell training
Power shell trainingPower shell training
Power shell trainingPedro Lopez
 
Intro to PowerShell Workflow
Intro to PowerShell WorkflowIntro to PowerShell Workflow
Intro to PowerShell WorkflowJeffery Hicks
 
Introduction To Power Shell
Introduction To Power ShellIntroduction To Power Shell
Introduction To Power ShellIvan Suhinin
 
Ive got a powershell secret
Ive got a powershell secretIve got a powershell secret
Ive got a powershell secretChris Conte
 
SQL Server AlwaysOn for Dummies SQLSaturday #202 Edition
SQL Server AlwaysOn for Dummies SQLSaturday #202 EditionSQL Server AlwaysOn for Dummies SQLSaturday #202 Edition
SQL Server AlwaysOn for Dummies SQLSaturday #202 EditionMark Broadbent
 
Power shell training
Power shell trainingPower shell training
Power shell trainingDavid Brabant
 
An Introduction to Windows PowerShell
An Introduction to Windows PowerShellAn Introduction to Windows PowerShell
An Introduction to Windows PowerShellDale Lane
 
Introduction to powershell
Introduction to powershellIntroduction to powershell
Introduction to powershellSalaudeen Rajack
 

Viewers also liked (14)

Enter the Dragon - SQL 2014 on Server Core PASS Summit 2014 Edition
Enter the Dragon -  SQL 2014 on Server Core PASS Summit 2014 EditionEnter the Dragon -  SQL 2014 on Server Core PASS Summit 2014 Edition
Enter the Dragon - SQL 2014 on Server Core PASS Summit 2014 Edition
 
Enter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto Edition
Enter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto EditionEnter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto Edition
Enter The Dragon - SQL 2014 on Server Core - SQLSaturday #341 Porto Edition
 
SQL 2012 and Powershell for the Bleeding Edge DBA
SQL 2012 and Powershell for the Bleeding Edge DBASQL 2012 and Powershell for the Bleeding Edge DBA
SQL 2012 and Powershell for the Bleeding Edge DBA
 
SQL Server 2014 Backup to Azure - SQL Saturday CR 2015
SQL Server 2014 Backup to Azure - SQL Saturday CR 2015SQL Server 2014 Backup to Azure - SQL Saturday CR 2015
SQL Server 2014 Backup to Azure - SQL Saturday CR 2015
 
PowerShell Functions
PowerShell FunctionsPowerShell Functions
PowerShell Functions
 
Power shell training
Power shell trainingPower shell training
Power shell training
 
Intro to PowerShell Workflow
Intro to PowerShell WorkflowIntro to PowerShell Workflow
Intro to PowerShell Workflow
 
Introduction To Power Shell
Introduction To Power ShellIntroduction To Power Shell
Introduction To Power Shell
 
Ive got a powershell secret
Ive got a powershell secretIve got a powershell secret
Ive got a powershell secret
 
SQL Server AlwaysOn for Dummies SQLSaturday #202 Edition
SQL Server AlwaysOn for Dummies SQLSaturday #202 EditionSQL Server AlwaysOn for Dummies SQLSaturday #202 Edition
SQL Server AlwaysOn for Dummies SQLSaturday #202 Edition
 
Power shell training
Power shell trainingPower shell training
Power shell training
 
Sql server infernals
Sql server infernalsSql server infernals
Sql server infernals
 
An Introduction to Windows PowerShell
An Introduction to Windows PowerShellAn Introduction to Windows PowerShell
An Introduction to Windows PowerShell
 
Introduction to powershell
Introduction to powershellIntroduction to powershell
Introduction to powershell
 

Similar to My first powershell script

Yapc10 Cdt World Domination
Yapc10   Cdt World DominationYapc10   Cdt World Domination
Yapc10 Cdt World DominationcPanel
 
Untangling spring week10
Untangling spring week10Untangling spring week10
Untangling spring week10Derek Jacoby
 
PowerShellForDBDevelopers
PowerShellForDBDevelopersPowerShellForDBDevelopers
PowerShellForDBDevelopersBryan Cafferky
 
Dev ops lessons learned - Michael Collins
Dev ops lessons learned  - Michael CollinsDev ops lessons learned  - Michael Collins
Dev ops lessons learned - Michael CollinsDevopsdays
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPressTaylor Lovett
 
Evolutionary Database Design
Evolutionary Database DesignEvolutionary Database Design
Evolutionary Database DesignAndrei Solntsev
 
Building a Simple Theme Framework
Building a Simple Theme FrameworkBuilding a Simple Theme Framework
Building a Simple Theme FrameworkJoe Casabona
 
Add-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his DutyAdd-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his Dutyreedmaniac
 
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...44CON
 
BTV PHP - Building Fast Websites
BTV PHP - Building Fast WebsitesBTV PHP - Building Fast Websites
BTV PHP - Building Fast WebsitesJonathan Klein
 
A tale of 3 databases
A tale of 3 databasesA tale of 3 databases
A tale of 3 databasesChris Skardon
 
Best Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsBest Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsTaylor Lovett
 
Features, Exportables & You
Features, Exportables & YouFeatures, Exportables & You
Features, Exportables & Youjskulski
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Drupalcon Paris
 
Managing SharePoint Anywhere with Windows PowerShell
Managing SharePoint Anywhere with Windows PowerShellManaging SharePoint Anywhere with Windows PowerShell
Managing SharePoint Anywhere with Windows PowerShellRyan Dennis
 
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?DATAVERSITY
 
pandas.(to/from)_sql is simple but not fast
pandas.(to/from)_sql is simple but not fastpandas.(to/from)_sql is simple but not fast
pandas.(to/from)_sql is simple but not fastUwe Korn
 

Similar to My first powershell script (20)

Yapc10 Cdt World Domination
Yapc10   Cdt World DominationYapc10   Cdt World Domination
Yapc10 Cdt World Domination
 
Untangling spring week10
Untangling spring week10Untangling spring week10
Untangling spring week10
 
PowerShellForDBDevelopers
PowerShellForDBDevelopersPowerShellForDBDevelopers
PowerShellForDBDevelopers
 
Dev ops lessons learned - Michael Collins
Dev ops lessons learned  - Michael CollinsDev ops lessons learned  - Michael Collins
Dev ops lessons learned - Michael Collins
 
Why ruby and rails
Why ruby and railsWhy ruby and rails
Why ruby and rails
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPress
 
Evolutionary Database Design
Evolutionary Database DesignEvolutionary Database Design
Evolutionary Database Design
 
Building a Simple Theme Framework
Building a Simple Theme FrameworkBuilding a Simple Theme Framework
Building a Simple Theme Framework
 
Add-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his DutyAdd-On Development: EE Expects that Every Developer will do his Duty
Add-On Development: EE Expects that Every Developer will do his Duty
 
presentation
presentationpresentation
presentation
 
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
 
BTV PHP - Building Fast Websites
BTV PHP - Building Fast WebsitesBTV PHP - Building Fast Websites
BTV PHP - Building Fast Websites
 
A tale of 3 databases
A tale of 3 databasesA tale of 3 databases
A tale of 3 databases
 
Best Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsBest Practices for Building WordPress Applications
Best Practices for Building WordPress Applications
 
Features, Exportables & You
Features, Exportables & YouFeatures, Exportables & You
Features, Exportables & You
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3
 
Managing SharePoint Anywhere with Windows PowerShell
Managing SharePoint Anywhere with Windows PowerShellManaging SharePoint Anywhere with Windows PowerShell
Managing SharePoint Anywhere with Windows PowerShell
 
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
 
<?php + WordPress
<?php + WordPress<?php + WordPress
<?php + WordPress
 
pandas.(to/from)_sql is simple but not fast
pandas.(to/from)_sql is simple but not fastpandas.(to/from)_sql is simple but not fast
pandas.(to/from)_sql is simple but not fast
 

My first powershell script

  • 1. A DBA's first PowerShell script: lessons learned. David Cobb SQL Consultant and Trainer MCITP SQL 2008 DBA,Dev,BI sql@davidcobb.net
  • 2. About Dave • Computer consultant since 1996 • Background in technical support, web application design, network administration • Primarily Windows, SQL Server • Dabble in Linux, Opensource • Microsoft Certified SQL Trainer since 2002 • MCITP, MCAD, MCSE 2000(I like taking tests.) • Lead I.T. Engineer with CheckAlt Payment Solutions providing Check21 Remote Deposit Capture solutions. • Enjoy helping my clients solve their I.T. problems • PowerShell Student and Fan  • http://daveslog.com • sql@davidcobb.net I like emails.(..when they’re from people)
  • 3. PowerShell NOOB • Familiar with Windows and SQL • Some Development Background, more on the Admin side • Interest in PowerShell from reading SQL bloggers • Just needed a problem to solve to learn on the job • Let's walk through that problem and my first solution • Comments and discussion welcome
  • 4. The Problem • "Write us a script to restore the latest backups from the network drive onto the development servers" • Alright should be easy. • Well...
  • 5. Environment • SQL 2012 on Win 2008 R2 • I have Powershell 2 and SQLPS • I can install Powershell 3 for development, use the Integrated Scripting Environment /ISE
  • 6. My Initial Approach • Break the problem into pieces, solve each piece: • Set starting variables • Connect to network share • Repeat for each requested server: • Repeat for each requested database: • Find matching backups • Find latest backup • Restore that backup • Done! Super easy piece of cake. Thank you for coming!
  • 7. Where do I start!!?? • I jumped in by reading blogs with PowerShell tutorials. • Copied and pasted their code, and ran it, tweaked and ran again. • Used the ISE to set breakpoints, look at objects in the PowerShell pipeline. • Be on the lookout for my new book: How to Copy and Paste your way to an I.T. Career!* Credit: Joe Lopez, former boss and great guy
  • 8. But there were the Gotchas… • Network drives • Restore and filenames • PowerShell and SQL version differences on different servers • Execution Policy • SQL Cmdlet bug!
  • 9. Gotcha 1: Network drives don't work consistently • Initially using script over network drive works. Man I am a GURU! • Then I get errors… • After a few hours of frustration, I discover PSDrive  New-PSDrive -name "Y" -PSProvider FileSystem -Root $BackupRoot
  • 10. Gotcha 2: Data and log files aren't in the same location in production and development • Restoring a database when the paths for data and log are different means you need WITH MOVE command clause • How do I move data files with RESTORE, I don’t know the logical name! • My approach: See Code • Next time: (From Get-Help Restore-SQLDatabase –examples) $RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("MyDB_Data", "c:MySQLServerMyDB.mdf") $RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("MyDB_Log", "c:MySQLServerMyDB.ldf") Restore-SqlDatabase -ServerInstance ComputerInstance -Database MyDB -BackupFile "sharefolderMyDB.trn" -RelocateFile @($RelocateData,$RelocateLog)
  • 11. Client: "Oh can we have that in a SQL Job“ Trickier that it looked at first • SQL Jobs can run PowerShell scripts, but.. I have to paste in the script to the job step, hard to update on multiple machines • I can run a script stored in a central file share, and execute it with a CmdExec SQL Agent Job Task...but ExecutionPolicy prevents this. • My ‘Solution’: • If I use ByPass to circumvent execution policy, like so: powershell -version 2 -ExecutionPolicy ByPass -Command "& servershare$RestoreDBsFromSource.ps1" • Runs fine from SQL Service Account with read permission on the network share. • Anything bother you about that?
  • 12. This raises a question! • Hey, so I can run PowerShell scripts with BYPASS without Admin rights? • Yep: http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/04/run- powershell-scripts-stored-on-a-central-file-share.aspx "...Interestingly, admin rights are not required to launch PowerShell in bypass mode. ... If you want to restrict it from users, then you should use software restriction policies. But keep in mind, that would preclude you from using Windows PowerShell for your logon scripts and for many other administrative purposes...." • This bears further investigation  • But the script works, client happy, but soon..
  • 13. Client: “Can you make it work in 2008 R2 for our 'new' Dev servers?”
  • 14. Gotcha 3: • Problem: SQL 2008 R2 Can't use Import-Module SQLPS • Solution: • Michiel Wories and his Initialize-SqlPsEnvironment.ps1 Let’s just take a moment to say…
  • 15. PowerShell Community is Awesome  • The amount and quality of help resources out there is amazing. • Find them, read them, apply what you learn and thank them.  • Thanks PowerShell People!
  • 16. Gotcha 4: • I can connect to SQL now, but… • I run and backups timeout after 30 seconds. Worked in SQL 2012! • Now Invoke-SqlCmd times out, why? • Invoke-SqlCmd bug in sql 2008 R2, doesn't respect timeout parameter • http://www.scarydba.com/2010/04/16/powershell-smo-problem/ • Chad Miller to the rescue with Invoke-SqlCmd2 (Thanks again!)
  • 17. Client: “Can you make it work with a parameter with a list of Databases we want?” • Actually easier than I thought.. Change to the code: param ([parameter(Mandatory = $true)] [string[]]$DBList) And the call: powershell -version 2 -ExecutionPolicy ByPass -Command "& davepcscripts$RestoreDBsFromSourceV2.ps1" Northwind,Products,Foo • For V3, I plan to create parameters with defaults for my other initial variables.
  • 18. My Process • Break your scripting problem down to small tasks • Solve each one in turn • Someone out there has probably solved your problem first • Good enough for now is OK, make a note to refine it later • Find, copy, paste, UNDERSTAND, modify, test, refine, repeat!
  • 19. Resources • Whatever it is you’re doing with PowerShell, someone has probably done it before and blogged about it! • Take advantage of the excellent free resources out there for learning PowerShell. • Read other people’s code, and adapt for your needs. • Use the tools!
  • 20. PowerShell Help • Stackoverflow.com http://stackoverflow.com/questions/tagged/PowerShell (Hard questions, great answers, great explanations) • PowerShell Community Resources • Technet PowerShell Communities • PowerShellCommunity.org • Florida PowerShell User Group - Resources • Just search the web  many great bloggers, may great resources
  • 21. References • Dr Tobias Weltner’s Mastering PowerShell http://PowerShell.com/Mastering-PowerShell.pdf • PowerShell V2 Owners Manual http://bit.ly/P0s0g4 • Windows PowerShell 3.0 and Server Manager Quick Reference Guides http://bit.ly/LaojTT • Stairway to SQL PowerShell http://bit.ly/MkM62G • Running PowerShell 2 and 3 side by side http://bit.ly/tPkfAq