SQLPS and its limitations

SQL Server Powershell (SQLPS) enables some new possibilities when it comes to managing your instances, but it also comes with some limitations. These limitations are not always obvious and can sometimes be worked around. To understand the limitations you must first understand that SQLPS is not Powershell V1, V2 or V3 but rather a minishell on its own. It was created by Microsoft as a minishell based on Powershell V1 so the cmdlets available are based on V1. To get an idea of what is missing compared to newer versions of Powershell there is a list of feature differences between versions on Wikipedia which could give some pointers.

You can still use all the Powershell features from newer versions with some workarounds. When setting up jobs in SQL Server Agent for example you can select Powershell as a job step type. This actually means that you will use SQLPS and not the regular Powershell.
If you want the job to run regular Powershell commands from V2 or later (like Invoke-Command) you can use CmdExec as job step type instead and from there execute Powershell with the PS script included, like this:

'
'Use with CmdExec
'
powershell.exe -Command "& {Invoke-Command -ComputerName MyServername -ScriptBlock {Stop-Service -DisplayName 'MyServiceDisplayName'}}"

When you run PS scripts in this way you will use the regular Powershell from the operating system instead of SQLPS and it gives you all the features of newer versions of Powershell in SQL Server Agent jobs. There is no difference in SQLPS in SQL Server 2008 and SQL Server 2008 R2. In SQL Server 2012 however, the Powershell integration is much deeper and these kind of workarounds are much less needed.

No comments

Post a Comment