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


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
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 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
    • 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 
    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=, Culture=neutral, `
    # 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') `
    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 | 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
  • 17. SQL Authentication
    # $True for integrated security
    # $False for SQL authentication, meaning
    # supply username & password at the prompt
    $credential = Get-Credential
    $userName = $credential.UserName –replace("quot;, "")
    $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