Skip to content

Commit 417b99f

Browse files
Added get-dbadatabasesize
1 parent d850480 commit 417b99f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Get-DBADatabaseSize.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function Get-DBADatabaseSize
2+
{
3+
<#
4+
.Synopsis
5+
Gets the size of the databases on an instance in Mb
6+
.DESCRIPTION
7+
returns a database smo object of the size of the databases on an instance in Mb and enables top N if required
8+
.EXAMPLE
9+
Get-DBADatabaseSize -Instance SERVER
10+
11+
Returns the database names and sizes in Mb ordered by size for all of the databases on SERVER
12+
.EXAMPLE
13+
Get-DBADatabaseSize -Instance SERVER -Top 3
14+
15+
Returns the database names and sizes in Mb ordered by size for the 3 largest databases on SERVER
16+
.OUTPUTS
17+
Database object
18+
.NOTES
19+
AUTHOR - Rob Sewell https://sqldbawithabeard.com
20+
DATE - 30/10/2016
21+
#>
22+
#Requires -Version 5
23+
[OutputType([object])]
24+
param(
25+
## The Name of the instance
26+
[Parameter(Position=0,Mandatory=$true)]
27+
[string]$Instance,
28+
[Parameter(Position=1,Mandatory=$false)]
29+
## The number of results to show
30+
[int]$Top
31+
)
32+
[void][reflection.assembly]::LoadWithPartialName( "Microsoft.SqlServer.Smo" );
33+
$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $Instance
34+
if(!$Top)
35+
{
36+
$srv.databases | Sort-Object -Descending size|Select-Object Name , Size
37+
}
38+
else
39+
{
40+
$srv.databases | Sort-Object -Descending size|Select-Object Name , Size -First $Top
41+
}
42+
}

0 commit comments

Comments
 (0)