functions/Get-DbaDiskSpace.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
Function Get-DbaDiskSpace
{
<#
.SYNOPSIS
Displays Disk information for all local drives on a server
 
.DESCRIPTION
Returns a custom object with Server name, name of disk, label of disk, total size, free size, percent free, block size and filesystem.
 
By default, this funtion only shows drives of type 2 and 3 (removable disk and local disk).
 
Requires: Windows administrator access on SQL Servers
 
.PARAMETER ComputerName
The SQL Server (or server in general) that you're connecting to. The -SqlServer parameter also works.
 
.PARAMETER Unit
Display the disk space information in a specific unit. Valid values include 'Bytes', 'KB', 'MB', 'GB', 'TB', and 'PB'. Default is GB.
 
.PARAMETER CheckForSql
Check to see if any SQL Data or Log files exists on the disk. Uses Windows authentication to connect by default.
 
.PARAMETER SqlCredential
If you want to use SQL Server Authentication to connect.
 
.PARAMETER CheckFragmentation
Includes a check for fragmentation in all filesystems. This will increase the runtime of the function, as a fragmentation check can take seconds or even minutes for a single volume.
 
.PARAMETER AllDrives
Without this switch, the function will only return information about drivetype 2 and 3 (removable disk and local disk).
When this switch is used, data from all types of disks are returned:
 
 Value Type of disk
   0 Unknown
   1 No Root Directory
   2 Removable Disk
   3 Local Disk
   4 Network Drive
   5 Compact Disk
   6 RAM Disk
 
https://msdn.microsoft.com/en-us/library/aa394515.aspx
 
.PARAMETER Detailed
Includes information about filesystem (FAT32, NTFS, ReFS, etc.), as well as the information provided by -CheckForSql and -AllDrives.
Also includes volumes normally excluded, such as '\\?\Volume*' volumes.
 
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
 
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
 
.NOTES
Tags: Storage
Author: Chrissy LeMaire (clemaire@gmail.com) & Jakob Bindslet (jakob@bindslet.dk)
 
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.LINK
https://dbatools.io/Get-DbaDiskSpace
 
.EXAMPLE
Get-DbaDiskSpace -ComputerName srv0042 | Format-Table -AutoSize
 
Get diskspace for the server srv0042
 
Server Name Label SizeInGB FreeInGB PercentFree BlockSize
------ ---- ----- -------- -------- ----------- ---------
srv0042 C:\ System 126,45 114,12 90,25 4096
srv0042 E:\ Data1 97,62 96,33 98,67 4096
srv0042 F:\ DATA2 29,2 29,2 100 16384
 
 
.EXAMPLE
Get-DbaDiskSpace -ComputerName srv0042 -Unit MB | Format-Table -AutoSize
 
Get diskspace for the server srv0042, display in MB
 
Server Name Label SizeInMB FreeInMB PercentFree BlockSize
------ ---- ----- -------- -------- ----------- ---------
srv0042 C:\ System 129481 116856,11 90,25 4096
srv0042 E:\ Data1 99968 98637,56 98,67 4096
srv0042 F:\ DATA2 29901 29900,92 100 16384
 
 
.EXAMPLE
Get-DbaDiskSpace -ComputerName srv0042, srv0007 -Unit TB | Format-Table -AutoSize
 
Get diskspace from two servers, display in TB
 
Server Name Label SizeInTB FreeInTB PercentFree BlockSize
------ ---- ----- -------- -------- ----------- ---------
srv0042 C:\ System 0,12 0,11 90,25 4096
srv0042 E:\ Data1 0,1 0,09 98,67 4096
srv0042 F:\ DATA2 0,03 0,03 100 16384
srv0007 C:\ System 0,07 0,01 11,92 4096
 
 
.EXAMPLE
Get-DbaDiskSpace -ComputerName srv0042 -Detailed | Format-Table -AutoSize
 
Get detailed diskspace information
 
Server Name Label SizeInGB FreeInGB PercentFree BlockSize IsSqlDisk FileSystem DriveType
------ ---- ----- -------- -------- ----------- --------- --------- ---------- ---------
srv0042 C:\ System 126,45 114,12 90,25 4096 False NTFS Local Disk
srv0042 E:\ Data1 97,62 96,33 98,67 4096 False ReFS Local Disk
srv0042 F:\ DATA2 29,2 29,2 100 16384 False FAT32 Local Disk
srv0042 \\?\Volume{7a31be94-b842-42f5-af71-e0464a1a9803}\ Recovery 0,44 0,13 30,01 4096 False NTFS Local Disk
srv0042 D:\ 0 0 0 False Compact Disk
 
#>

[CmdletBinding(SupportsShouldProcess = $true)]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias('ServerInstance', 'SqlInstance', 'SqlServer')]
[String[]]$ComputerName,
[ValidateSet('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB')]
[String]$Unit = 'GB',
[Switch]$CheckForSql,
[Object]$SqlCredential,
[Switch]$Detailed,
[Switch]$CheckFragmentation,
[Switch]$AllDrives
)

