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