O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Demo for Why Use PowerShell

Demo of various ways to invoke PowerShell and how to run administrative commands

  • Entre para ver os comentários

Demo for Why Use PowerShell

  1. 1. Why Use PowerShell(demo)<br />Siraj Jamdar<br />
  2. 2. Invoke PowerShell<br />From the command line:<br />Windows+R-> sqlps<br />Limited shell by design (to protect the innocent from themselves)<br />
  3. 3. Invoke PowerShell from SQL<br />Context sensitive in SSMS<br />Right-Click -> pick Start PowerShelloption<br />My favourite, pretty snappy and useful<br />Automatically traverses to the correct level<br />
  4. 4. Invoke PowerShell from SQLAgent<br />SQL Agent Job Step<br />Pick PowerShell from the Type dropdown list and paste the script in the box<br />Dreadfully slow… <br />I’d use an SSIS package instead<br />Use windows scheduler, better integration with Event Viewer logs<br />SCOM raises alerts from Event Viewer logs<br />
  5. 5. Invoke PowerShell<br />The hard way<br />Start <br /><ul><li> All Programs
  6. 6. Accessories
  7. 7. Windows PowerShell
  8. 8. Windows PowerShell</li></ul>PowerShellcorrupts ;-)<br />
  9. 9. Invoke PowerShell<br />To get any real work done …<br />runas /noprofile /user:anotherDomainJamdarSi_adm %SystemRoot%system32WindowsPowerShell v1.0powershell.exe<br />runas /noprofile /user:yetanotherDomainSJamdar%SystemRoot%system32WindowsPowerShell v1.0powershell.exe<br />Flex your muscles now…<br />N.B. PowerShell 2.0 still refers to v1.0 path for backward compatibility<br />
  10. 10. In real life <br />Gotchas:<br />Firewall is not blocking ports<br />Firewall is not dropping packets<br />Correct trust relationships are set up between AD domains<br />Grab the swiss ball, we’re doing Pilates now…<br />
  11. 11. Check SQL Snapins<br />PS H:> Get-PSSnapin -registered<br />Name : SqlServerCmdletSnapin100<br />PSVersion : 2.0<br />Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.<br />Name : SqlServerProviderSnapin100<br />PSVersion : 2.0<br />Description : SQL Server Provider<br />
  12. 12. Load SQL Snapins<br />Add-PSSnapin SqlServerProviderSnapin100<br />Add-PSSnapin SqlServerCmdletSnapin100<br />
  13. 13. Load SMO assemblies<br />The hard way :<br />[Reflection.Assembly]::Load( ` "Microsoft.SqlServer.Smo, `<br />Version=, Culture=neutral, `<br />PublicKeyToken=89845dcd8080cc91")<br /># connect to SQL Server named instance<br /># server name is L50011051<br /># instance name is Instance1<br />$serverName = "L50011051Instance1"<br />$server = New-Object –typeName ` Microsoft.SqlServer.Management.Smo.Server `<br />-argumentList "$serverName"<br />Is there an easier way?<br />
  14. 14. Load SMO assemblies<br />The easier way :<br />[System.Reflection.Assembly]::LoadWithPartialName( ` 'Microsoft.SqlServer.SMO') | out-null<br /># This time connect to default instance<br /># server name is L50011051<br /># and pass in a literal<br />$server = New-Object( ` 'Microsoft.SqlServer.Management.Smo.Server') `<br />"L50011051" <br />Let PowerShell do the heavy lifting with .net assemblies & reflection API<br />
  15. 15. Load AMO assemblies<br />Slightly different syntax:<br />[Reflection.Assembly]::LoadWithPartialName( ` "Microsoft.AnalysisServices")<br />$as=New-Object Microsoft.AnalysisServices.Server<br />$as.connect("L50011051")<br />$as | Format-List<br />Teaser for a separate demo on PowerShell to administer Analysis Services (volunteers please…)<br />
  16. 16. Other SMO assemblies<br />#Need SmoExtended for smo.backup<br />[System.Reflection.Assembly]::LoadWithPartialName( ` "Microsoft.SqlServer.SmoExtended") | Out-Null<br />[System.Reflection.Assembly]::LoadWithPartialName( `<br /> "Microsoft.SqlServer.ConnectionInfo") | Out-Null<br /># 2005 specific, not available in 2008. <br /># Use common instead<br />[System.Reflection.Assembly]::LoadWithPartialName( ` "Microsoft.SqlServer.SmoEnum") | Out-Null<br />http://sqlblog.com/blogs/allen_white/archive/2008/12/07/loading-smo-assemblies-into-powershell.aspx<br />
  17. 17. SQL Authentication<br /># $True for integrated security <br /># $False for SQL authentication, meaning <br /># supply username & password at the prompt<br />$server.ConnectionContext.LoginSecure=$false; <br />$credential = Get-Credential<br />$userName = $credential.UserName –replace("quot;, "")<br />$server.ConnectionContext.set_Login($userName)<br />$server.ConnectionContext.set_SecurePassword( ` $credential.Password)<br />Write-Host $svr.ConnectionContext.ConnectionString<br /><ul><li>Horror story here!!!</li></li></ul><li>Customise sp_who<br />$server.EnumProcesses() | `<br />?{$_.IsSystem -eq $False -and $_.Login -eq 'sa'} | `<br /> Select Spid, Login, Status, Cpu, MemUsage, | ` BlockingSpid, Database, Host, Command, Program | `<br />Format-Table -auto -wrap<br />User spid’s only<br />Pick which columns to display<br />Re-order them, narrower columns first<br />
  18. 18. Some links<br />http://www.databasejournal.com/article.php/3300441/Muthusamy-Anantha-Kumar-aka-The-MAK.htm<br />http://blogs.technet.com/b/heyscriptingguy/archive/2009/05/26/why-should-i-use-windows-powershell-with-sql-server-2008.aspx<br />http://sqlblog.com/blogs/allen_white/archive/2008/01/25/using-powershell-and-sql-server-together.aspx<br />