BEGIN
{
Function Get-AllDiskSpace
{
$alldisks = @()
$driveTypeName = @{
'0' = 'Unknown';
'1' = 'No Root Directory';
'2' = 'Removable Disk';
'3' = 'Local Disk';
'4' = 'Network Drive';
'5' = 'Compact Disk';
'6' = 'RAM Disk'
}

if ($Detailed -or $AllDrives)
{
$driveTypes = 0 .. 6
}
else
{
$driveTypes = 2, 3
}

if ($Unit -eq 'Bytes')
{
$measure = '1'
}
else
{
$measure = "1$unit"
}

try
{
if ($CheckFragmentation)
{
## $disks = Get-CimInstance -Class Win32_Volume -Namespace 'root\CIMV2' -ComputerName $ipaddr | Where-Object DriveType -in (2,3)
$disks = Get-WmiObject -Class Win32_Volume -Namespace 'root\CIMV2' -ComputerName $ipaddr | Where-Object DriveType -in ($driveTypes)
$disks = $disks | Select-Object SystemName, Name, DriveType, FileSystem, FreeSpace, Capacity, Label, BlockSize, @{ Name = 'FilePercentFragmentation'; Expression = { "$($_.defraganalysis().defraganalysis.FilePercentFragmentation)" } }
}
else
{
$query = "Select SystemName, Name, DriveType, FileSystem, FreeSpace, Capacity, Label, BlockSize from Win32_Volume where DriveType = 2 or DriveType = 3"
## $disks = Get-CimInstance -ComputerName $ipaddr -Query $query | Sort-Object -Property Name
$disks = Get-WmiObject -Class Win32_Volume -Namespace 'root\CIMV2' -ComputerName $ipaddr | Where-Object DriveType -in ($driveTypes)
$disks = $disks | Select-Object SystemName, Name, DriveType, FileSystem, FreeSpace, Capacity, Label, BlockSize

}
}
catch
{
Write-Warning "Cannot connect to WMI on $server"
return
}

if ($CheckForSql -or $Detailed)
{
$sqlservers = @()
$FailedToGetServiceInformation = $false
try
{
$sqlservices = Get-Service -ComputerName $ipaddr | Where-Object { $_.DisplayName -like 'SQL Server (*' }
}
catch
{
Write-Verbose "Cannot retrieve service information from $server using Get-Service. Trying WMI"
try
{
$sqlservices = Get-WmiObject Win32_Service -ComputerName $ipaddr | Where-Object { $_.DisplayName -like 'SQL Server (*' }
}
catch
{
Write-Warning "Cannot retrieve service information from $server using Get-Service or WMI."
$FailedToGetServiceInformation = $true
}
}

foreach ($service in $sqlservices)
{
$instance = $service.DisplayName.Replace('SQL Server (', '')
$instance = $instance.TrimEnd(')')

if ($instance -eq 'MSSQLSERVER')
{
$sqlservers += $server
Write-Verbose "Instance resolved as $server"
}
else
{
$sqlservers += "$server\$instance"
Write-Verbose "Instance resolved as $server\$instance"
}
}
}

foreach ($disk in $disks)
{
$diskname = $disk.Name
if ($CheckForSql -or $Detailed)
{
$sqldisk = $false
if ($FailedToGetServiceInformation)
{
$sqldisk = 'unknown'
}
else
{
foreach ($sqlserver in $sqlservers)
{
try
{
Write-Verbose "Connecting to $SqlServer SQL instance"
$smoserver = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
$sql = "Select count(*) as Count from sys.master_files where physical_name like '$diskname%'"
$sqlcount = $smoserver.Databases['master'].ExecuteWithResults($sql).Tables[0].Count
if ($sqlcount -gt 0)
{
$sqldisk = $true
break
}
}
catch
{
Write-Warning "Can't connect to $server ($sqlserver)"
continue
}
}
}
}

if (!$diskname.StartsWith('\\') -or $Detailed)
{
if ($disk.capacity -eq 0 -or [string]::IsNullOrEmpty($disk.capacity))
{
$total = 0
$free = 0
$percentfree = 0
}
else
{
$total = [math]::round($disk.Capacity / $measure, 2)
$free = [math]::round($disk.Freespace/$measure, 2)
$percentfree = [math]::round(($disk.Freespace / $disk.Capacity) * 100, 2)
}

$diskinfo = [PSCustomObject]@{
Server = $server
Name = $diskname
Label = $disk.Label
"SizeIn$unit" = $total
"FreeIn$unit" = $free
PercentFree = $percentfree
BlockSize = $disk.BlockSize
}

if ($CheckForSql -or $Detailed)
{
Add-Member -InputObject $diskinfo -MemberType Noteproperty IsSqlDisk -value $sqldisk
}

if ($Detailed)
{
Add-Member -InputObject $diskinfo -MemberType Noteproperty FileSystem -value $disk.FileSystem
Add-Member -InputObject $diskinfo -MemberType Noteproperty DriveType -value $driveTypeName["$($disk.DriveType)"]
}

if ($CheckFragmentation)
{
Add-Member -InputObject $diskinfo -MemberType Noteproperty PercentFragmented -value $disk.FilePercentFragmentation
}
$alldisks += $diskinfo
}
}
return $alldisks
}


$processed = New-Object System.Collections.ArrayList
}

PROCESS
{

foreach ($server in $ComputerName)
{
if ($server -match '\\')
{
$server = $server.Split('\')[0]
}

if ($server -notin $processed)
{
$null = $processed.Add($server)
Write-Verbose "Connecting to $server"
}
else
{
continue
}

Write-Verbose "Resolving computername"
try
{
$ipaddr = ((Test-Connection -ComputerName $ComputerName -Count 1 -ErrorAction SilentlyContinue).Ipv4Address).IPAddressToString
}
catch
{
Write-Warning "Can't resolve $server address"
return
}

$data = Get-AllDiskSpace $server

if ($data.Count -gt 1)
{
$data.GetEnumerator() | ForEach-Object { $_ }
}
else
{
$data
}
}
}
}