Skip to content

Commit e02f942

Browse files
Get-DbaDiskSpace - Make Unit work and clean up the two other dead parameters (#10611)
1 parent 4104e0b commit e02f942

2 files changed

Lines changed: 48 additions & 18 deletions

File tree

public/Get-DbaDiskSpace.ps1

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function Get-DbaDiskSpace {
44
Retrieves disk space and filesystem details from SQL Server host systems for capacity monitoring and performance analysis.
55
66
.DESCRIPTION
7-
Queries Windows disk volumes on SQL Server systems using WMI to gather critical storage information for database administration. Returns comprehensive disk details including capacity, free space, filesystem type, and optional fragmentation analysis.
7+
Queries Windows disk volumes on SQL Server systems using WMI to gather critical storage information for database administration. Returns comprehensive disk details including capacity, free space and filesystem type.
88
99
Essential for SQL Server capacity planning, this function helps DBAs monitor disk space before growth limits impact database operations. Use it to verify adequate space for backup operations, identify performance bottlenecks from fragmented volumes hosting data or log files, and maintain compliance documentation for storage utilization.
1010
@@ -20,23 +20,16 @@ function Get-DbaDiskSpace {
2020
Credential object used to connect to the computer as a different user.
2121
2222
.PARAMETER Unit
23-
This parameter has been deprecated and will be removed in 1.0.0.
24-
All size properties (Bytes, KB, MB, GB, TB, PB) are now available simultaneously in the output object but hidden by default for cleaner display.
25-
26-
.PARAMETER SqlCredential
27-
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
28-
29-
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
30-
31-
For MFA support, please use Connect-DbaInstance.
23+
Displays the Capacity and Free values in the specified unit: Bytes, KB, MB, GB or TB.
24+
By default each value picks a human friendly unit for display ("1.82 TB"). The underlying values are not changed, and every unit stays available through the SizeIn* and FreeIn* properties of the output object.
3225
3326
.PARAMETER ExcludeDrive
3427
Specifies drive letters to exclude from the disk space report, using the format 'C:\' or 'D:\'.
3528
Use this to skip system drives or non-SQL storage when focusing on database file locations, or to exclude network drives that may cause timeouts.
3629
3730
.PARAMETER CheckFragmentation
38-
Enables filesystem fragmentation analysis for all volumes, which can impact SQL Server I/O performance when database or log files are stored on fragmented drives.
39-
This significantly increases runtime (seconds to minutes per volume) but provides critical data for troubleshooting slow database operations or planning defragmentation maintenance.
31+
This parameter is deprecated and will cause the command to stop with a warning.
32+
The fragmentation analysis was removed from this command because the underlying DefragAnalysis call took minutes per volume.
4033
4134
.PARAMETER Force
4235
Includes all drive types and hidden volumes in the results, not just local and removable disks (DriveType 2 and 3).
@@ -70,8 +63,8 @@ function Get-DbaDiskSpace {
7063
- ComputerName: The name of the computer
7164
- Name: The volume name (drive letter or UNC path, e.g., 'C:\' or '\\server\share')
7265
- Label: The volume label/name if assigned
73-
- Capacity: Total disk capacity in the specified unit (default GB)
74-
- Free: Free space available in the specified unit (default GB)
66+
- Capacity: Total disk capacity, displayed in a human friendly unit or in the unit requested with -Unit
67+
- Free: Free space available, displayed in a human friendly unit or in the unit requested with -Unit
7568
- PercentFree: Percentage of disk space that is free
7669
- BlockSize: File system block size in bytes
7770
@@ -125,28 +118,43 @@ function Get-DbaDiskSpace {
125118
[Parameter(ValueFromPipeline)]
126119
[DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
127120
[PSCredential]$Credential,
128-
[ValidateSet('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB')]
129-
[string]$Unit = 'GB',
130-
[PSCredential]$SqlCredential,
121+
[ValidateSet("Bytes", "KB", "MB", "GB", "TB")]
122+
[string]$Unit,
131123
[string[]]$ExcludeDrive,
132124
[switch]$CheckFragmentation,
133125
[switch]$Force,
134126
[switch]$EnableException
135127
)
136128

137129
begin {
130+
if ($CheckFragmentation) {
131+
Stop-Function -Message "The parameter CheckFragmentation is deprecated. The fragmentation analysis has been removed from this command because the underlying DefragAnalysis call took minutes per volume."
132+
return
133+
}
138134

139135
$condition = " WHERE DriveType = 2 OR DriveType = 3"
140136
if (Test-Bound 'Force') {
141137
$condition = ""
142138
}
143139

140+
# The Capacity and Free properties are Size objects that pick a human friendly display unit.
141+
# A requested unit overrides that display without changing the underlying values.
142+
if (Test-Bound "Unit") {
143+
if ($Unit -eq "Bytes") {
144+
$unitStyle = [Dataplat.Dbatools.Utility.SizeStyle]::Byte
145+
} else {
146+
$unitStyle = [Dataplat.Dbatools.Utility.SizeStyle]$Unit
147+
}
148+
}
149+
144150
# Keep track of what computer was already processed to avoid duplicates
145151
$processed = New-Object System.Collections.ArrayList
146152

147153
}
148154

149155
process {
156+
if (Test-FunctionInterrupt) { return }
157+
150158
foreach ($computer in $ComputerName) {
151159
if ($computer.ComputerName -notin $processed) {
152160
$null = $processed.Add($computer.ComputerName)
@@ -181,6 +189,11 @@ function Get-DbaDiskSpace {
181189
$info.FileSystem = $disk.FileSystem
182190
$info.Type = $disk.DriveType
183191

192+
if ($unitStyle) {
193+
if ($null -ne $info.Capacity) { $info.Capacity.Style = $unitStyle }
194+
if ($null -ne $info.Free) { $info.Free.Style = $unitStyle }
195+
}
196+
184197
$info
185198
}
186199
}

tests/Get-DbaDiskSpace.Tests.ps1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Describe $CommandName -Tag UnitTests {
1414
"ComputerName",
1515
"Credential",
1616
"Unit",
17-
"SqlCredential",
1817
"ExcludeDrive",
1918
"CheckFragmentation",
2019
"Force",
@@ -45,4 +44,22 @@ Describe $CommandName -Tag IntegrationTests {
4544
$systemDriveResults.SizeInGB -gt 0 | Should -Be $true
4645
}
4746
}
47+
48+
Context "Honors the Unit parameter" {
49+
It "Displays Capacity and Free in the requested unit" {
50+
$results = Get-DbaDiskSpace -ComputerName $env:COMPUTERNAME -Unit MB
51+
$systemDriveResult = $results | Where-Object Name -eq "$env:SystemDrive\"
52+
"$($systemDriveResult.Capacity)" | Should -Match "MB$"
53+
"$($systemDriveResult.Free)" | Should -Match "MB$"
54+
$systemDriveResult.SizeInGB -gt 0 | Should -Be $true
55+
}
56+
}
57+
58+
Context "Deprecated parameters" {
59+
It "Stops with a message when CheckFragmentation is used" {
60+
$results = Get-DbaDiskSpace -ComputerName $env:COMPUTERNAME -CheckFragmentation -WarningAction SilentlyContinue
61+
$results | Should -BeNullOrEmpty
62+
$WarnVar | Should -Match "CheckFragmentation"
63+
}
64+
}
4865
}

0 commit comments

Comments
 (0)