1

I have to change the service accounts of hundreds of SQL Servers and agents.

Microsoft recommends using the SQL Server configuration manager. This way works fine, but it is horrible if you have to do it hundred times.

On the more recent servers (Windows Server 2016, SQL Server 2016/2017) that PowerShell works fine:

$computer = $env:computername
[void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
[void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")
$wmi = New-Object ("Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer") $computer
$wmi.Services | Select Name, DisplayName, Type, StartMode, ServiceState, 
ServiceAccount | ft -auto
$sqlserver = $wmi.Services | where {$_.Type -eq "SqlServer"}
$sqlserver.SetServiceAccount($account, $pw)

But nearly every older server (Windows Server 2008-2012R2, SQL Server 2008-2014) gives me a empty WmiConnectionInfo an the exception:

ConnectionSettings : Microsoft.SqlServer.Management.Smo.Wmi.WmiConnectionInfo
Services           : 
ClientProtocols    : 
ServerInstances    : 
ServerAliases      : 
Urn                : ManagedComputer[@Name='SERVER']
Name               : SERVER
Properties         : {}
UserData           : 
State              : Existing
The following exception occurred while trying to enumerate the collection: "SQL Server WMI provider is not available on SERVER.".

On the older server I checked the loaded assembylies. [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo").FullName gives [...] Version=13.0.0.0 [...]. SQL Server 2014 is version 12, not 13.

But even if I load the right (?) versions of the dlls, the result stays the same.

[reflection.assembly]::Load("Microsoft.SqlServer.Smo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=...")
[reflection.assembly]::Load("Microsoft.SqlServer.SqlWmiManagement, Version=12.0.0.0, Culture=neutral, PublicKeyToken=...")

Do I have to load more assemblies with the correct version?

Is there another recommended way to change the SQL Server service accounts via PowerShell?

2 Answers 2

1

Interesting. There appears to be some incompatibility with the assemblies and Server 2012. If I run this against an SQL Server 2012 instance:

Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Add-Type -AssemblyName "Microsoft.SqlServer.SqlWmiManagement, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
$wmi = New-Object -TypeName "Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer" -ArgumentList $ServerName
$wmi.Services | Select-Object -Property Name, DisplayName, Type, StartMode, ServiceState, ServiceAccount | Format-Table -AutoSize

I get:

Name           DisplayName                                                 Type StartMode ServiceState
----           -----------                                                 ---- --------- ------------
MsDtsServer100 SQL Server Integration Services 10.0 SqlServerIntegrationService  Disabled      Stopped

(Note: I've cut off the ServiceAccount column here intentionally for privacy purposes.)

But against an SQL Server 2008 R2 instance, I get:

Name                   DisplayName                                                               Type StartMode ServiceState
----                   -----------                                                               ---- --------- ------------
MsDtsServer100         SQL Server Integration Services 10.0               SqlServerIntegrationService      Auto      Running
MSSQLFDLauncher        SQL Full-text Filter Daemon Launcher (MSSQLSERVER)                           9    Manual      Running
MSSQLSERVER            SQL Server (MSSQLSERVER)                                             SqlServer      Auto      Running
MSSQLServerOLAPService SQL Server Analysis Services (MSSQLSERVER)                      AnalysisServer      Auto      Running
ReportServer           SQL Server Reporting Services (MSSQLSERVER)                       ReportServer      Auto      Running
SQLBrowser             SQL Server Browser                                                  SqlBrowser  Disabled      Stopped
SQLSERVERAGENT         SQL Server Agent (MSSQLSERVER)                                        SqlAgent      Auto      Running

However, if I use v14 of the assemblies:

Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Add-Type -AssemblyName "Microsoft.SqlServer.SqlWmiManagement, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
$wmi = New-Object -TypeName "Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer" -ArgumentList $ServerName
$wmi.Services | Select-Object -Property Name, DisplayName, Type, StartMode, ServiceState, ServiceAccount | Format-Table -AutoSize

SQL Server 2012:

Name                   DisplayName                                                               Type StartMode ServiceState
----                   -----------                                                               ---- --------- ------------
MsDtsServer100         SQL Server Integration Services 10.0               SqlServerIntegrationService  Disabled      Stopped
MsDtsServer110         SQL Server Integration Services 11.0               SqlServerIntegrationService      Auto      Running
MSSQLFDLauncher        SQL Full-text Filter Daemon Launcher (MSSQLSERVER)                           9    Manual      Running
MSSQLSERVER            SQL Server (MSSQLSERVER)                                             SqlServer      Auto      Running
MSSQLServerOLAPService SQL Server Analysis Services (MSSQLSERVER)                      AnalysisServer      Auto      Running
ReportServer           SQL Server Reporting Services (MSSQLSERVER)                       ReportServer      Auto      Running
SQLBrowser             SQL Server Browser                                                  SqlBrowser      Auto      Running
SQLSERVERAGENT         SQL Server Agent (MSSQLSERVER)                                        SqlAgent      Auto      Running

SQL Server 2008 R2:

Name                   DisplayName                                                               Type StartMode ServiceState
----                   -----------                                                               ---- --------- ------------
MsDtsServer100         SQL Server Integration Services 10.0               SqlServerIntegrationService      Auto      Running
MSSQLFDLauncher        SQL Full-text Filter Daemon Launcher (MSSQLSERVER)                           9    Manual      Running
MSSQLSERVER            SQL Server (MSSQLSERVER)                                             SqlServer      Auto      Running
MSSQLServerOLAPService SQL Server Analysis Services (MSSQLSERVER)                      AnalysisServer      Auto      Running
ReportServer           SQL Server Reporting Services (MSSQLSERVER)                       ReportServer      Auto      Running
SQLBrowser             SQL Server Browser                                                  SqlBrowser  Disabled      Stopped
SQLSERVERAGENT         SQL Server Agent (MSSQLSERVER)                                        SqlAgent      Auto      Running

I don't have a SQL Server 2014 or later instance that I can safely test against, unfortunately.

I'm not sure if I'm getting version 14 of the assemblies from Microsoft's SqlServer PowerShell module from the PowerShell Gallery, or if I'm getting it from SSMS v17.

I actually have v10 of the assemblies still available on my system, and they work just like the v12 assemblies do.

I wonder if this is a bug or known issue.

Sign up to request clarification or add additional context in comments.

Comments

0

this approach solves my problem.

I check the loaded assemblies with this command:

$dlls = [System.AppDomain]::CurrentDomain.GetAssemblies()
$dlls | where {$_.Location -match "SQL"}

I believe, I have found a solution for the most cases (Windows Server 2008-2012R2, SQL Server 2008-2014).

Add-Type -path "C:\Program Files (x86)\Microsoft SQL Server\$version\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"
Add-Type -path "C:\Program Files (x86)\Microsoft SQL Server\$version\SDK\Assemblies\Microsoft.SqlServer.SqlWmiManagement.dll"

Sometimes the path is different. Sometimes the correct dlls are in the directory, which was specified as INSTALLSHAREDWOWDIR while installation. Maybe aftermaths of an inplace-upgrade or installation of a new management studio.

More recent servers (Windows Server 2016, SQL Server 2016/2017) works great with [void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo"), ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.