问题
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 ?
回答1:
You can use the sc.exe.
To find SQL instances services:
sc \\<remote computer name> query | find /i "sql"
To get configuration:
sc \\<remote computer name> qc <service name from listing above>
回答2:
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;
回答3:
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
回答4:
For a default instance:
DECLARE @sn NVARCHAR(128);
EXEC master.dbo.xp_regread
'HKEY_LOCAL_MACHINE',
'SYSTEM\CurrentControlSet\services\SQLSERVERAGENT',
'ObjectName',
@sn OUTPUT;
SELECT @sn;
For a named instance, you'll need the second argument to be:
'SYSTEM\CurrentControlSet\services\SQLAGENT$InstanceName',
Now, it may not work because you may not have access to xp_regread, and the location of this property may change from version to version (I only tested 2008, 2008 R2 and Denali - I don't have a 2005 instance handy to check).
In any case you are probably better off asking the DBA (as suggested in a comment) or, if you have access to the physical machine, just checking the service account in the Control Panel.
回答5:
I know this is an old thread but here is my solution. This has been tested against SQL Server 2000, 2005, 2008, 2008 R2, 2012, 2014 and 2016.
if (select CONVERT(INT, (REPLACE(SUBSTRING(convert(nvarchar, SERVERPROPERTY('ProductVersion')), 1, 2), '.', '')))) >10
BEGIN
select distinct(service_account) AS SvcAccount from sys.dm_server_services;
END
ELSE
BEGIN
DECLARE @instanceName varchar(100)
set @instanceName = convert(varchar,SERVERPROPERTY ('InstanceName'))
IF (@instanceName) IS NULL
begin
DECLARE @sn NVARCHAR(128);
EXEC master.dbo.xp_regread
'HKEY_LOCAL_MACHINE',
'SYSTEM\CurrentControlSet\services\SQLSERVERAGENT',
'ObjectName',
@sn OUTPUT ;
SELECT @sn AS SvcAccount;
END
ELSE
BEGIN
DECLARE @SQL varchar (500)
SET @SQL = 'DECLARE @sn NVARCHAR(128); exec master.dbo.xp_regread ''HKEY_LOCAL_MACHINE'', ''SYSTEM\CurrentControlSet\services\SQLAgent$'+@instanceName+''',''ObjectName'', @sn OUTPUT; SELECT @sn AS SvcAccount;'
EXEC (@SQL)
END
END
来源:https://stackoverflow.com/questions/7324407/get-service-account-details-of-the-sql-agent-service