Skip to content

Instantly share code, notes, and snippets.

@bill-long
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bill-long/97f702f162b9f6e1e4a4 to your computer and use it in GitHub Desktop.
Save bill-long/97f702f162b9f6e1e4a4 to your computer and use it in GitHub Desktop.
$localComputerName = [Environment]::MachineName
# Monitor counter for threshold
function WaitForCounter($counter, $value, $duration)
{
"Started watching " + $counter + " to reach " + $value + " for " + $duration
$timeCounterReached = [DateTime]::MaxValue
Get-Counter -Counter $counter -Continuous |
% {
$_.CounterSamples[0].CookedValue;
if ($_.CounterSamples[0].CookedValue -ge $value)
{
$now = [DateTime]::Now
if ($timeCounterReached -gt $now)
{
"The counter reached the threshold. Waiting " + $duration + " before collection."
$timeCounterReached = $now
}
if ($timeCounterReached -le $now)
{
$currentDuration = $now - $timeCounterReached
"Counter reached desired value " + $currentDuration + " ago."
if ($currentDuration -ge $duration)
{
"Trigger hit! Starting data collection..."
wpr -start CPU -start Network
"Data collection running."
# Send-MailMessage -To user@contoso.com -From user@contoso.com -Subject "Alert on $localComputerName" -Body "CPU exceeded threshold." -SmtpServer mail.contoso.com
Start-Sleep 60
"Stopping data collection..."
wpr -stop C:\wpr.etl
"Done!"
break
}
}
}
else
{
if ($timeCounterReached -lt $now)
{
"Counter dropped below threshold."
$timeCounterReached = [DateTime]::MaxValue
}
}
}
# End of script block
}
##############################
#
# Here's where we start monitoring. Adjust as needed.
#
WaitForCounter "\Processor(_Total)\% Processor Time" 10 (new-object TimeSpan(0, 0, 0))
#
##############################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment