Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Service Account Details of the SQL Agent Service

How can I get the Service Account name for the SQL Agent service for a particular SQL Server (SQL 2005). Is it possible to get using SQL statements or WMI ?

like image 625
John Avatar asked Sep 06 '11 18:09

John


2 Answers

As Aaron Bertrand pointed out, you can use the undocumented xp_regread in SQL Server 2005 and SQL Server 2008, but there is a better way, starting with SQL Server 2008R2 SP1.

From the article How to identify the SQL Server Service Account in T-SQL, you can use sys.dm_server_services like this:

SELECT  DSS.servicename,
        DSS.startup_type_desc,
        DSS.status_desc,
        DSS.last_startup_time,
        DSS.service_account,
        DSS.is_clustered,
        DSS.cluster_nodename,
        DSS.filename,
        DSS.startup_type,
        DSS.status,
        DSS.process_id
FROM    sys.dm_server_services AS DSS;

SQL Service Account Query Results

like image 118
KyleMit Avatar answered Oct 06 '22 01:10

KyleMit


Since SQL Server runs as a windows service you can use wmic to query the start name.

wmic service where "name Like 'MSSQL%'" get Name , StartName

For me this outputs the following (since I've got multiple instances thoes are included as well)

Name                    StartName
MSSQL$SQLEXPRESS        NT AUTHORITY\NetworkService
MSSQL$SQLEXPRESS2005    NT AUTHORITY\NetworkService
MSSQLFDLauncher         NT AUTHORITY\NETWORK SERVICE
MSSQLSERVER             NT AUTHORITY\NETWORK SERVICE
MSSQLServerADHelper     NT AUTHORITY\NetworkService
MSSQLServerADHelper100  NT AUTHORITY\NETWORK SERVICE
MSSQLServerOLAPService  NT AUTHORITY\NETWORK SERVICE

You can add /NODE to query remote computers. As with any WMI query you will need sufficient privileges in order for this to work

Or the same query using Powershell's Get-WmiObject (Supports remote/multiple computersnames):

Get-WmiObject Win32_Service -ComputerName localhost,W-Remote -Filter "name Like 'MSSQL%'" | ft __Server,State,Name,DisplayName,StartName -AutoSize

Sample Output:

__SERVER State   Name                   DisplayName                         StartName
-------- -----   ----                   -----------                         ---------
W0123456 Stopped MSSQL$SQLEXPRESS       SQL Server (SQLEXPRESS)             NT AUTHORITY\NETWORK SERVICE
W0123456 Running MSSQLSERVER            SQL Server (MSSQLSERVER)            LocalSystem
W0123456 Stopped MSSQLServerADHelper100 SQL Active Directory Helper Service NT AUTHORITY\NETWORKSERVICE
W-REMOTE Stopped MSSQL$SQLEXPRESS       SQL Server (SQLEXPRESS)             NT AUTHORITY\NETWORK SERVICE
W-REMOTE Running MSSQLSERVER            SQL Server (MSSQLSERVER)            LocalSystem
like image 35
Conrad Frix Avatar answered Oct 06 '22 00:10

Conrad Frix