In this chapter, we will cover:
Working with the sample code
Exploring the SQL Server PowerShell hierarchy
Installing SMO
Loading SMO assemblies
Discovering SQL-related cmdlets and modules
Creating a SQL Server instance object
Exploring SMO Server objects
PowerShell is an administrative tool that has both shell and scripting capabilities that can leverage Windows Management Instrumentation (WMI), COM components, and .NET libraries. PowerShell is becoming more prominent with each generation of Microsoft products. Support for it is being bundled, and improved, in a number of new and upcoming Microsoft product releases. Windows Server, Exchange, ActiveDirectory, SharePoint, and even SQL Server, have all shipped with added PowerShell support and cmdlets. Even vendors such as VMWare, Citrix, Cisco, and Quest, to name a few, have provided ways to allow their products to be accessible via PowerShell.
What makes PowerShell tick? Every systems administrator probably knows the pain of trying to integrate heterogeneous systems using some kind of scripting. Historically, the solution involved some kind of VBScript, some good old batch files, maybe some C# code, some Perl—you name it. Sysadmins either had to resort to duct taping different languages together to get things to work the way they intended, or just did not bother because of the complicated code.
This is where PowerShell comes in. One of the strongest points for PowerShell is that it simplifies automation and integration between different Microsoft ecosystems. As most products have support for PowerShell, getting one system to talk to another is just a matter of discovering what cmdlets, functions, or modules need to be pulled into the script. Even if the product does not have support yet for PowerShell, it most likely has .NET or COM support, which PowerShell can easily use.
Some of the notable features in the latest PowerShell version are:
Workflows: PowerShell V3 introduces Windows PowerShell Workflow (PSWF), which as stated in MSDN (http://msdn.microsoft.com/en-us/library/jj134242.aspx):
helps automate the distribution, orchestration, and completion of multi-computer tasks, freeing users and administrators to focus on higher-level tasks.
PSWF leverages Windows Workflow Foundation 4.0 for the declarative framework, but using familiar PowerShell syntax and constructs.
Robust sessions: PowerShell V3 supports more robust sessions. Sessions can now be retained amid network interruptions. These sessions will remain open until they time out.
Scheduled jobs: There is an improved support for scheduled tasks. There are new cmdlets in the
PSScheduledJob
module that allow you to create, enable, and manage scheduled tasks.Module AutoLoading: If you use a cmdlet that belongs to a module that hasn't been loaded yet, this will trigger PowerShell to search
PSModulePath
and load the first module that contains that cmdlet. This is something we can easily test:Web service support: PowerShell V3 introduces the
Invoke-WebRequest
cmdlet, which sends HTTP or HTTPS requests to a web service and returns the object-based content that can easily be manipulated in PowerShell. You can think about downloading entire websites using PowerShell (and check out Lee Holmes' article on it: http://www.leeholmes.com/blog/2012/03/31/how-to-download-an-entire-wordpress-blog/).Simplified language syntax: Writing your
Where-Object
andForeach-Object
has just become cleaner. Improvements in the language include supporting default parameter values, and simplified syntax.What you used to write in V1 and V2 with curly braces and
$_
as follows:can now be rewritten in V3 as:
Improved Integrated Scripting Environment (ISE): The new ISE comes with Intellisense, searchable commands in the sidebar, parameter forms, and live syntax checking.
Before we dive into the recipes, let's go over a few important concepts and terminologies that will help you understand how SQL Server and PowerShell can work together:
PSProvider and PSDrive: PowerShell allows different data stores to be accessed as if they are regular files and folders.
PSProvider
is similar to an adapter, which allows these data stores to be seen as drives.To get a list of the supported
PSProvider
objects, type:You should see something similar to the following screenshot:
Depending on which instance of
PSProvider
is already available in your system, yours may be slightly different:PSDrive: Think of your
C:\
, but for data stores other than the file system. To get a list ofPSDrive
objects in your system, type:You should see something similar to the following screenshot:
Note that there is a
PSDrive
for SQLServer, which can be used to navigate, access, and manipulate SQL Server objects.Execution policy: By default, PowerShell will abide by the current execution policy to determine what kind of scripts can be run. For our recipes, we are going to assume that you will run PowerShell as the administrator on your test environment. You will also need to set the execution policy to
RemoteSigned
:This setting will allow PowerShell to run digitally-signed scripts, or local unsigned scripts.
Modules and snap-ins: Modules and snap-ins are ways to extend PowerShell. Both modules and snap-ins can add cmdlets and providers to your current session. Modules can additionally load functions, variables, aliases, and other tools to your session.
Snap-ins are Dynamically Linked Libraries (DLL), and need to be registered before they can be used. Snap-ins are available in V1, V2, and V3. For example:
Modules, on the other hand, are more like your regular PowerShell
.ps1
script files. Modules are available in V2 and V3. You do not need to register a module to use it, you just need to import:Note
For more information on PowerShell basics, check out Appendix B, PowerShell Primer.