powershell使用45%cpu停止进程

时间:2014-05-15 19:01:41

标签: powershell process

有没有人知道如何使用PowerShell停止占用大约45%CPU使用率的进程。

我不得不手动做很多事情,并希望:

首先开发一个可以按名称查找进程的脚本(在这种情况下,它是一个名为dfileman.exe的进程,由Windows 2003服务器上运行的应用程序使用),检查它是否卡住了等于或大于45%的CPU使用时间超过5分钟,如果符合标准则停止使用。应用程序将在下次需要时启动新进程,因此我不担心重新启动它。

其次,使用MS SCOM监视dfileman.exe进程,每当它挂起时运行上面的脚本,并在脚本运行时向我发送电子邮件。

非常感谢任何帮助。即使它只是帮我编写脚本。

到目前为止我所拥有的是:

$ProcessName = "defilman.exe"

$PSEmailServer = "smtp.company.com"


foreach ($proc in (Get-WmiObject  Win32_Processor)){
    if($proc.numberofcores -eq $null){
        $cores++
    }else{
        $cores = $cores + $proc.numberofcores
    }
} 
$cpuusage = [Math]::round(((((Get-Counter "\Process($ProcessName)\% Processor Time" -MaxSamples 2).Countersamples)[0].CookedValue)/$cores),2)

if ($cpuusage -gt 45)

Send-MailMessage -To "Me (myaddress)" -From "Me (myaddress)" -Subject "DFileMan Process Hung" -body "An instance of $ProcessName on Server has reached a CPU Percentage of $cpuusage %. Please Kill Process Immediately"

else
Exit

2 个答案:

答案 0 :(得分:1)

而不是自定义脚本,请查看性能监视器。当计数器在特定值上保持足够长时间时,它有built-in functionality来执行操作。

在Perfmon检测到应用程序使用了太多CPU后,您可以使用Powershell或其他任何方法来终止该进程。

答案 1 :(得分:0)

所以If语句是这样编写的:如果(测试){代码在测试通过时运行}现在你错过了{}。更正后的代码:

if ($cpuusage -gt 45){
    Send-MailMessage -To "Me (myaddress)" -From "Me (myaddress)" -Subject "DFileMan Process Hung" -body "An instance of $ProcessName on Server has reached a CPU Percentage of $cpuusage %. Please Kill Process Immediately"
}
相关问题