Created
November 29, 2018 00:24
-
-
Save blark/d85717bf87c4c4024b4e9e579db02584 to your computer and use it in GitHub Desktop.
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
# | |
# 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