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 ?
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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With