Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SQL Server 2014 with Powershell v5 Cookbook

You're reading from   SQL Server 2014 with Powershell v5 Cookbook Over 150 real-world recipes to simplify database management, automate repetitive tasks, and enhance your productivity

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher Packt
ISBN-13 9781785283321
Length 760 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Donabel Santos Donabel Santos
Author Profile Icon Donabel Santos
Donabel Santos
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Getting Started with SQL Server and PowerShell 2. SQL Server and PowerShell Basic Tasks FREE CHAPTER 3. Basic Administration 4. Security 5. Backup and Restore 6. Advanced Administration 7. Audit and Policies 8. High Availability with AlwaysOn 9. SQL Server Development 10. Business Intelligence 11. Helpful PowerShell Snippets A. PowerShell Primer B. Creating a SQL Server VM Index

Exploring SMO Server Objects

SMO comes with a hierarchy of objects that are accessible programmatically. For example, when we create an SMO server variable, we can then access databases, logins, and database level triggers. Once we get a handle of individual databases, we can then traverse the tables, stored procedures and views that it contains. Since many tasks involve SMO objects, you will be at an advantage if you know how to discover and navigate these objects.

Getting ready

Open up your PowerShell console, PowerShell ISE, or your favorite PowerShell editor.

You will also need to note what your instance name is. If you have a default instance, you can use your machine name. If you have a named instance, the format will be <machine name>\<instance name>.

How to do it...

In this recipe, we will start exploring the hierarchy of objects with SMO:

  1. Import the SQLPS module as follows:
    Import-Module SQLPS -DisableNameChecking
  2. Create a server instance as follows:
    $instanceName = "localhost"
    
    #code below all in one line
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
  3. Get the SMO objects directly accessible from the $server object:
    $server |
    Get-Member -MemberType "Property" |
    Where-Object Definition -Like "*Smo*"

    Note

    If you are using PowerShell v2, you will have to change the Where-Object cmdlet usage to use the curly braces {} and the $_ variable:

    Where-Object {$_.Definition -like "Smo*" }
  4. Now, let's check SMO objects under databases:
    $server.Databases |
    Get-Member -MemberType "Property" |
    Where-Object Definition -Like "*Smo*"
  5. To check out the tables, you can type and execute the following:
    $server.Databases["AdventureWorks2014"].Tables |
    Get-Member -MemberType "Property" |
    Where-Object Definition -Like "*Smo*"

How it works...

SMO contains a hierarchy of objects. At the very top there is a server object, which in turn contains objects such as Databases, Configuration, SqlMail, LoginCollection, and so on. These objects in turn contain other objects, for example, Databases is a collection that contains Database objects, and a Database contains Tables.

Note

You can check out the SMO Object Model Diagram from the MSDN at https://msdn.microsoft.com/en-ca/library/ms162209.aspx.

One way to navigate through the hierarchy is by creating a server instance first. From here, you can use Get-Member to figure out which properties belong to that object. Once you find out, you can start creating additional variables for the member objects and then use Get-Member on them. Lather, rinse, and repeat.

See also

  • The recipe Loading SMO assemblies.
  • The recipe Creating a SQL Server Instance Object.
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image