SlideShare une entreprise Scribd logo
1  sur  13
PowerShell for SQL Server  Leonard Mwangi MCDBA, MCTS eTek Global Inc.
Agenda Introduction to PowerShell Lifecycle Language Usability PowerShell with SQL PowerShell difficulties
Introduction Command-line shell with an associated scripting language for automation. Heavily integrated with .NET Framework. Provides full access to COM and WMI allowing local and remote administration of Windows Systems.  Commands referred to as cmdlets - .NET Classes allowing implementation of specific operations.  Utilized by most Microsoft applications and Systems (Windows Server, MSSQL, Exchange Server, SCOM, SCDMP etc.
Lifecycle Code name “Monad” – Sept 2003  Version 1.0 released 2006 for Windows XP SP2/SP3 Version 2.0 Integrated with Windows 7 and Window Server 2008 R2. Downloadable for earlier versions of Windows.  Can execute four kinds of commands. Cmdlets – specialized commands implementing specific functions. PowerShell scripts – suffixed as .ps1 PowerShell functions Executable programs
Language .Net Aspect  Property  -- can get Method  -- can do  CMDLETS Integrated with command Line  Built with WMI in mind. Language format – verb-noun Arranged in hard drives Variables
Scripting Getting RAM from Local Machine Get-WmiObject Win32_ComputerSystem Getting RAM from Local Machine Option ExplicitDim objWMIService, objComputer, colComputerDim strLogonUser, strComputer strComputer = "."  Set objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!" _ & strComputer & "ootimv2") Set colComputer = objWMIService.ExecQuery _("Select * from Win32_ComputerSystem")  For Each objComputer in colComputerWscript.Echo "System Name: " & objComputer.Name _& vbCr & "Total RAM " & objComputer.TotalPhysicalMemoryNext  WScript.Quit
Usability - basics Functions like DOS Store everything as a drive psdrive Variables  $varname Displaying you variables after assigning it  $dt = Get-Date | $dt – Wont work  $dt = Get-Date ; $dt Can pass doss command to PowerShell $varname = "Localhost“ Piping and Pipeline Get-Service  | Sort-Object -Property ServiceType | Format-Table name, ServiceType, status, CanStop, -Auto Filtering   Get-Service | Where {$_.Status -eq "Stopped"} | ft name, status, CanStop -AutoSize Looping  For-each  {$_.} --  current iteration of the loop
Usability - basics Get-Member Using Alias  New-Alias myalias Get-Service Reference previously used commands  get-history Signing Scripts – configured to run in most secure mode (Restricted)  AllSigning, RemoreSigned, UnRestricted HELP
PowerShell & SQL  SQLPS – accessible from any where in SSMS dir| Get-Member From a table  dir | ft Name, RowCount, State, Owner, Triggers SQL Server Management Objects (SMO) – Objects collections for SQL Server Management. [reflection.assembly]::LoadWithPartialName("Microsoft.SQLServer.SMO") Assign variables
Useful SMO’s
Question & Answer Session
References	 http://technet.microsoft.com/en-us/library/ee221100.aspx - Owners Manual http://technet.microsoft.com/en-us/library/ee176868.aspx - PowerShell shortcuts http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx -- PowerShell http://www.microsoft.com/downloads/en/details.aspx?FamilyId=DF8ED469-9007-401C-85E7-46649A32D0E0&displaylang=en – PowerShell cheatSheet http://gallery.technet.microsoft.com/ScriptCenter/en-us/ - Microsoft Script Center http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.aspx - SMO Classes
Thank you for coming  Email:- leonard.mwangi@etekglobalinc.com Site:- www.etekglobalinc.com Blog:- www.geekswithblogs.neteonardm LinkedIn:- http://www.linkedin.com/in/lmundia Twitter:- lmundia

Contenu connexe

En vedette

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 

En vedette (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Power shell for sql server

  • 1. PowerShell for SQL Server Leonard Mwangi MCDBA, MCTS eTek Global Inc.
  • 2. Agenda Introduction to PowerShell Lifecycle Language Usability PowerShell with SQL PowerShell difficulties
  • 3. Introduction Command-line shell with an associated scripting language for automation. Heavily integrated with .NET Framework. Provides full access to COM and WMI allowing local and remote administration of Windows Systems. Commands referred to as cmdlets - .NET Classes allowing implementation of specific operations. Utilized by most Microsoft applications and Systems (Windows Server, MSSQL, Exchange Server, SCOM, SCDMP etc.
  • 4. Lifecycle Code name “Monad” – Sept 2003 Version 1.0 released 2006 for Windows XP SP2/SP3 Version 2.0 Integrated with Windows 7 and Window Server 2008 R2. Downloadable for earlier versions of Windows. Can execute four kinds of commands. Cmdlets – specialized commands implementing specific functions. PowerShell scripts – suffixed as .ps1 PowerShell functions Executable programs
  • 5. Language .Net Aspect Property -- can get Method -- can do CMDLETS Integrated with command Line Built with WMI in mind. Language format – verb-noun Arranged in hard drives Variables
  • 6. Scripting Getting RAM from Local Machine Get-WmiObject Win32_ComputerSystem Getting RAM from Local Machine Option ExplicitDim objWMIService, objComputer, colComputerDim strLogonUser, strComputer strComputer = "." Set objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!" _ & strComputer & "ootimv2") Set colComputer = objWMIService.ExecQuery _("Select * from Win32_ComputerSystem") For Each objComputer in colComputerWscript.Echo "System Name: " & objComputer.Name _& vbCr & "Total RAM " & objComputer.TotalPhysicalMemoryNext WScript.Quit
  • 7. Usability - basics Functions like DOS Store everything as a drive psdrive Variables $varname Displaying you variables after assigning it $dt = Get-Date | $dt – Wont work $dt = Get-Date ; $dt Can pass doss command to PowerShell $varname = "Localhost“ Piping and Pipeline Get-Service | Sort-Object -Property ServiceType | Format-Table name, ServiceType, status, CanStop, -Auto Filtering Get-Service | Where {$_.Status -eq "Stopped"} | ft name, status, CanStop -AutoSize Looping For-each {$_.} -- current iteration of the loop
  • 8. Usability - basics Get-Member Using Alias New-Alias myalias Get-Service Reference previously used commands get-history Signing Scripts – configured to run in most secure mode (Restricted) AllSigning, RemoreSigned, UnRestricted HELP
  • 9. PowerShell & SQL SQLPS – accessible from any where in SSMS dir| Get-Member From a table dir | ft Name, RowCount, State, Owner, Triggers SQL Server Management Objects (SMO) – Objects collections for SQL Server Management. [reflection.assembly]::LoadWithPartialName("Microsoft.SQLServer.SMO") Assign variables
  • 11. Question & Answer Session
  • 12. References http://technet.microsoft.com/en-us/library/ee221100.aspx - Owners Manual http://technet.microsoft.com/en-us/library/ee176868.aspx - PowerShell shortcuts http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx -- PowerShell http://www.microsoft.com/downloads/en/details.aspx?FamilyId=DF8ED469-9007-401C-85E7-46649A32D0E0&displaylang=en – PowerShell cheatSheet http://gallery.technet.microsoft.com/ScriptCenter/en-us/ - Microsoft Script Center http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.aspx - SMO Classes
  • 13. Thank you for coming Email:- leonard.mwangi@etekglobalinc.com Site:- www.etekglobalinc.com Blog:- www.geekswithblogs.neteonardm LinkedIn:- http://www.linkedin.com/in/lmundia Twitter:- lmundia