1
- function Get-DbaDatabaseFreespace {
1
+ function Get-DbaDatabaseFreespace
2
+ {
2
3
<#
3
4
. SYNOPSIS
4
5
Returns database file space information for database files on a SQL instance.
@@ -36,7 +37,13 @@ PSCredential object to connect under. If not specified, currend Windows login wi
36
37
37
38
. PARAMETER IncludeSystemDBs
38
39
Switch parameter that when used will display system database information
40
+
41
+ . PARAMETER Databases
42
+ Specify one or more databases to process.
39
43
44
+ . PARAMETER Exclude
45
+ Specify one or more databases to exclude.
46
+
40
47
. LINK
41
48
https://dbatools.io/Get-DbaDatabaseFreespace
42
49
@@ -56,84 +63,117 @@ the output object by any files that have a percent used of greater than 80%.
56
63
57
64
Returns all user database files and free space information for the localhost and
58
65
localhost\namedinstance SQL Server instances. Processes data via the pipeline.
66
+
67
+ . EXAMPLE
68
+ Get-DbaDatabaseFreespace -SqlServer localhost -Databases db1, db2
69
+
70
+ Returns database files and free space information for the db1 and db2 on localhost.
59
71
#>
60
- [CmdletBinding (SupportsShouldProcess = $true )]
61
- param ([parameter (ValueFromPipeline , Mandatory = $true )]
72
+ [CmdletBinding ()]
73
+ param ([parameter (ValueFromPipeline , Mandatory = $true )]
62
74
[Alias (" ServerInstance" , " SqlInstance" )]
63
- [object []]$SqlServer
64
- , [System.Management.Automation.PSCredential ]$SqlCredential
65
- , [switch ] $IncludeSystemDBs )
66
- BEGIN {
67
- $outputraw = @ ()
68
- $sql = @'
69
- SELECT
70
- @@SERVERNAME as SqlServer
71
- ,DB_NAME() as DBName
72
- ,f.name AS [FileName]
73
- ,fg.name AS [Filegroup]
74
- ,f.physical_name AS [PhysicalName]
75
- ,CAST(CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS int)/128.0 AS DECIMAL(15,2)) as [UsedSpaceMB]
76
- ,CAST(f.size/128.0 - CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS int)/128.0 AS DECIMAL(15,2)) AS [FreeSpaceMB]
77
- ,CAST((f.size/128.0) AS DECIMAL(15,2)) AS [FileSizeMB]
78
- ,CAST((FILEPROPERTY(f.name, 'SpaceUsed')/(f.size/1.0)) * 100 as DECIMAL(15,2)) as [PercentUsed]
79
- FROM sys.database_files AS f WITH (NOLOCK)
80
- LEFT OUTER JOIN sys.filegroups AS fg WITH (NOLOCK)
81
- ON f.data_space_id = fg.data_space_id
82
- '@
83
- }
84
-
85
- PROCESS {
86
- foreach ($s in $SqlServer ){
87
- # For each SQL Server in collection, connect and get SMO object
88
- Write-Verbose " Connecting to $SqlServer "
89
- $server = Connect-SqlServer $SqlServer - SqlCredential $SqlCredential
90
- # If IncludeSystemDBs is true, include systemdbs
91
- # only look at online databases (Status equal normal)
92
- try {
93
- if ($IncludeSystemDBs ){
94
- $dbs = $server.Databases | Where-Object {$_.status -eq ' Normal' }
95
- } else {
96
- $dbs = $server.Databases | Where-Object {$_.status -eq ' Normal' -and $_.IsSystemObject -eq 0 }
97
- }
98
- }
99
- catch {
100
- Write-Exception $_
101
- throw " Unable to gather dbs for $ ( $s.name ) "
102
- continue
103
- }
104
-
105
- foreach ($db in $dbs ){
106
- try {
107
- Write-Verbose " Querying $ ( $s ) - $ ( $db.name ) ."
108
- # Execute query against individual database and add to output
109
- $outputraw += ($db.ExecuteWithResults ($sql )).Tables[0 ]
110
- }
111
- catch {
112
- Write-Exception $_
113
- throw " Unable to query $ ( $s ) - $ ( $db.name ) "
114
- continue
115
- }
116
- }
117
- }
118
- }
119
- END {
120
- # Sanitize output into array of custom objects, not DataRow objects
121
- Write-Verbose ' Sanitizing outupt, converting DataRow to custom PSObject.'
122
- $output = @ ()
123
- foreach ($row in $outputraw ){
124
- $outrow = [ordered ]@ {' SqlServer' = $row.SqlServer ;`
125
- ' DatabaseName' = $row.DBName ;`
126
- ' FileName' = $row.FileName ;`
127
- ' FileGroup' = $row.FileGroup ;`
128
- ' PhysicalName' = $row.PhysicalName ;`
129
- ' UsedSpaceMB' = $row.UsedSpaceMB ;`
130
- ' FreeSpaceMB' = $row.FreeSpaceMB ;`
131
- ' FileSizeMB' = $row.FileSizeMB ;`
132
- ' PercentUsed' = $row.PercentUSed }
133
- $output += New-Object psobject - Property $outrow
134
- }
135
-
136
-
137
- return $output
138
- }
75
+ [object []]$SqlServer ,
76
+ [System.Management.Automation.PSCredential ]$SqlCredential ,
77
+ [switch ]$IncludeSystemDBs )
78
+
79
+ DynamicParam { if ($SqlServer ) { return Get-ParamSqlDatabases - SqlServer $SqlServer [0 ] - SqlCredential $SourceSqlCredential } }
80
+
81
+ BEGIN
82
+ {
83
+ $outputraw = @ ()
84
+ $sql = " SELECT
85
+ @@SERVERNAME as SqlServer
86
+ ,DB_NAME() as DBName
87
+ ,f.name AS [FileName]
88
+ ,fg.name AS [Filegroup]
89
+ ,f.physical_name AS [PhysicalName]
90
+ ,CAST(CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS int)/128.0 AS DECIMAL(15,2)) as [UsedSpaceMB]
91
+ ,CAST(f.size/128.0 - CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS int)/128.0 AS DECIMAL(15,2)) AS [FreeSpaceMB]
92
+ ,CAST((f.size/128.0) AS DECIMAL(15,2)) AS [FileSizeMB]
93
+ ,CAST((FILEPROPERTY(f.name, 'SpaceUsed')/(f.size/1.0)) * 100 as DECIMAL(15,2)) as [PercentUsed]
94
+ FROM sys.database_files AS f WITH (NOLOCK)
95
+ LEFT OUTER JOIN sys.filegroups AS fg WITH (NOLOCK)
96
+ ON f.data_space_id = fg.data_space_id"
97
+
98
+ $databases = $psboundparameters.Databases
99
+ $exclude = $psboundparameters.Exclude
100
+ }
101
+
102
+ PROCESS
103
+ {
104
+ foreach ($s in $SqlServer )
105
+ {
106
+ # For each SQL Server in collection, connect and get SMO object
107
+ Write-Verbose " Connecting to $s "
108
+ $server = Connect-SqlServer $s - SqlCredential $SqlCredential
109
+ # If IncludeSystemDBs is true, include systemdbs
110
+ # only look at online databases (Status equal normal)
111
+ try
112
+ {
113
+ if ($databases.length -gt 0 )
114
+ {
115
+ $dbs = $server.Databases | Where-Object { $databases -contains $_.Name }
116
+ }
117
+ elseif ($IncludeSystemDBs )
118
+ {
119
+ $dbs = $server.Databases | Where-Object { $_.status -eq ' Normal' }
120
+ }
121
+ else
122
+ {
123
+ $dbs = $server.Databases | Where-Object { $_.status -eq ' Normal' -and $_.IsSystemObject -eq 0 }
124
+ }
125
+
126
+ if ($exclude.length -gt 0 )
127
+ {
128
+ $dbs = $dbs | Where-Object { $exclude -notcontains $_.Name }
129
+ }
130
+ }
131
+ catch
132
+ {
133
+ Write-Exception $_
134
+ throw " Unable to gather dbs for $ ( $s.name ) "
135
+ continue
136
+ }
137
+
138
+ foreach ($db in $dbs )
139
+ {
140
+ try
141
+ {
142
+ Write-Verbose " Querying $ ( $s ) - $ ( $db.name ) ."
143
+ # Execute query against individual database and add to output
144
+ $outputraw += ($db.ExecuteWithResults ($sql )).Tables[0 ]
145
+ }
146
+ catch
147
+ {
148
+ Write-Exception $_
149
+ throw " Unable to query $ ( $s ) - $ ( $db.name ) "
150
+ continue
151
+ }
152
+ }
153
+ }
154
+ }
155
+ END
156
+ {
157
+ # Sanitize output into array of custom objects, not DataRow objects
158
+ Write-Verbose ' Sanitizing outupt, converting DataRow to custom PSObject.'
159
+ $output = @ ()
160
+ foreach ($row in $outputraw )
161
+ {
162
+ $outrow = [ordered ]@ {
163
+ ' SqlServer' = $row.SqlServer ;`
164
+ ' DatabaseName' = $row.DBName ;`
165
+ ' FileName' = $row.FileName ;`
166
+ ' FileGroup' = $row.FileGroup ;`
167
+ ' PhysicalName' = $row.PhysicalName ;`
168
+ ' UsedSpaceMB' = $row.UsedSpaceMB ;`
169
+ ' FreeSpaceMB' = $row.FreeSpaceMB ;`
170
+ ' FileSizeMB' = $row.FileSizeMB ;`
171
+ ' PercentUsed' = $row.PercentUSed
172
+ }
173
+ $output += New-Object psobject - Property $outrow
174
+ }
175
+
176
+
177
+ return $output
178
+ }
139
179
}
0 commit comments