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
9. Invoke PowerShell To get any real work done … runas /noprofile /user:anotherDomainamdarSi_adm %SystemRoot%ystem32indowsPowerShellv1.0owershell.exe runas /noprofile /user:yetanotherDomainJamdar%SystemRoot%ystem32indowsPowerShell v1.0owershell.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:gt; 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
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 = "L50011051nstance1" $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