I am using Powershell to try to create an object of type Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore. According to the constructor, it ought to be able to be created using a Microsoft.SqlServer.Management.Common.ServerConnection object as a parameter. With that in mind, I am trying to run the following code:
$ServerConnection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection('ServerName')
$ServerConnection
$store = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($ServerConnection)
The assignment for $ServerConnection is successful and printing the object's information verifies that. However, the assignment for $store gives this error:
New-Object : Cannot find an overload for "RegisteredServersStore" and the argument count: "1".
At line:4 char:10
+ $store = New-Object Microsoft.SqlServer.Management.RegisteredServers. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
I am confused as to why the constructor for this class is throwing that error when the documentation suggests that it should work with that parameter.
I have tried it without the parameter and it does initialize in that case; I was hoping perhaps there was a method to instantiate a connection to the server, but I don't see anything like that. I am using Powershell 5.1.17763.4720 and the SqlServer module is on version 22.1.1.
For the record, this came about through attempting to use the dbatools function Add-DbaRegServer, which then calls a couple of other dbatools functions, including Get-DbaRegServerStore, of which line 63 is the problematic one:
$store = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($server.ConnectionContext)
My code above is an attempt at recreating this in a controlled environment. Any insight would be appreciated!