【问题标题】:powershell stop process using 45% cpu使用 45% cpu 的 powershell 停止进程
【发布时间】:2014-05-15 19:01:41
【问题描述】:

有谁知道如何使用 powershell 停止在 CPU 使用率约为 45% 的情况下挂起的进程。

我不得不手动执行此操作,并希望:

首先开发一个可以通过名称找到进程的脚本(在这种情况下,它是一个名为 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

【问题讨论】:

  • 你能粘贴你的脚本吗?
  • 我已对您的脚本进行了格式化,使其更易于阅读。感谢您添加。希望@Bonifacio2,或者无论是谁,你都会回来并删除你的反对票,因为你按照要求做了。该脚本(尽管在您的最后一个 If/Than/Else 语句中缺少诸如 { } 之类的内容)甚至不会尝试停止该过程。你做了什么来真正尝试停止这个过程?你遇到了什么错误?
  • 不是我,@TheMadTechnician。 :)
  • 谢谢!一旦我可以清除此问题,我就会调整以删除该过程并通过电子邮件向我发送通知,说明该过程已停止。我在当前脚本中遇到此错误。 > if ( 条件 ) 后缺少语句块。在 C:\dfileman.ps1:17 char:1 +
  • 所以 If 语句是这样写的:If(test){code to run if test passes} 现在你缺少了 { }。此外,您可以从脚本末尾删除 Else 和 Exit。看看有没有帮助。

标签: powershell process


【解决方案1】:

查看性能监视器,而不是自定义脚本。它有built-in functionality 用于在计数器在特定值上停留足够长的时间时采取行动。

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

【讨论】:

    【解决方案2】:

    所以一个 If 语句是这样写的: If(test){code to run if test pass} 现在你缺少 { } 。更正代码:

    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"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多