Question

I am trying to use PowerShell to connect to ReplicationServer and backup replication. The SQL Server Edition is 2008 SP2. It works fine with Windows authentication but when I try to connect with SQL Server authentication it shows error

New-Object : Cannot find an overload for "ReplicationServer" and the argument count: "1". At .....\Experiment.ps1:34 char:74 + [Microsoft.SqlServer.Replication.ReplicationServer]$var_server=new-object <<<< ("Microsoft.SqlServer.Replication.ReplicationServer") $con + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Here is the code snippet

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")  
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")  
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Replication") | out-null
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Rmo") | out-null

$script:servername="server1"
$loginname="test"
$passcode="test"

[Microsoft.SqlServer.Management.Common.ServerConnection]$con=new-object ("Microsoft.SqlServer.Management.Common.ServerConnection")  $servername,$loginname,$passcode

[Microsoft.SqlServer.Replication.ReplicationServer]$var_server=new-object ("Microsoft.SqlServer.Replication.ReplicationServer") $con

foreach($replicateddatabase in $var_server.ReplicationDatabases) 
{
   .................
}
Was it helpful?

Solution

Try opening the connection before creating the ReplicationServer:

$con.Connect()
[Microsoft.SqlServer.Replication.ReplicationServer]$var_server=new-object ("Microsoft.SqlServer.Replication.ReplicationServer") $con

OTHER TIPS

Connection does not have to be opened, wrong object type is being passed to ReplicationServer constructor.

This is what should be done.

[Microsoft.SqlServer.Replication.ReplicationServer]$var_server=new-object ("Microsoft.SqlServer.Replication.ReplicationServer") $con.SqlConnectionObject
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top