Your SlideShare is downloading. ×
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,429

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

×