-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-DiskDriveUsage.ps1
196 lines (149 loc) · 5.05 KB
/
Get-DiskDriveUsage.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
<#PSScriptInfo
.VERSION 1.3
.GUID b2e27333-e8a4-4e0a-87f9-6425df6f8012
.AUTHOR Joshua Melo
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI https://github.com/mallockey/Get-DiskDriveInfo
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
<#
.DESCRIPTION
This script gets hard drive usage via WMI or CIM instances
#>
[CmdletBinding(DefaultParameterSetName = 'Computers')]
Param(
[Parameter(ParameterSetName = "AD")][ValidateNotNullOrEmpty()][String]$SpecifyOU,
[Parameter(ParameterSetName = "AD")][Switch]$WorkstationsOnly,
[Parameter(ParameterSetName = "AD")][Switch]$ServersOnly,
[Parameter(ParameterSetName = "InputFile")][ValidateNotNullOrEmpty()][String]$InputFile,
[parameter(ParameterSetName = "Computers",ValueFromPipeline)][Array]$ComputerName = $env:COMPUTERNAME,
[String]$OutputFile,
[Switch]$UseCIM
)
$ErrorActionPreference = 'Stop'
if($SpecifyOU -or $WorkstationsOnly -or $ServersOnly){
try{
Import-Module ActiveDirectory
}catch{
Write-Warning "Unable to import the ActiveDirectory Module"
Write-Warning "Make sure you are running this from a domain controller"
Exit
}
if($SpecifyOU){
try{
$allComputers = (Get-ADComputer -Filter {Enabled -eq $True} -SearchBase $SpecifyOU).Name
}
catch{
Write-Warning "OU not correct please verify OU and rerun."
Exit
}
}elseif($WorkstationsOnly){
$allComputers = (Get-ADComputer -Filter {OperatingSystem -NotLike '*server*' -and Enabled -eq $True}).Name
}elseif($ServersOnly){
$allComputers = (Get-ADComputer -Filter {OperatingSystem -Like '*server*' -and Enabled -eq $True}).Name
}else{
$allComputers = (Get-ADComputer -Filter {Enabled -eq $True}).Name
}
}elseif($inputFile){
try{
$allComputers = Get-Content $InputFile
}catch{
Write-Warning "$inputFile is not a valid list of workstations."
Exit
}
}elseif($computerName){
$allComputers = $ComputerName
}
$resultsArray = @()
$objProp = [Ordered]@{
ComputerName = $null
DriveLetter = $null
DriveLabel = $null
FreeSpace = $null
TotalSpace = $null
PercentFree = $null
Status = $null
Online = $null
}
function get-DiskStats {
Param(
$computerName
)
if($UseCIM){
$cmdLetToTry = "Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computerName"
}else{
$cmdLetToTry = "Get-WMIObject -Class Win32_LogicalDisk -ComputerName $computerName"
}
try{
$allDriveInfo = Invoke-Expression -Command $cmdLetToTry
}catch{
$allDriveInfo = $null
}
return $allDriveInfo
}
$counter = 0
foreach($currentComputer in $allComputers){
[Int]$currentPercent = ($counter / $allComputers.length) * 100
Write-Progress -Activity "Getting disk info from $($currentComputer)" -CurrentOperation "$currentPercent% completed"
$computerObj = New-Object -TypeName PSObject -Prop $objProp
$computerObj.ComputerName = $currentComputer
if(Test-Connection $currentComputer -Quiet -Count 1){
$computerObj.Online = "Online"
$allDriveInfo = get-DiskStats -computerName $currentComputer
if($allDriveInfo){
foreach($drive in $allDriveInfo){
if($drive.DriveType -ne 3){
continue
}
$computerObj = New-Object -TypeName PSObject -Prop $objProp
$freeSpace = ([int]($drive.FreeSpace / 1gb))
$freeSpaceString = $freeSpace.ToString() + "GBs"
$totalSpace = ([int]($drive.Size / 1gb))
$totalSpaceString = $totalSpace.ToString() + "GBs"
$percentFree = ([Int](($freeSpace / $totalSpace) * 100))
$percentFreeString = $percentFree.ToString() + "%"
$computerObj.FreeSpace = $freeSpaceString
$computerObj.ComputerName = $currentComputer
$computerObj.Online = "Online"
$computerObj.DriveLetter = $drive.DeviceID
$computerObj.DriveLabel = $drive.VolumeName
$computerObj.TotalSpace = $totalSpaceString
$computerObj.PercentFree = $percentFreeString
$computerObj.Status = "OK"
if ($percentFree -lt 10){
$computerObj.Status = "LOW"
}
$resultsArray += $computerObj
}
}else{
$computerObj.Online = "Online"
if($UseCIM){
$computerObj.Status = "Unable to get disk info via CIM"
}else{
$computerObj.Status = "Unable to get disk info via WMI"
}
$computerObj.Online = "Online"
$resultsArray += $computerObj
}
}else{
$computerObj.Online = "Offline"
$resultsArray += $computerObj
}
$counter++
}
$resultsArray
if($OutputFile){
$checkIfCSV = $OutputFile.Substring($OutputFile.Length -3)
if($checkIfCSV -ne "csv"){
$OutputFile = $OutputFile.Replace($checkIfCSV,"csv")
}
$resultsArray | Sort-Object Online -Descending | Export-Csv $outputFile -NoTypeInformation
}