Is there anyway to query the sql service account?

  • Hi all,

    Is there some way to query what they SQL service account is from either query analyzer/management studio or the command line?

    Im trying to come up with an automated way of gathering all of the service accounts on my servers so that I dont have to log in to each one (over 100) to inventory them.

    I was thinking something like @@SERVERNAME only for the service account.

    Any ideas?

  • Here's some code that I'm using.

    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

    Ola Hallengren

    http://ola.hallengren.com

  • Works perfect. Thanks Ola!

  • Excellent advice.

  • Useful utility, thanks:-)

    Cheers,

    JohnA

    MCM: SQL2008

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply