Yan Sklyarenko
Yan Sklyarenko

Reputation: 32240

How to get the account the SQL Server service runs as with C#?

Ideally, I would like to pass a SQL Server instance name (for instance, .\SQLEXPRESS) and get the name (or a SID) of the account that particular instance is running as. Something like this:

var serviceAccount = SomeClass.GetServiceAccount(".\SQLEXPRESS");

Maybe this info is stored somewhere in system registry or other well-known location? Any ideas?

Upvotes: 1

Views: 1076

Answers (2)

Venkataraman R
Venkataraman R

Reputation: 12959

Since SQL Server 2008, we can get the information from below query:

SELECT value_data
FROM   sys.dm_server_registry
WHERE  value_name = 'ObjectName'
AND     registry_key = 'HKLM\SYSTEM\CurrentControlSet\Services\MSSQLSERVER'

We can also use below approach:

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;   

Upvotes: 0

richard
richard

Reputation: 12498

Try this: "Query to find out Service Account details"

Upvotes: 3

Related Questions