Created
September 15, 2020 19:08
-
-
Save mirontoli/08e0f1bfde3dd79ccde59fb266af272c to your computer and use it in GitHub Desktop.
This file contains 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
# do you want to check something in a mssql database and don't have sql server management studio, then powershell is your best friend | |
# best parts from that blog post: https://www.sqlshack.com/connecting-powershell-to-sql-server/ | |
$sqlConn = New-Object System.Data.SqlClient.SqlConnection | |
$sqlConn.ConnectionString = "data source=servername;initial catalog=<database_name>;Integrated Security=SSPI;MultipleActiveResultSets=True;App=EntityFramework" | |
$sqlcmd = $sqlConn.CreateCommand() | |
$sqlcmd.Connection = $sqlConn | |
$query = "SELECT name, database_id FROM sys.databases" | |
$sqlcmd.CommandText = $query | |
# repeat this for every command | |
$adp = New-Object System.Data.SqlClient.SqlDataAdapter $sqlcmd | |
$data = New-Object System.Data.DataSet | |
$adp.Fill($data) | Out-Null | |
# output data | |
$data.Tables | |
$queryTables = "SELECT TABLE_NAME FROM <database_name>.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'" | |
$sqlcmd.CommandText = $queryTables | |
$queryTable = "SELECT * FROM <table_name>" | |
$sqlcmd.CommandText = $queryTable | |
$queryUnique = "SELECT DISTINCT SiteUrl FROM <table_name> ORDER BY SiteUrl" | |
$sqlcmd.CommandText = $queryUnique |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment