Last active
October 17, 2017 20:02
-
-
Save noogen/b48aeeefcd80d54e5e3c3286686c0695 to your computer and use it in GitHub Desktop.
Powershell test connection to sql server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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