You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/database-engine/configure-windows/connect-to-sql-server-when-system-administrators-are-locked-out.md
+26-27Lines changed: 26 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -62,63 +62,62 @@ The following table summarizes the different ways to start your instance in sing
62
62
For step-by-step instructions about how to start [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] in single-user mode, see [Start SQL Server in Single-User Mode](../../database-engine/configure-windows/start-sql-server-in-single-user-mode.md).
63
63
64
64
65
-
### Using Command Prompt
65
+
### Using Powershell
66
66
67
-
1. Open a Command Prompt as Administrator
68
-
1.Stop SQL Server service so it can be restarted with single-user mode, using the following command:
67
+
1. Open a Windows Powershell command - Run as an Administrator
68
+
1.Set up service name and SQL Server instance, and Windows login variables. Replace these with values to match your environment
69
69
70
-
```Command Prompt
71
-
net stop MSSQL$instancename
70
+
```powershell
71
+
$service_name = "MSSQL`$instancename" # for a default instace use: "MSSQLSERVER"
72
+
$sql_server_instance = "machine_name\instance"
73
+
$login_to_be_granted_access = "[CONTOSO\PatK]"
72
74
```
73
75
74
-
For a default instance name use: `net stop MSSQLSERVER`
76
+
1. Stop SQL Server service so it can be restarted with single-user mode, using the following command:
75
77
78
+
```powershell
79
+
net stop $service_name
80
+
```
76
81
77
82
1. Now start your SQL Server instance in a single user mode and only allow SQLCMD.exe to connect (/mSQLCMD)
78
83
79
84
> [!NOTE]
80
85
> Be sure to use upper-case SQLCMD
81
86
82
-
```Command Prompt
83
-
net start MSSQL$instance /mSQLCMD
87
+
```powershell
88
+
net start $service_name /mSQLCMD
84
89
```
85
90
86
91
1. Using **SQLCMD** execute a CREATE LOGIN command followed by ALTER SERVER ROLE command. This step assumes you have logged into Windows with an account that is a member of the Local Administrators group. Replace the domain and login name with the credentials you want to give Sysadmin access.
87
92
88
-
```Command Prompt
89
-
sqlcmd.exe -E -S.\instancename -Q "CREATE LOGIN [CONTOSO\PatK] FROM WINDOWS; ALTER SERVER ROLE sysadmin ADD MEMBER [CONTOSO\PatK]; "
93
+
```powershell
94
+
sqlcmd.exe -E -S $sql_server_instance -Q "CREATE LOGIN $login_to_be_granted_access FROM WINDOWS; ALTER SERVER ROLE sysadmin ADD MEMBER $login_to_be_granted_access; "
90
95
```
91
96
92
97
> [!NOTE]
93
-
> If you receive the following error, you need to ensure no other SQLCMD has connected to SQL Server: </br>
98
+
> If you receive the following error, you must ensure no other SQLCMD has connected to SQL Server: </br>
94
99
> `Sqlcmd: Error: Microsoft ODBC Driver X for SQL Server : Login failed for user 'CONTOSO\BobD'. Reason: Server is in single user mode. Only one administrator can connect at this time..`
95
100
96
-
97
101
1.**Mixed Mode (optional):** If your SQL Server is running in mixed authentication mode, you can also:
98
-
1. Grant a SQL login Sysadmin role membership. Execute code such as the following to create a new SQL Server authentication login that is a member of the sysadmin fixed server role. Replace '************' with a strong password.
102
+
1. Grant a SQL login Sysadmin role membership. Execute code such as the following to create a new SQL Server authentication login that is a member of the sysadmin fixed server role. Replace "?j8:z$G=JE9" with a strong password of your choice.
99
103
100
-
```Command Prompt
101
-
sqlcmd.exe -E -S.\instancename -Q "CREATE LOGIN TempLogin WITH PASSWORD = '************'; ALTER SERVER ROLE sysadmin ADD MEMBER TempLogin; "
104
+
```powershell
105
+
$strong_password = "j8:zG=J?E9"
106
+
sqlcmd.exe -E -S $sql_server_instance -Q "CREATE LOGIN TempLogin WITH PASSWORD = '$strong_password'; ALTER SERVER ROLE sysadmin ADD MEMBER TempLogin; "
102
107
```
103
108
104
-
1. Also, if your SQL Server is running in mixed authentication mode and you want to reset the password of the **sa** account. Change the password of the sa account with the following syntax. Be sure to replace '************' with a strong password:
109
+
1. Also, if your SQL Server is running in mixed authentication mode and you want to reset the password of the enabled **sa** account. Change the password of the sa account with the following syntax. Be sure to replace "j8:zG=J?E9" with a strong password of your choice:
105
110
106
-
```Command Prompt
107
-
sqlcmd.exe -E -S.\instancename -Q "ALTER LOGIN sa WITH PASSWORD = '************'; "
111
+
```powershell
112
+
$strong_password = "j8:zG=J?E9"
113
+
sqlcmd.exe -E -S $sql_server_instance -Q "ALTER LOGIN sa WITH PASSWORD = $strong_password; "
108
114
```
109
115
110
116
1. Stop and restart your SQL Server instance in multi-user mode
111
117
112
-
```Command Prompt
113
-
net stop MSSQL$instancename& net start MSSQL$instancename
118
+
```powershell
119
+
net stop $service_name & net start $service_name
114
120
```
115
-
116
-
1. Verify that you can connect to SQL Server using the account for which you granted access and if that account is member of Sysadmin role.
0 commit comments