|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, June 14, 2013 6:35 AM
Points: 306,
Visits: 1,020
|
|
John,
Good idea! Do you think we can all see this code of your? Originally, I did not include service accounts before we have a separate network for our production and development server so I did not think about adding that feature.
Glad to see everyone's ideas on how to enhance this code.
Thanks,
Rudy
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Saturday, June 15, 2013 2:09 AM
Points: 155,
Visits: 1,301
|
|
Rudy,
The code below returns the names of SQLService and SQLAgent accounts. Works on 2000, 2005 and 2008 as far as I can tell:
DECLARE @NamedInstance bit IF CAST(SERVERPROPERTY('ServerName') AS varchar) LIKE '%\%' SET @NamedInstance = 1 ELSE SET @NamedInstance = 0
DECLARE @ServiceName varchar(50)
IF @NamedInstance = 0 BEGIN SET @ServiceName = 'MSSQLSERVER' END ELSE BEGIN SET @ServiceName = 'MSSQL$' + RIGHT(CAST(SERVERPROPERTY('ServerName') AS varchar),LEN(CAST(SERVERPROPERTY('ServerName') AS varchar)) - CHARINDEX('\',CAST(SERVERPROPERTY('ServerName') AS varchar),1)) END
DECLARE @KEY_VALUE varchar(100) DECLARE @ServiceAccountName varchar(100)
SET @KEY_VALUE = 'SYSTEM\CurrentControlSet\Services\' + @ServiceName EXECUTE master..xp_regread 'HKEY_LOCAL_MACHINE', @KEY_VALUE, 'ObjectName', @ServiceAccountName OUTPUT SELECT @ServiceAccountName as 'SQLService Account' IF CAST(SERVERPROPERTY('ServerName') AS varchar) LIKE '%\%' SET @NamedInstance = 1 ELSE SET @NamedInstance = 0
IF @NamedInstance = 0 BEGIN SET @ServiceName = 'SQLSERVERAGENT' END ELSE BEGIN SET @ServiceName = 'SQLAgent$' + RIGHT(CAST(SERVERPROPERTY('ServerName') AS varchar),LEN(CAST(SERVERPROPERTY('ServerName') AS varchar)) - CHARINDEX('\',CAST(SERVERPROPERTY('ServerName') AS varchar),1)) END
SET @KEY_VALUE = 'SYSTEM\CurrentControlSet\Services\' + @ServiceName
EXECUTE master..xp_regread 'HKEY_LOCAL_MACHINE', @KEY_VALUE, 'ObjectName', @ServiceAccountName OUTPUT SELECT @ServiceAccountName as 'SQLAgent Account'
Cheers,
JohnA
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Today @ 11:17 AM
Points: 78,
Visits: 554
|
|
Hi, I actually worked on this issue for almost half a day and I couldn't resolve a snack with John's code a few days ago, and I just gave up. 
He uses the following
DECLARE @KEY_VALUE varchar(100) DECLARE @ServiceAccountName varchar(100)
EXECUTE master..xp_regread 'HKEY_LOCAL_MACHINE', @KEY_VALUE, 'ObjectName', @ServiceAccountName OUTPUT SELECT @ServiceAccountName as 'SQLAgent Account'
however, I've run that code on several servers and in some of them the result is 'NULL'. (it doesn't matter the service, it happens with several services).
The only thing I could figure out is that when I run the following code:
declare @regkey varchar (100) SET @REGKEY = 'System\CurrentControlSet\Services\SQLAgent$instance_name'
EXEC master..xp_regenumvalues @rootkey='HKEY_LOCAL_MACHINE',@key=@regkey
for the Object value I get the following:
Value data value data ObjectName .\sqlservice NULL NULL
It seems that for some cases the registry key has two sets of values, weird.
Anyone has ideas of why this might be?
Thank you
Miguel
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, June 14, 2013 11:51 AM
Points: 27,
Visits: 421
|
|
Excellent script, Rudy. Thanks a mint for sharing it.
|
|
|
|