Skip to content

Instantly share code, notes, and snippets.

@noogen
Last active October 17, 2017 20:02
Show Gist options
  • Save noogen/b48aeeefcd80d54e5e3c3286686c0695 to your computer and use it in GitHub Desktop.
Save noogen/b48aeeefcd80d54e5e3c3286686c0695 to your computer and use it in GitHub Desktop.
Powershell test connection to sql server
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=mssqlprod01;Initial Catalog=master;User Id=youruser;Password=yourpassword"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = 'SELECT @@SERVERNAME AS ServerName'
$SqlCmd.Connection = $SqlConnection
$SqlConnection.Open()
$rdr = $SqlCmd.ExecuteReader()
while($rdr.Read())
{
$rdr["ServerName"].ToString()
}
$SqlConnection.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment