CPU阈值满足时执行ps1脚本

我正在寻找一个类似于debugging诊断收集器的function。

您可以在哪里设置性能(或任何计数器)触发器(例如50秒以上的CPU超过50%)。 一旦触发条件满足,我想运行一个PS1脚本。

有没有人做过类似的事情?

不知道“超过50秒”的方面,但你可以轮询,看你的CPU是否超过一定的限制。

只是在PowerShell中快速的草图…

# checks cpu threshold and runs script in $scriptName variable function CPUthreshold { # mandatory single variable in function for script name Param( [Parameter(Mandatory=$true)] [string]$scriptName ) # cpu percentage limit $limit = 50 # time to poll CPU limit in seconds $pollTimeSec = 60 # check limit forever! while($true){ # get the win32_processor object to get stats on the CPU $cpu = Get-WmiObject win32_processor # check if the CPU is over our limit if ($cpu.LoadPercentage -gt $limit) { # call your script here! & $scriptName } # wait again until the next poll Start-Sleep -s $pollTimeSec } } # call function with script name you want to run CPUthreshold .\Hello-World.ps1 

你可以在一个线程中运行它,或者在你感兴趣的机器上运行这个过程。