Skip to content

Instantly share code, notes, and snippets.

@FrankSpierings
Last active July 14, 2021 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrankSpierings/4ee9f6dcea52a897bffdfe741fc215e3 to your computer and use it in GitHub Desktop.
Save FrankSpierings/4ee9f6dcea52a897bffdfe741fc215e3 to your computer and use it in GitHub Desktop.
PowerShell Oneliner to perform database queries.
powershell "$sql='SELECT @@VERSION';$c=(New-Object -TypeName System.Data.SqlClient.SqlConnection('server=SERVER;Database=DATABASE;Integrated Security=True;'));$c.open();$q=(New-Object System.Data.SqlClient.SqlCommand($sql,$c));$r=$q.ExecuteReader();$oo=@();while ($r.Read()){$o=(New-Object PSObject);for ($i=0;$i -lt $r.FieldCount;$i++){$n=$r.GetName($i);if($n -eq ''){$n='column_'+$i};$o|Add-Member -type NoteProperty -Name $n -Value $r[$i];}$oo+=$o};$oo|FT -Wrap"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment