Sample Header Ad - 728x90

How do I change the service accounts multiple SQL services run under, using PowerShell, and preserve dependencies?

0 votes
0 answers
171 views
I am in the process of converting all of our user-based service accounts on all MSSQL servers to gMSA and I have used PowerShell to script most of the required changes. I have hit a rather large roadblock when it comes to changing the SQL services to running under the new gMSA as the method I wrote will not preserve dependencies:
# Define the current account and the new gMSA
$currentAccount = "DOMAIN\\CurrentAccount"
$newAccount = "DOMAIN\\NewgMSA$"

# Get all services running under the current account
$services = Get-WmiObject -Class Win32_Service -Filter "StartName='$currentAccount'"

# Loop through each service
foreach ($service in $services) {
    # Stop the service
    $service.StopService() | Out-Null

    # Change the service account to the new gMSA
    $service.Change($null, $null, $null, $null, $null, $null, $newAccount, $null, $null, $null, $null) | Out-Null

    # Start the service
    $service.StartService() | Out-Null
}
Is there any way to do this programmatically or will I have to do this using Sql Server Configuration Manager? I have seen similar questions asked in the past but I was hoping that it might now be possible. I would appreciate any help I can get even if it is to tell me I have to do the manual approach. Thank you.
Asked by Dana H (1 rep)
Feb 16, 2024, 09:09 PM