-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-SysDriveSpace.ps1
155 lines (122 loc) · 4.41 KB
/
Get-SysDriveSpace.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
function Get-SysDriveSpace {
<#
.SYNOPSIS
Report free space on C: drive
.DESCRIPTION
Report System drive freespace to identify ComputerName <30%
.PARAMETER ComputerName
Target computer name.
Default: "$env:COMPUTERNAME"
.PARAMETER Credential
Target computer credential.
.PARAMETER ShowProgress
Display the progress meter.
Default: "$false"
.PARAMETER Timeout
Cim Instance timeout.
Default: '30'
.PARAMETER Protocol
Cim Session protocol. Dcom or Wsman
.EXAMPLE
Get-SysDriveSpace.ps1 -ComputerName Computer1
.NOTES
Author: Andrew Bounds
.LINK
https://github.com/p0rkjello/PowerShell
#>
#Requires -Version 3
[CmdletBinding()]
Param(
[ValidateNotNullOrEmpty()]
[string[]]
$ComputerName = $env:COMPUTERNAME,
[Alias("RunAs")]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[switch]
$ShowProgress = $false,
[int]
$Timeout = 30,
[ValidateSet('Dcom', 'Wsman')]
[string]
$Protocol
)
begin {
$Computer = $null
[int]$CountTotal = $ComputerName.Count
[int]$Count = 0
}
process {
foreach ($Computer in $ComputerName) {
$Computer = $Computer.ToLower()
if ($ShowProgress.IsPresent) {
if ($CountTotal -gt 0) {
$Count++
$Status = '{0} / {1} | {2}' -f $Count, $CountTotal, $Computer
$Progress = @{
Activity = 'Collecting Disk Space'
Id = '1'
Status = $Status
PercentComplete = (($Count / $CountTotal) * 100)
}
Write-Progress @Progress
}
}
# Build CimSession
$CimSessionSplat = @{
ComputerName = $Computer
}
# Build CimSessionOptions
if ($PSBoundParameters.ContainsKey('Protocol')) {
$Opt = New-CimSessionOption -Protocol $Protocol
$CimSessionSplat.SessionOption = $Opt
}
else {
if (-not (Test-WSMan @CimSessionSplat -ErrorAction SilentlyContinue)) {
$Opt = New-CimSessionOption -Protocol Dcom
$CimSessionSplat.SessionOption = $opt
}
}
if ($PSBoundParameters.ContainsKey('Credential')) {
$CimSessionSplat.Credential = $Credential
}
$CimSessionSplat.ErrorAction = 'Stop'
try {
$CS = New-CimSession @CimSessionSplat
}
catch {
$Exception = $_.Exception.Message
Write-Warning "[$Computer] $Exception"
}
# Continue on loop if not able to create a CimSession
if (-not ($CS)) { continue }
# Build CimInstance
$CimInstanceSplat = @{
ClassName = "Win32_LogicalDisk"
Filter = "DeviceID = 'C:'"
OperationTimeoutSec = "$Timeout"
ErrorAction = "Stop"
}
try {
$DriveInfo = Get-CimInstance @CimInstanceSplat -CimSession $CS
}
catch {
$Exception = $_.exception.message
Write-Warning "[$Computer] $Exception"
}
$DiskSize = [math]::round($DriveInfo.Size / 1GB)
$DiskFree = [math]::round($DriveInfo.FreeSpace / 1GB)
if ($DiskSize -eq 0) { $Percent = 0 } else {
$Percent = ($DiskFree / $DiskSize).ToString("P")
}
[pscustomobject]@{
'Computer' = $DriveInfo.PSComputerName
'Drive' = $DriveInfo.DeviceID
'Size (GB)' = $DiskSize
'FreeSpace (GB)' = $DiskFree
'Percentage Free' = $Percent
}
}
}
}