问题
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)
{
.................
}
回答1:
Try opening the connection before creating the ReplicationServer:
$con.Connect()
[Microsoft.SqlServer.Replication.ReplicationServer]$var_server=new-object ("Microsoft.SqlServer.Replication.ReplicationServer") $con
来源:https://stackoverflow.com/questions/20788945/issue-with-connecting-to-replicationserver-with-sql-authentication