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 syst开发者_开发知识库em registry or other well-known location? Any ideas?
Try this: "Query to find out Service Account details"
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;
精彩评论