Issue with connecting to ReplicationServer with sql authentication

家住魔仙堡 提交于 2019-12-13 21:35:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!