Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Listing SQL Server instances


In this recipe, we will list all SQL Server instances in the local network.

Getting ready

Log in to the server that has your SQL Server development instance as an administrator.

How to do it...

Let's look at the steps to list your SQL Server instances:

  1. Open PowerShell ISE as administrator.

  2. Let's use the Start-Service cmdlet to start the SQL Browser service:

    Import-Module SQLPS -DisableNameChecking
    
    #out of the box, the SQLBrowser is disabled. To enable:
    Set-Service SQLBrowser -StartupType Automatic
    
    #sql browser must be installed and running for us
    #to discover SQL Server instances
    Start-Service "SQLBrowser"
  3. Next, you need to create a ManagedComputer object to get access to instances. Type the following script and run:

    $instanceName = "localhost"
    $managedComputer = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $instanceName
    
    #list server instances
    $managedComputer.ServerInstances

    Your result should look similar to the one shown in the following screenshot...