Sql server 从PowerShell访问SQL Server时出现问题

Sql server 从PowerShell访问SQL Server时出现问题,sql-server,powershell,Sql Server,Powershell,我正在尝试实现在中找到的备份管理脚本 文章说它取决于SQLPSX版本2.3.2.1或更高版本——我有最新的版本 正在尝试执行此行: $server = get-sqlserver $sqlserver 结果: New-Object : Cannot find type [Microsoft.SqlServer.Management.Common.ServerConnection]: make sure the assembly containing this type is loaded. A

我正在尝试实现在中找到的备份管理脚本

文章说它取决于SQLPSX版本2.3.2.1或更高版本——我有最新的版本

正在尝试执行此行:

$server = get-sqlserver $sqlserver
结果:

New-Object : Cannot find type [Microsoft.SqlServer.Management.Common.ServerConnection]: make sure the assembly containing this type is loaded.
At C:\Users\...\Documents\WindowsPowerShell\Modules\sqlserver\SQLServer.psm1:68 char:24
+     { $con = new-object <<<<  ("Microsoft.SqlServer.Management.Common.ServerConnection") $sqlserver }
回复:SMO

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") 
结果:

True   v2.0.50727     C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.ConnectionInfo\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.ConnectionInfo.dll                             
True   v2.0.50727     C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Smo\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Smo.dll                                                   
True   v2.0.50727     C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.SmoExtended\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.SmoExtended.dll                                   
New-Object : Cannot find type [Microsoft.SqlServer.Management.Common.ServerConnection]: make sure the assembly containing this type is loaded.

确保首先加载包含Microsoft.SqlServer.Management.Common.ServerConnection类型(我认为是Microsoft.SqlServer.ConnectionInfo)的程序集:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")

我意识到这是有史以来最糟糕的答案,但是。。。 看起来你的SMO可能有问题。您可以尝试加载或重新安装SSMS 2008。安装2008 R2版本的SSMS也是一种选择


同样,这不是世界上最好的答案,但可能值得一试。

只有在SSMS安装时安装了powershell 2.0,才会安装powershell组件。除非您使用的是Windows Server 2008,否则机器上通常已经安装了Powershell。确保在安装SSMS之前安装了powershell 2.0。

$sc=New Object Microsoft.SqlServer.Management.Common.ServerConnection

错误: 新对象:找不到类型[Microsoft.SqlServer.Management.Common.ServerConnection]:请验证是否加载了包含此类型的程序集

对于这个问题,

请确保安装了micro soft sqlserver管理对象2012(x64)。 如果给定路径“C:\Program files\Microsoft SQL Server\110\SDK\Assembly”中存在dll文件,请继续检查这些文件

导入模块-名称SqlServer
它可以停留在脚本中 注:

  • Windows Server 2016

  • 在我安装之前

  • With[Reflection.Assembly]::LoadWithPartialName仅适用于我的windows 10

您是否可以发布此结果:
get module-listavable
您需要按照自述文件和文档中的说明安装SMO它是否产生任何输出?您应该会看到有关该程序集的一些信息。如果它不产生任何输出,那么您的GAC中就没有该程序集,这意味着它将无法加载该程序集,因此无法加载服务器连接类型,从而导致上述错误。True v2.0.50727 C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.ConnectionInfo\10.0.0\uu 89845。。。
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
Install-Module -Name SqlServer