Last active
June 12, 2018 17:00
-
-
Save vaderj/28c3ec83804e568078402b670f3a8377 to your computer and use it in GitHub Desktop.
PowerShell => MS SQL - just to verify that a connection is correctly setup.This is equivalent to "The UDL trick" #PowerShell #SQL #Windows
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
# Open a connection to the SQL Server Database Engine | |
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection | |
$sqlConnection.ConnectionString = "Server=SQLDB2016\SHAREPOINT;Database=master;Integrated Security=True" | |
$sqlConnection.Open() | |
# Query the master database | |
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand | |
$sqlCommand.CommandText = "SELECT name FROM [master].[sys].[databases]" | |
$sqlCommand.Connection = $sqlConnection | |
$sqlDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter | |
$sqlDataAdapter.SelectCommand = $sqlCommand | |
$dataSet = New-Object System.Data.DataSet | |
$sqlDataAdapter.Fill($dataSet) | |
# Close the SQL Server connection | |
$sqlConnection.Close() | |
# Dump out the results | |
$dataSet.Tables |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment