-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-DriveStat.ps1
142 lines (129 loc) · 5.55 KB
/
Get-DriveStat.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
function Get-DriveStat {
<#
.SYNOPSIS
To get statistics on drives on a particular server or servers.
.DESCRIPTION
To get statistics on drives on a server including Size, FreeSpace, and FreePct. Command line
parameter allows for capacity statistics in Bytes, KB, MB, and GB
.EXAMPLE
Get-DriveStat
ComputerName DeviceID VolumeName SizeGB FreeSpaceGB FreePct
------------ -------- ---------- ------ ----------- -------
localhost C: Windows 237.14 19.56 8.25
.EXAMPLE
Get-DriveStat -Capacity MB
ComputerName DeviceID VolumeName SizeMB FreeSpaceMB FreePct
------------ -------- ---------- ------ ----------- -------
localhost C: Windows 242831.45 20026.65 8.25
.EXAMPLE
Get-DriveStat -Verbose
VERBOSE: Starting Get-DriveStat
VERBOSE: Capacity will be expressed in [GB]
VERBOSE: Processing MDA-102192
ComputerName : localhost
DeviceID : C:
VolumeName : Windows
SizeGB : 237.14
FreeSpaceGB : 19.56
FreePct : 8.25
VERBOSE: Ending Get-DriveStat
.NOTES
Put in error checking around Get-CimInstance to handle Kerberos errors.
Changed Select-Object statements to New-Object statements and specified hidden property PSTypeName
to define typename (which can be used for formatting purposes)
#>
# todo - add Credential
#region Parameters
[CmdletBinding()]
[OutputType('psobject')]
Param (
[Parameter(Position = 0)]
[ValidateNotNullorEmpty()]
[Alias('CN', 'Server', 'ServerName', 'PSComputerName', 'SystemName')]
[string[]] $ComputerName = $env:COMPUTERNAME,
[Parameter(Position = 1)]
[ValidateSet('Bytes', 'KB', 'MB', 'GB')]
[string] $Capacity = 'GB'
)
#endregion Parameters
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
Write-Verbose -Message "Capacity will be expressed in [$Capacity]"
$Query = "Select * from Win32_LogicalDisk where DriveType='3' and FileSystem='NTFS'"
$CimOption = New-CimSessionOption -EncodePortInServicePrincipalName
}
process {
foreach ($CurName in $ComputerName) {
Write-Verbose -Message "Processing $CurName"
try {
$DriveStat = Get-CimInstance -Query $Query -ComputerName $CurName
} catch {
try {
$CimSession = New-CimSession -ComputerName $CurName -SessionOption $CimOption
$DriveStat = Get-CimInstance -Query $Query -CimSession -CimSession
$CimSession.Close()
$CimSession.Dispose()
} catch {
Write-Error -Message "Could not connect to [$CurName]"
}
}
switch ($Capacity) {
'Bytes' {
$DriveStat | ForEach-Object {
New-Object -Typename psobject -Property ([ordered] @{
PSTypeName = 'PFDriveStatBytes'
ComputerName = $_.SystemName
DeviceID = $_.DeviceID
VolumeName = $_.VolumeName
Size = $_.Size
FreeSpace = $_.FreeSpace
FreePct = [double] ('{0:f2}' -f ($_.FreeSpace / $_.Size * 100))
})
}
}
'KB' {
$DriveStat | ForEach-Object {
New-Object -Typename psobject -Property ([ordered] @{
PSTypeName = 'PFDriveStatKB'
ComputerName = $_.SystemName
DeviceID = $_.DeviceID
VolumeName = $_.VolumeName
SizeKB = [double] ('{0:f2}' -f ($_.Size / 1KB))
FreeSpaceKB = [double] ('{0:f2}' -f ($_.FreeSpace / 1KB))
FreePct = [double] ('{0:f2}' -f ($_.FreeSpace / $_.Size * 100))
})
}
}
'MB' {
$DriveStat | ForEach-Object {
New-Object -Typename psobject -Property ([ordered] @{
PSTypeName = 'PFDriveStatMB'
ComputerName = $_.SystemName
DeviceID = $_.DeviceID
VolumeName = $_.VolumeName
SizeMB = [double] ('{0:f2}' -f ($_.Size / 1MB))
FreeSpaceMB = [double] ('{0:f2}' -f ($_.FreeSpace / 1MB))
FreePct = [double] ('{0:f2}' -f ($_.FreeSpace / $_.Size * 100))
})
}
}
'GB' {
$DriveStat | ForEach-Object {
New-Object -Typename psobject -Property ([ordered] @{
PSTypeName = 'PFDriveStatGB'
ComputerName = $_.SystemName
DeviceID = $_.DeviceID
VolumeName = $_.VolumeName
SizeGB = [double] ('{0:f2}' -f ($_.Size / 1GB))
FreeSpaceGB = [double] ('{0:f2}' -f ($_.FreeSpace / 1GB))
FreePct = [double] ('{0:f2}' -f ($_.FreeSpace / $_.Size * 100))
})
}
}
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}