SQL Server Powershell is here!

I am very excited that SQL Server will ship with a pretty solid first release of Powershell extensions with SQL Server 2008. The SQL Server Powershell extensions deliver on a vision that we’ll expand on in the next releases to come. So what’s in this release of SQL Server Powershell?

- SQLPS – a minishell that gives you a complete pre-configured Powershell with all of SQL Server’s extensions preloaded.

- SQL Server Agent integration – A new job subsystem for Powershell

- SQL Server Management Studio Integration – context menus on every applicable node in Object Explorer (with connection context reuse. Including SQL security!)

- Four new Providers! – new providers for SQL Server relational engine, Registered Servers, Data Collection, and SQL Server Policy Management

- SQLCMD integration – SQLCMD compatible script execution within Powershell (reuses the SQL Server connection context, and even database context of the provider!)

- SQL Server Policy Management integration – Allows evaluation of any Policy

- Various other cmdlets – support the provider, such as conversion of a SMO Urn to a Powershell path, encoding and decoding of SQL identifiers.

- SQL Server Powershell redist – allows you to install SQL Server Powershell with your application or on any machine you need to have it on (this still being built so with the caveat it may be shipped later, or being cut altogether – don’t flame me yet).

As always with every release, a lot of things were left on the cutting floor, such as Powershell editor integration & execution within SSMS (with grid output), Powershell script generation from any SSMS menu, SMO objects generating Powershell script, more cmdlets that address common user scenarios, Analysis Server support. And this doesn’t include a long list of ideas that for the sake of brevity will not discuss here.

The excitement about this release is palpable, and there is no shortness of ideas for the next releases. I think you’ll be pleased with this first release.

---

From recent posts and email I know there is some explanation needed why we ship ‘pre-packaged’ SQLPS functionality. Let me address that here as well.

SQLPS is slated to be replacing SQLCMD and other tools that people now use to do ad-hoc management and management task automation. It’s more than that, we want uniformity of management across ALL of SQL Server services, whether it is Analysis Server, SQL Server, Integration Services, Reporting Services etc.

SQLPS.exe is a Minishell (also called “custom shell”). It is a form of pre-packaging of Powershell functionality, and it is available to anyone who wants to do this (make-shell). It is regular Powershell, albeit with limitations that the Powershell team decided to impose on it – it is a ‘closed’ shell, which doesn’t allow adding other snapins.

We are shipping SQLPS to make life of our DBA’s a whole lot easier. If they need to have quick access to the SQL providers, assemblies, cmdlets, default security settings, everything is there. We could have possibly done this through a startup script but not everything can be accomplished this way. We are changing the default security settings, without affecting the settings for overall Powershell. Minishells have their own settings.

We also use SQLPS to execute SQL Agent jobs. We pipe the data into SQLPS, as we cannot pass it on the cmdline. From that perspective it serves our purpose very well. This is somewhat harder (if not impossible) to accomplish using the startup script approach.

That said, in the next release we will look into it to ship a SQPS that is more flexible. Listening to the feedback, we should have a form of packaging that allows users to add cmdlets/providers.

We are however not stopping anyone to create their own startup scripts that include the cmdlets and/or providers for SQL Server, Exchange, IIS etc. etc. To get you started on this, here is a script that exactly does that:

#
# Initialize-SqlpsEnvironment.ps1
#
# Loads the SQL Server provider extensions
#
# Usage: Powershell -NoExit -Command "& '.\Initialize-SqlPsEnvironment.ps1'"
#
# Change log:
# June 14, 2008: Michiel Wories
# Initial Version
# June 17, 2008: Michiel Wories
# Fixed issue with path that did not allow for snapin\provider:: prefix of path
# Fixed issue with provider variables. Provider does not handle case yet
# that these variables do not exist (bug has been filed)

$ErrorActionPreference = "Stop"

$sqlpsreg="HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps"

if (Get-ChildItem $sqlpsreg -ErrorAction "SilentlyContinue")
{
throw "SQL Server Powershell is not installed."
}
else
{
$item = Get-ItemProperty $sqlpsreg
$sqlpsPath = [System.IO.Path]::GetDirectoryName($item.Path)
}

#
# Preload the assemblies. Note that most assemblies will be loaded when the provider
# is used. if you work only within the provider this may not be needed. It will reduce
# the shell's footprint if you leave these out.
#
$assemblylist =
"Microsoft.SqlServer.Smo",
"Microsoft.SqlServer.Dmf ",
"Microsoft.SqlServer.SqlWmiManagement ",
"Microsoft.SqlServer.ConnectionInfo ",
"Microsoft.SqlServer.SmoExtended ",
"Microsoft.SqlServer.Management.RegisteredServers ",
"Microsoft.SqlServer.Management.Sdk.Sfc ",
"Microsoft.SqlServer.SqlEnum ",
"Microsoft.SqlServer.RegSvrEnum ",
"Microsoft.SqlServer.WmiEnum ",
"Microsoft.SqlServer.ServiceBrokerEnum ",
"Microsoft.SqlServer.ConnectionInfoExtended ",
"Microsoft.SqlServer.Management.Collector ",
"Microsoft.SqlServer.Management.CollectorEnum"

foreach ($asm in $assemblylist)
{
$asm = [Reflection.Assembly]::LoadWithPartialName($asm)
}

#
# Set variables that the provider expects (mandatory for the SQL provider)
#
Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0
Set-Variable -scope Global -name SqlServerConnectionTimeout -Value 30
Set-Variable -scope Global -name SqlServerIncludeSystemObjects -Value $false
Set-Variable -scope Global -name SqlServerMaximumTabCompletion -Value 1000

#
# Load the snapins, type data, format data
#
Push-Location
cd $sqlpsPath
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Update-TypeData -PrependPath SQLProvider.Types.ps1xml
update-FormatData -prependpath SQLProvider.Format.ps1xml
Pop-Location

Write-Host -ForegroundColor Yellow 'SQL Server Powershell extensions are loaded.'
Write-Host
Write-Host -ForegroundColor Yellow 'Type "cd SQLSERVER:\" to step into the provider.'
Write-Host
Write-Host -ForegroundColor Yellow 'For more information, type "help SQLServer".'

Now, go and use SQL Server Powershell :-). Give us your feedback, and I promise you we’ll listen to it, and we’ll keep on improving on it. This is written for you, the DBA, the IT-PRO, the casual user who needs to do some ad hoc administration or anyone else who uses Powershell for their administrative tasks.

Michiel