Skip to content

Instantly share code, notes, and snippets.

@blark
Created November 29, 2018 00:24
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 blark/d85717bf87c4c4024b4e9e579db02584 to your computer and use it in GitHub Desktop.
Save blark/d85717bf87c4c4024b4e9e579db02584 to your computer and use it in GitHub Desktop.
#
# A bunch of MSSQL shortcuts
#
Import-Module Sqlps -DisableNameChecking
function Get-Databases {
Invoke-Sqlcmd -Query "sp_databases"
}
function Get-Tables {
Param (
[Parameter(Mandatory=$True)][String]$Database
)
$params = @{
"Query"=("SELECT TABLE_NAME FROM [{0}].INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'" -f $Database)
}
Invoke-Sqlcmd @params | Format-Table -Wrap -AutoSize
}
function Get-DBInfo {
Param (
[Parameter(Mandatory=$True)][String]$OutPath
)
Begin {
Write-Output "Getting database list."
$DBs = Get-Databases
("# {0} Database List`n{1}" -f $env:computername,($DBs|Out-String)) |
Out-File -FilePath $OutPath
}
Process {
ForEach ($item in $DBs) {
$d = $item.DATABASE_NAME
Write-Output ("Dumping table information for: {0}" -f $d)
$t = Get-Tables -Database $d
("## {0}`n{1}" -f $d,($t|Out-String)) | Out-File -Append -FilePath $OutPath
}
}
End {
Write-Output "Done."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment