5

I am using this powershell script below to connect to an instance of sql server. I am pretty sure that my username and password are correct.

$connectionString = "server={0};database={1};uid={2};pwd={3};"

$c = Get-Credential
Write-Host($ipAddress)
Write-Host  $c.username 
Write-Host  $c.GetNetworkCredential().password

$connectionString = [string]::Format( "server={0};database={1};uid={2};pwd={3};", "servername", "databasename",$c.username,$c.GetNetworkCredential().password) 
#open database connection to SQL Server

Write-Host  $connectionString
$conn = New-Object system.Data.SqlClient.SqlConnection
$conn.connectionstring = $connectionString
$conn.open

    switch ($conn.State)
{
"Open" { Write-Host "Do some work"; }
Default { Write-Host "The connection is $($conn.State).  There has been an error connecting to the database."; }
}

It is always falling to the default statement.

1 Answer 1

4

Your call to $conn.open is missing the (), so it would be returning a reference to that method rather than calling it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.