Your SlideShare is downloading. ×
0
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Demo for Why Use PowerShell
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Demo for Why Use PowerShell

1,445

Published on

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

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,445
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Why Use PowerShell(demo)<br />Siraj Jamdar<br />
  • 2. Invoke PowerShell<br />From the command line:<br />Windows+R-&gt; sqlps<br />Limited shell by design (to protect the innocent from themselves)<br />
  • 3. Invoke PowerShell from SQL<br />Context sensitive in SSMS<br />Right-Click -&gt; pick Start PowerShelloption<br />My favourite, pretty snappy and useful<br />Automatically traverses to the correct level<br />
  • 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. Invoke PowerShell<br />The hard way<br />Start <br /><ul><li> All Programs
  • 6. Accessories
  • 7. Windows PowerShell
  • 8. Windows PowerShell</li></ul>PowerShellcorrupts ;-)<br />
  • 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. 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. Check SQL Snapins<br />PS H:&gt; 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. Load SQL Snapins<br />Add-PSSnapin SqlServerProviderSnapin100<br />Add-PSSnapin SqlServerCmdletSnapin100<br />
  • 13. Load SMO assemblies<br />The hard way :<br />[Reflection.Assembly]::Load( ` &quot;Microsoft.SqlServer.Smo, `<br />Version=10.0.0.0, Culture=neutral, `<br />PublicKeyToken=89845dcd8080cc91&quot;)<br /># connect to SQL Server named instance<br /># server name is L50011051<br /># instance name is Instance1<br />$serverName = &quot;L50011051Instance1&quot;<br />$server = New-Object –typeName ` Microsoft.SqlServer.Management.Smo.Server `<br />-argumentList &quot;$serverName&quot;<br />Is there an easier way?<br />
  • 14. Load SMO assemblies<br />The easier way :<br />[System.Reflection.Assembly]::LoadWithPartialName( ` &apos;Microsoft.SqlServer.SMO&apos;) | out-null<br /># This time connect to default instance<br /># server name is L50011051<br /># and pass in a literal<br />$server = New-Object( ` &apos;Microsoft.SqlServer.Management.Smo.Server&apos;) `<br />&quot;L50011051&quot; <br />Let PowerShell do the heavy lifting with .net assemblies &amp; reflection API<br />
  • 15. Load AMO assemblies<br />Slightly different syntax:<br />[Reflection.Assembly]::LoadWithPartialName( ` &quot;Microsoft.AnalysisServices&quot;)<br />$as=New-Object Microsoft.AnalysisServices.Server<br />$as.connect(&quot;L50011051&quot;)<br />$as | Format-List<br />Teaser for a separate demo on PowerShell to administer Analysis Services (volunteers please…)<br />
  • 16. Other SMO assemblies<br />#Need SmoExtended for smo.backup<br />[System.Reflection.Assembly]::LoadWithPartialName( ` &quot;Microsoft.SqlServer.SmoExtended&quot;) | Out-Null<br />[System.Reflection.Assembly]::LoadWithPartialName( `<br /> &quot;Microsoft.SqlServer.ConnectionInfo&quot;) | Out-Null<br /># 2005 specific, not available in 2008. <br /># Use common instead<br />[System.Reflection.Assembly]::LoadWithPartialName( ` &quot;Microsoft.SqlServer.SmoEnum&quot;) | Out-Null<br />http://sqlblog.com/blogs/allen_white/archive/2008/12/07/loading-smo-assemblies-into-powershell.aspx<br />
  • 17. SQL Authentication<br /># $True for integrated security <br /># $False for SQL authentication, meaning <br /># supply username &amp; password at the prompt<br />$server.ConnectionContext.LoginSecure=$false; <br />$credential = Get-Credential<br />$userName = $credential.UserName –replace(&quot;quot;, &quot;&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 &apos;sa&apos;} | `<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. 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 />

×