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 service accounts


We will list service accounts in this recipe.

How to do it...

These are the steps for listing SQL Server service accounts:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
  3. Add the following script and run:

    #replace localhost with your machine name
    #make sure you open the appropriate firewall ports
    $instanceName = "localhost"
    $managedComputer = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $instanceName
    
    #list services
    $managedComputer.Services |
    Select-Object Name, ServiceAccount, ServiceState |
    Format-Table –AutoSize

You should see a list of services and their service accounts in a format like this:

How it works...

A service account is an account created for the exclusive purpose of running a service. To list service accounts, we can use a Wmi.ManagedComputer object:

$managedComputer = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer...