ConfigureSQLAccounts.ps1 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. #
  3. # THIS SAMPLE CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  4. # WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  5. # WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  6. # IF THIS CODE AND INFORMATION IS MODIFIED, THE ENTIRE RISK OF USE OR RESULTS IN
  7. # CONNECTION WITH THE USE OF THIS CODE AND INFORMATION REMAINS WITH THE USER.
  8. # The purpose of this script is to configure the SQL Server startup account and also enable
  9. # LPIM and IFI for the account.
  10. # We would create a local user on the machine and use that as the startup account for SQL.
  11. # For simplicity the account would be added to the builtin Administrator group.
  12. Try
  13. {
  14. $connect = [ADSI]"WinNT://localhost"
  15. $user = $connect.Create("User","SQLServiceAccount")
  16. $user.SetPassword("LS1setup!")
  17. $user.setinfo()
  18. $user.description = "SQL Server Startup Account"
  19. $user.SetInfo()
  20. #Add Account to the Admin Group
  21. $Admingroup = [ADSI]("WinNT://"+$env:COMPUTERNAME +"/administrators,group")
  22. $AdminGroup.Add("WinNT://"+$env:ComputerName +"/SQLServiceAccount,user")
  23. #Now Change SQL Server Startup Account and Restart the services.
  24. Import-Module sqlps -DisableNameChecking
  25. Start-Sleep -Seconds 10
  26. $SMOWmiserver = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $env:COMPUTERNAME
  27. $ChangeService=$SMOWmiserver.Services | where {$_.name -eq "MSSQLSERVER"}
  28. $UName=$env:COMPUTERNAME + "\SQLServiceAccount"
  29. $PWord="LS1setup!"
  30. $ChangeService.SetServiceAccount($UName, $PWord)
  31. }
  32. Catch
  33. {
  34. Write-Host "***Erorr Configuring the SQL Startup Account****" -ForegroundColor Red
  35. }