PowerShell for SQL Server <br />Leonard Mwangi MCDBA, MCTS<br />eTek Global Inc.<br />
Agenda<br />Introduction to PowerShell<br />Lifecycle<br />Language<br />Usability<br />PowerShell with SQL<br />PowerShel...
Introduction<br />Command-line shell with an associated scripting language for automation.<br />Heavily integrated with .N...
Lifecycle<br />Code name “Monad” – Sept 2003 <br />Version 1.0 released 2006 for Windows XP SP2/SP3<br />Version 2.0 Integ...
Language<br />.Net Aspect <br />Property  -- can get<br />Method  -- can do <br />CMDLETS<br />Integrated with command Lin...
Scripting<br />Getting RAM from Local Machine<br />Get-WmiObject Win32_ComputerSystem<br />Getting RAM from Local Machine<...
Usability - basics<br />Functions like DOS<br />Store everything as a drive<br />psdrive<br />Variables <br />$varname<br ...
Usability - basics<br />Get-Member<br />Using Alias <br />New-Alias myalias Get-Service<br />Reference previously used com...
PowerShell & SQL <br />SQLPS – accessible from any where in SSMS<br />dir| Get-Member<br />From a table <br />dir | ft Nam...
Useful SMO’s<br />
Question & Answer Session <br />
References	<br />http://technet.microsoft.com/en-us/library/ee221100.aspx - Owners Manual<br />http://technet.microsoft.co...
Thank you for coming <br />Email:- leonard.mwangi@etekglobalinc.com<br />Site:- www.etekglobalinc.com<br />Blog:- www.geek...
Upcoming SlideShare
Loading in …5
×

Power shell for sql server

2,629 views

Published on

Unleashing Powershell to

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,629
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
25
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Power shell for sql server

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

×