简体   繁体   English

Powershell SMO无法连接

[英]Powershell SMO Can't Connect

I'm having an issue connecting to a SQL Server using SMO when using a ServerConnection. 使用ServerConnection时,我在使用SMO连接到SQL Server时遇到问题。 For the last few months this has been working, but now failing. 在过去的几个月里,这一直在发挥作用,但现在却失败了。 Here is a snippet of the script: 这是脚本的片段:

        $svrname = "Server"

        #Establish Server connection
        Write-ColorOutput "Establishing SQL Server Connection to $svrName" "White"
        $mysvrConn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
        $mysvrConn.ServerInstance=$svrName
        $mysvrConn.LoginSecure = $false
        $mysvrConn.Login = "Admin"
        $mysvrConn.Password = "Password"

        $svr = new-object 'Microsoft.SqlServer.Management.SMO.Server' $mysvrConn

However, the following works. 但是,以下工作。

$svrname = "Server"
$svr = new-object ('Microsoft.SQLServer.Management.SMO.Server') $svrname

And the assemblies: 和组件:

        Write-ColorOutput "Loading assemblies" "White"
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.Smo") | out-null
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | out-null
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | out-null
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

You almost certainly have permission issues with your SQL login. 您几乎肯定会遇到SQL登录的权限问题。 You are either using the wrong password, or the login does not have the proper creds. 您要么使用了错误的密码,要么登录没有正确的信用。

Embarrassingly, when I had this issue, it turned out to be that I had entered the login password incorrectly. 令人尴尬的是,当我遇到这个问题时,原来是我输错了登录密码。

I had expected a specific error stating the login failed. 我曾预料到一个特定的错误,说明登录失败。 Instead it was the more generic: Failed to connect to server localhost. 相反,它更通用: Failed to connect to server localhost. , which threw me off. ,这让我失望了。

Like you, I ran a test with the $LoginSecure = $True which worked, telling me that the connection to the server was fine. 和你一样,我用$LoginSecure = $True运行测试,告诉我与服务器的连接没问题。 Only then did I spot my typo and was on my way to SMO bliss. 只有到那时我才发现我的错字并且正在前往SMO幸福的路上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM