0
votes

My objective is to connect to SQL server with secured password. Below is my script, but it failed. Anyone can pinpoint what the mistake is? I've checked instance name was correct.

$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()

Error message:

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
Did you verify that you can actually connect to the server in the first place? - Ansgar Wiechers

1 Answers

0
votes

As the error says, your problem is locating the server, not logging in.

Server=SLB-CLMFZ52\MSSQLSERVER

Verify the name of the instance and that the instance is listening. Is the server name correct? Do you need the FQDN rather than just 'SLB-CLMFZ52' ?