通过安全密码连接到SQL Server

时间:2016-01-09 11:25:22

标签: sql-server powershell

我的目标是使用安全密码连接到SQL服务器。下面是我的脚本,但它失败了。任何人都可以找出错误是什么?我检查过实例名称是否正确。

$Path = "D:\AdminStuff\PowerShell\Password\Password.txt"
$uid = 'sa'
$pwd = Get-Content D:\AdminStuff\PowerShell\Password\Password.txt |
       ConvertTo-SecureString
$pwd.MakeReadOnly()

$creds = New-Object System.Data.SqlClient.SqlCredential($uid,$pwd)

$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = "Server=SLB-CLMFZ52\MSSQLSERVER;Database=master;"
$con.Credential = $creds
$con.Open()

Write-Host "--------------------------------------------------------"
Write-Host "Connection String : "
Write-Host $server.ConnectionContext.ConnectionString
Write-Host "--------------------------------------------------------"

$con.Close()

错误讯息:

Exception calling "Open" with "0" argument(s): "A network-related or instance-
specific error occurred while establishing a connection to SQL Server. The server
was not found or was not accessible. Verify that the instance name is correct and
that SQL Server is configured to allow remote connections. (provider: SQL Network
Interfaces, error: 25 - Connection string is not valid)"
At D:\AdminStuff\PowerShell\Password\Testing.ps1:12 char:10
+ $con.Open <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

1 个答案:

答案 0 :(得分:0)

如错误所示,您的问题是找到服务器,而不是登录。

Server=SLB-CLMFZ52\MSSQLSERVER

验证实例的名称以及实例正在侦听。服务器名称是否正确?您需要FQDN而不仅仅是'SLB-CLMFZ52'吗?