Create a Secondary SA (Admin) Account for MSSQL

Create a Secondary SA (Admin) Account for MSSQL

In the event that you have lost or forgotten your SQL administrator password, the following script can create an additional administrator SA user account that you can use to log in.  This is preferable over resetting the SA password because changing the password would break any applications that had the old password stored.

 

  1. $InstanceName = 'MSSQLServer'
  2. $NewUser = 'sa2'
  3. $NewPassword = '12AeAOuaOE,.'

  4. #--------------------

  5. $Instance_List_Path = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\"


  6. if(!$InstanceName){
  7. $Instances = (Get-Item $Instance_List_Path).Property
  8. if($Instances.Count -gt 1){
  9. Write-Host "No instance name specified. Valid values are:"
  10. Write-Host $Instances
  11. } elseif ($Instances.Count -eq 1) {
  12. $InstanceName = $Instances;
  13. Write-Information "SQL Instance not specified. Using $($InstanceName)"
  14. } else {
  15. Write-Host "Unable to detect SQL instances."
  16. }
  17. }

  18. if($InstanceName){

  19. $ServiceDisplayName = "SQL Server ($($InstanceName))"
  20. $ServiceName = Get-Service -DisplayName $ServiceDisplayName


  21. $InstanceLocation = Get-ItemPropertyValue $Instance_List_Path -Name $InstanceName

  22. $InstanceParameters = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$($InstanceLocation)\MSSQLServer\Parameters"
  23. $ArgName = "SQLArg99"

  24. Set-ItemProperty -Path $InstanceParameters -Name $ArgName -Value "-m"

  25. Stop-Service $ServiceName -Force
  26. Start-Service $ServiceName

  27. Start-Sleep -Seconds 1

  28. if($InstanceName -ieq 'MSSQLServer'){
  29. $ServerInstance = "(local)"
  30. } else {
  31. $ServerInstance = "(local)\$($InstanceName)"
  32. }

  33. $Query = "
  34. --Enable Administrator Access
  35. CREATE LOGIN [BUILTIN\Administrators] FROM WINDOWS
  36. GO

  37. ALTER SERVER ROLE sysadmin ADD MEMBER [BUILTIN\Administrators]
  38. GO

  39. --Enable Regular User Access
  40. --CREATE LOGIN [BUILTIN\Users] FROM WINDOWS
  41. --GO

  42. ALTER SERVER ROLE sysadmin ADD MEMBER [BUILTIN\Users]
  43. GO

  44. --Create a secondary admin user
  45. CREATE LOGIN [$($NewUser)] WITH PASSWORD=N'$($NewPassword)', CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
  46. GO

  47. ALTER SERVER ROLE [sysadmin] ADD MEMBER [$($NewUser)]
  48. GO
  49. "
  50. Invoke-Sqlcmd -ServerInstance $ServerInstance -Query $Query

  51. Remove-ItemProperty -Path $InstanceParameters -Name $ArgName
  52. Stop-Service $ServiceName -Force
  53. Start-Service $ServiceName

  54. }

 


    • Related Articles

    • Create a MSSQL BACPAC Backup

      To create a BACPAC backup, do the following steps: Open Management Studio and connect to the database server. Right click on the database you wish to backup and select Tasks > Export Data-tier Application... In the dialog that appears click "Next >" ...
    • Create a MSSQL BAK Backup

      To create a BAK backup, do the following steps: 1. Shrink the Database (Optional) Open Management Studio and connect to the database server. Change the Recovery Model to Simple Right click on the database you wish to backup and select Properties. ...
    • Convert a Postgres database to MSSQL

      Full Convert by Spectral Core is a robust tool for converting databases from one format to another. The following instructions will walk you through using Full Convert to convert a Postgres database to MSSQL. Launch Full Convert Click New database ...
    • Convert a CTREE Database to MSSQL

      CTREE Databases are a legacy database format that has been used by some applications. In order to work with them, you'll need to do the following: Install CTREE ODBC Drivers Create a "Table List" Configure the ODBC Drivers to use the Table List Use ...
    • Restore a MSSQL BAK Backup

      To restore a MSSQL BAK database backup, do the following steps: 1. Open Management Studio and connect to the database server. 2. Right-Click on Databases and select Restore Database... 3. In the dialog that appears, select Device and then click the ...