SQL Express Batch Script: Add Account to SQL Server SysAdmin Role
Try this script and see if it adds the user account that you execute it as the sysadmin access to the SQL Server instance on your Windows machine. This will need to be saved to a .cmd or .bat file by renaming a text document and then it needs executed as a batch script.
I got this from GitHub "Script to add the current user to the sysadmin role in SQL Server" a few years back and used it for an SQL Express install to grant the sysadmin access to a local admin account on a server. I saved it after confirming it worked it worked—so I'm sharing it with others here.
@echo off
::
:: ****************************************************************************
::
:: Copyright (c) Microsoft Corporation. All rights reserved.
:: This code is licensed under the Microsoft Public License.
:: THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
:: ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
:: IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
:: PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
::
:: ****************************************************************************
::
:: CMD script to add a user to the SQL Server sysadmin role
::
:: Input: %1 specifies the instance name to be modified. Defaults to SQLEXPRESS.
:: %2 specifies the principal identity to be added (in the form "<domain>\<user>").
:: If omitted, the script will request elevation and add the current user (pre-elevation) to the sysadmin role.
:: If provided explicitly, the script is assumed to be running elevated already.
::
:: Method: 1) restart the SQL service with the '-m' option, which allows a single connection from a box admin
:: (the box admin is temporarily added to the sysadmin role with this start option)
:: 2) connect to the SQL instance and add the user to the sysadmin role
:: 3) restart the SQL service for normal connections
::
:: Output: Messages indicating success/failure.
:: Note that if elevation is done by this script, a new command process window is created: the output of this
:: window is not directly accessible to the caller.
::
::
setlocal
set sqlresult=N/A
if .%1 == . (set /P sqlinstance=Enter SQL instance name, or default to SQLEXPRESS: ) else (set sqlinstance=%1)
if .%sqlinstance% == . (set sqlinstance=SQLEXPRESS)
if /I %sqlinstance% == MSSQLSERVER (set sqlservice=MSSQLSERVER) else (set sqlservice=MSSQL$%sqlinstance%)
if .%2 == . (set sqllogin="%USERDOMAIN%\%USERNAME%") else (set sqllogin=%2)
:: remove enclosing quotes
for %%i in (%sqllogin%) do set sqllogin=%%~i
@echo Adding '%sqllogin%' to the 'sysadmin' role on SQL Server instance '%sqlinstance%'.
@echo Verify the '%sqlservice%' service exists ...
set srvstate=0
for /F "usebackq tokens=1,3" %%i in (`sc query %sqlservice%`) do if .%%i == .STATE set srvstate=%%j
if .%srvstate% == .0 goto existerror
:: elevate if <domain/user> was defaulted
if NOT .%2 == . goto continue
echo new ActiveXObject("Shell.Application").ShellExecute("cmd.exe", "/D /Q /C pushd \""+WScript.Arguments(0)+"\" & \""+WScript.Arguments(1)+"\" %sqlinstance% \""+WScript.Arguments(2)+"\"", "", "runas"); >"%TEMP%\addsysadmin{7FC2CAE2-2E9E-47a0-ADE5-C43582022EA8}.js"
call "%TEMP%\addsysadmin{7FC2CAE2-2E9E-47a0-ADE5-C43582022EA8}.js" "%cd%" %0 "%sqllogin%"
del "%TEMP%\addsysadmin{7FC2CAE2-2E9E-47a0-ADE5-C43582022EA8}.js"
goto :EOF
:continue
:: determine if the SQL service is running
set srvstarted=0
set srvstate=0
for /F "usebackq tokens=1,3" %%i in (`sc query %sqlservice%`) do if .%%i == .STATE set srvstate=%%j
if .%srvstate% == .0 goto queryerror
:: if required, stop the SQL service
if .%srvstate% == .1 goto startm
set srvstarted=1
@echo Stop the '%sqlservice%' service ...
net stop %sqlservice%
if errorlevel 1 goto stoperror
:startm
:: start the SQL service with the '-m' option (single admin connection) and wait until its STATE is '4' (STARTED)
:: also use trace flags as follows:
:: 3659 - log all errors to errorlog
:: 4010 - enable shared memory only (lpc:)
:: 4022 - do not start autoprocs
@echo Start the '%sqlservice%' service in maintenance mode ...
sc start %sqlservice% -m -T3659 -T4010 -T4022 >nul
if errorlevel 1 goto startmerror
:checkstate1
set srvstate=0
for /F "usebackq tokens=1,3" %%i in (`sc query %sqlservice%`) do if .%%i == .STATE set srvstate=%%j
if .%srvstate% == .0 goto queryerror
if .%srvstate% == .1 goto startmerror
if NOT .%srvstate% == .4 goto checkstate1
:: add the specified user to the sysadmin role
:: access tempdb to avoid a misleading shutdown error
@echo Add '%sqllogin%' to the 'sysadmin' role ...
for /F "usebackq tokens=1,3" %%i in (`sqlcmd -S np:\\.\pipe\SQLLocal\%sqlinstance% -E -Q "create table #foo (bar int); declare @rc int; execute @rc = sp_addsrvrolemember '$(sqllogin)', 'sysadmin'; print 'RETURN_CODE : '+CAST(@rc as char)"`) do if .%%i == .RETURN_CODE set sqlresult=%%j
:: stop the SQL service
@echo Stop the '%sqlservice%' service ...
net stop %sqlservice%
if errorlevel 1 goto stoperror
if .%srvstarted% == .0 goto exit
:: start the SQL service for normal connections
net start %sqlservice%
if errorlevel 1 goto starterror
goto exit
:: handle unexpected errors
:existerror
sc query %sqlservice%
@echo '%sqlservice%' service is invalid
goto exit
:queryerror
@echo 'sc query %sqlservice%' failed
goto exit
:stoperror
@echo 'net stop %sqlservice%' failed
goto exit
:startmerror
@echo 'sc start %sqlservice% -m' failed
goto exit
:starterror
@echo 'net start %sqlservice%' failed
goto exit
:exit
if .%sqlresult% == .0 (@echo '%sqllogin%' was successfully added to the 'sysadmin' role.) else (@echo '%sqllogin%' was NOT added to the 'sysadmin' role: SQL return code is %sqlresult%.)
endlocal
pause