-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathGet-CustomCounters.ps1
357 lines (307 loc) · 11.3 KB
/
Get-CustomCounters.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
<#
.SYNOPSIS
Collect pre-defined performance counters from a local or remote system.
.DESCRIPTION
Collect pre-defined performance counters from a local or remote system.
Pre-Defined counters have been defined and stored in variables, defined in the BEGIN block of this script/function.
The default counter set is 'Summary' and the default Computer is localhost.
Optionally, you can supply a number of minutes you want to collect counters using the -RunTimeInMinutes parameter, which will collect
the counters every 30 seconds, until the number of minutes that you provide expires. A .CSV file is generated and the data is appended
to it on each 30 second iteration.
.PARAMETER ComputerName
Name of remote system (FQDN preferred)
.PARAMETER RunTimeInMinutes
Number of minutes to query for counters
.PARAMETER ReportPath
Optionally supply a new path for the .CSV export generated as part of a time period scan
.PARAMETER Counters
Selecte the desired counter set. Valid options are:
- Memory
- Processor
- LogicalDisk
- PhysicalDisk
- Network
- Processes
- Summary
.NOTES
20140925 K. Kirkpatrick Created
GitHub: https://github.com/vScripter
Twitter: @vScripter
Email: [email protected]
[-------------------------------------DISCLAIMER-------------------------------------]
All script are provided as-is with no implicit
warranty or support. It's always considered a best practice
to test scripts in a DEV/TEST environment, before running them
in production. In other words, I will not be held accountable
if one of my scripts is responsible for an RGE (Resume Generating Event).
If you have questions or issues, please reach out/report them on
my GitHub page. Thanks for your support!
[-------------------------------------DISCLAIMER-------------------------------------]
#>
[cmdletbinding(PositionalBinding = $true)]
param (
[parameter(Mandatory = $false, Position = 0)]
[validatescript({ Test-Connection -ComputerName $_ -Count 2 -Quiet })]
[string]$ComputerName = "$(hostname)",
[parameter(Mandatory = $false, ParameterSetName = "Collection")]
[int]$RunTimeInMinutes,
[parameter(Mandatory = $false, ParameterSetName = "Collection")]
[string]$ReportPath = 'C:\DiskIOStats.csv',
[parameter(Mandatory = $false)]
[ValidateSet("Memory", "Processor", "LogicalDisk", "PhysicalDisk", "Network", "Processes", "Summary")]
[string]$Counters = "Summary"
)
BEGIN
{
#Requires -Version 3
Set-StrictMode -Version Latest
# Setup preferences and variables
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
$windowTitle = $host.ui.RawUI.WindowTitle
$TimeStart = Get-Date
$TimeEnd = $timeStart.addminutes($RunTimeInMinutes)
Function logstamp
{ # function to capture the current time in a custom format
$now = Get-Date -Format T
Write-Output $now
}# function logstamp
function Get-CounterStats
{
[cmdletbinding()]
param (
[parameter()]
[string]$System,
[parameter()]
[string[]]$PerfCounters
)
BEGIN
{
# Function that queries counters, based on what is supplied to the function
if (!($PerfCounters))
{ # Write a warning if for some reason no value is passed to the $PerfCounters variable
Write-Warning -Message "A counter was not specified; please specify a counter switch parameter and try again."
return
}
}
PROCESS
{
try
{
# define the results array
$Result = @()
# query for counters and store the results
$ioQuery = (Get-Counter -ComputerName $System -Counter $PerfCounters -ErrorAction 'SilentlyContinue').countersamples
# Cycle through each counter and add assign the results to a custom object
foreach ($sample in $ioQuery)
{
# clear the $col array on each cycle through the loop
$col = @()
# define custom object and properties
$ioObj = [PSCustomObject] @{
TimeStamp = "$(logstamp)"
Counter = $sample.path
Instance = $sample.InstanceName
Value = $sample.cookedvalue
}# end $ioObj
# assign the object to a temporary array
$col += $ioObj
# assign the data in the temporary array to the final results array
$result += $col
}# end foreach
} catch
{
# write any errors to the console
Write-Warning -Message " Error in Get-CounterStats function: $_"
}# try/catch
}# PROCESS
END
{
# sort the order of the results
$result
}# END
}# Get-CounterStats
#region Counter Variables
$summaryCounters = @(
'\Network Interface(*)\Bytes Total/sec',
'\Processor(_total)\% Processor Time',
'\Memory\% Committed Bytes In Use',
'\memory\cache faults/sec',
'\PhysicalDisk(_total)\% Disk Time',
'\physicaldisk(_total)\current disk queue length'
)
$ramCounters = @(
'\Memory\% Committed Bytes In Use',
'\Memory\Available MBytes',
'\memory\cache faults/sec'
)
$processCounters = @(
'\Process(*)\IO Read Operations/sec',
'\Process(*)\IO Write Operations/sec',
'\Process(*)\IO Data Operations/sec',
'\Process(*)\IO Other Operations/sec'
)
$processorCounters = @(
'\Processor(*)\% Processor Time',
'\Processor Performance(*)\% of Maximum Frequency'
)
$diskLogicalCounters = @(
'\LogicalDisk(*)\Disk Transfers/sec',
'\LogicalDisk(*)\Disk Reads/sec',
'\LogicalDisk(*)\Disk Writes/sec',
'\LogicalDisk(*)\% Disk Read Time',
'\LogicalDisk(*)\% Disk Write Time',
'\LogicalDisk(*)\Disk Bytes/sec',
'\LogicalDisk(*)\Disk Read Bytes/sec',
'\LogicalDisk(*)\Disk Write Bytes/sec',
'\LogicalDisk(*)\Current Disk Queue Length',
'\LogicalDisk(*)\Avg. Disk Read Queue Length',
'\LogicalDisk(*)\Avg. Disk Write Queue Length',
'\LogicalDisk(*)\% Disk Time',
'\LogicalDisk(*)\% Disk Read Time',
'\LogicalDisk(*)\% Disk Write Time',
'\LogicalDisk(*)\Split IO/Sec',
'\LogicalDisk(*)\Disk Bytes/sec',
'\LogicalDisk(*)\Avg. Disk Bytes/Read',
'\LogicalDisk(*)\Avg. Disk Bytes/Write',
'\LogicalDisk(*)\Disk Read Bytes/sec',
'\LogicalDisk(*)\Disk Write Bytes/sec'
)
$diskPhysicalCounters = @(
'\PhysicalDisk(*)\Disk Transfers/sec',
'\PhysicalDisk(*)\Disk Reads/sec',
'\PhysicalDisk(*)\Disk Writes/sec',
'\PhysicalDisk(*)\% Disk Read Time',
'\PhysicalDisk(*)\% Disk Write Time',
'\PhysicalDisk(*)\Disk Bytes/sec',
'\PhysicalDisk(*)\Disk Read Bytes/sec',
'\PhysicalDisk(*)\Disk Write Bytes/sec',
'\PhysicalDisk(*)\Current Disk Queue Length',
'\PhysicalDisk(*)\Avg. Disk Read Queue Length',
'\PhysicalDisk(*)\Avg. Disk Write Queue Length',
'\PhysicalDisk(*)\% Disk Time',
'\PhysicalDisk(*)\% Disk Read Time',
'\PhysicalDisk(*)\% Disk Write Time',
'\PhysicalDisk(*)\Split IO/Sec',
'\PhysicalDisk(*)\Disk Bytes/sec',
'\PhysicalDisk(*)\Disk Read Bytes/sec',
'\PhysicalDisk(*)\Disk Write Bytes/sec',
'\PhysicalDisk(*)\Avg. Disk Bytes/Read',
'\PhysicalDisk(*)\Avg. Disk Bytes/Write'
)
$networkCounters = @(
'\Network Interface(*)\Bytes Total/sec',
'\Network Interface(*)\Bytes Received/sec',
'\Network Interface(*)\Packets Received Discarded',
'\Network Interface(*)\Packets Received Errors',
'\Network Interface(*)\Bytes Sent/sec',
'\Network Interface(*)\Packets Outbound Discarded',
'\Network Interface(*)\Packets Outbound Errors',
'\Network Interface(*)\Output Queue Length'
)
#endregion Counter Variables
}# BEGIN
PROCESS
{
try
{
# if a run time was specified, execute this script block
if ($RunTimeInMinutes)
{
# set the console window title message
$host.ui.RawUI.WindowTitle = "Gathering Counter Statistics. Do Not Close Window."
# if the report exists, delete it so no new data is appended to a sheet with old data
if (Test-Path -Path $ReportPath)
{
Write-Warning -Message "Export file exists; removing now..."
Remove-Item $ReportPath -Force | Out-Null
}# if
Write-Verbose -Message "Collecting counters from $ComputerName..."
Write-Verbose -Message "The collection will end at $timeEnd"
Do
{
# grab the start time that the loop was entered
$TimeNow = Get-Date
if ($TimeEnd -gt $TimeStart)
{
# switch block that reads the string value assigned to the -Counters param and runs the associated counters collection
switch ($Counters)
{
"Memory" {
Get-CounterStats -System $ComputerName -PerfCounters $ramCounters |
Export-Csv $ReportPath -Append -NoTypeInformation
}
"Processor" {
Get-CounterStats -System $ComputerName -PerfCounters $processorCounters |
Export-Csv $ReportPath -Append -NoTypeInformation
}
"LogicalDisk" {
Get-CounterStats -System $ComputerName -PerfCounters $diskLogicalCounters |
Where-Object { $_.Instance -notlike 'harddiskvolume*' } |
Export-Csv $ReportPath -Append -NoTypeInformation
}
"PhysicalDisk" {
Get-CounterStats -System $ComputerName -PerfCounters $diskPhysicalCounters |
Export-Csv $ReportPath -Append -NoTypeInformation
}
"Network" { # do some filtering on network devices I don't care about
Get-CounterStats -System $ComputerName -PerfCounters $networkCounters |
Where-Object { $_.Instance -notlike 'isatap*' -and $_.Instance -notlike '*virtual wifi*' -and $_.Instance -notlike 'teredo*' } |
Export-Csv $ReportPath -Append -NoTypeInformation
}
"Processes" {
Get-CounterStats -System $ComputerName -PerfCounters $processCounters |
Export-Csv $ReportPath -Append -NoTypeInformation
}
"Summary" { # do some filtering on network devices I don't care about
Get-CounterStats -System $ComputerName -PerfCounters $summaryCounters |
Where-Object { $_.Instance -notlike 'isatap*' -and $_.Instance -notlike '*virtual wifi*' -and $_.Instance -notlike 'teredo*' } |
Export-Csv $ReportPath -Append -NoTypeInformation
}
}# switch
}# if
# wait 30 seconds before looping through the funcion, again
Start-Sleep -Seconds 30
}# Do
# when the current time is past the set end time, stop the collection
until ($TimeNow -ge $TimeEnd)
} else
{
# if no run time value was supplied, simply output to the console
switch ($Counters)
{
"Memory" {
Get-CounterStats -System $ComputerName -PerfCounters $ramCounters
}
"Processor" {
Get-CounterStats -System $ComputerName -PerfCounters $processorCounters
}
"LogicalDisk" {
Get-CounterStats -System $ComputerName -PerfCounters $diskLogicalCounters |
Where-Object { $_.Instance -notlike 'harddiskvolume*' }
}
"PhysicalDisk" {
Get-CounterStats -System $ComputerName -PerfCounters $diskPhysicalCounters
}
"Network" { # do some filtering on network devices I don't care about
Get-CounterStats -System $ComputerName -PerfCounters $networkCounters |
Where-Object { $_.Instance -notlike 'isatap*' -and $_.Instance -notlike '*virtual wifi*' -and $_.Instance -notlike 'teredo*' }
}
"Processes" {
Get-CounterStats -System $ComputerName -PerfCounters $processCounters
}
"Summary" { # do some filtering on network devices I don't care about
Get-CounterStats -System $ComputerName -PerfCounters $summaryCounters |
Where-Object { $_.Instance -notlike 'isatap*' -and $_.Instance -notlike '*virtual wifi*' -and $_.Instance -notlike 'teredo*' }
}
}# switch
}# if/else
} catch
{
Write-Warning -Message "$_"
}# try/catch
}# PROCESS
END
{
Write-Verbose -Message "Script Execution Complete..."
$host.ui.RawUI.WindowTitle = $windowTitle
}# END