SMO Powershell 在装载组件时甚至收支平衡

SMO Powershell broken even with loading assemblies

我整天都在处理一个 powershell 脚本,突然间它坏了。这是不起作用的示例:

#load assemblies for SMO
    $assemblylist =   
    "Microsoft.SqlServer.Management.Common",  
    "Microsoft.SqlServer.Smo",  
    "Microsoft.SqlServer.Dmf ",  
    "Microsoft.SqlServer.Instapi ",  
    "Microsoft.SqlServer.SqlWmiManagement ",  
    "Microsoft.SqlServer.ConnectionInfo ",  
    "Microsoft.SqlServer.SmoExtended ",  
    "Microsoft.SqlServer.SqlTDiagM ",  
    "Microsoft.SqlServer.SString ",  
    "Microsoft.SqlServer.Management.RegisteredServers ",  
    "Microsoft.SqlServer.Management.Sdk.Sfc ",  
    "Microsoft.SqlServer.SqlEnum ",  
    "Microsoft.SqlServer.RegSvrEnum ",  
    "Microsoft.SqlServer.WmiEnum ",  
    "Microsoft.SqlServer.ServiceBrokerEnum ",  
    "Microsoft.SqlServer.ConnectionInfoExtended ",  
    "Microsoft.SqlServer.Management.Collector ",  
    "Microsoft.SqlServer.Management.CollectorEnum",  
    "Microsoft.SqlServer.Management.Dac",  
    "Microsoft.SqlServer.Management.DacEnum",  
    "Microsoft.SqlServer.Management.Utility",
    "Microsoft.SqlServer.Management.Smo"


    foreach ($asm in $assemblylist)  
    {  
        $asm = [Reflection.Assembly]::LoadWithPartialName($asm)  
    }

$PrimaryInstance = "myserver"
$PrimaryConnection = New-Object ('Microsoft.SqlServer.Management.SMO.Server') $PrimaryInstance

我收到这个错误:

New-Object : Cannot find type [Microsoft.SqlServer.Management.SMO.Server]: verify that the assembly containing this type is loaded. At line:30 char:22

  • ... onnection = New-Object ('Microsoft.SqlServer.Management.SMO.Server') ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
    • FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

我什至重新启动了一切,这仍然是错误的。 运行 在其他地方使用此方法的同事说这对他们有用。

我已经确认我仍然可以连接到 SSMS 中的服务器,但是无论我尝试连接到哪个服务器,这对我来说仍然失败。

不确定发生了什么变化,但卸载并重新安装 SQLServer 模块然后在脚本顶部添加 import-module sqlserver 似乎已修复它