Solved

Powershell script to kill process based on processor utilization

Posted on 2012-03-13
3
1,418 Views
Last Modified: 2012-04-12
I'm using this command to kill winword.exe processes on a server we have that uses MS Word for a document conversion process.  Because of a bug in this process, the winword.exe process is being left open when the conversion should close it after it completes.  Until a bug fix is released for the third-party app involved, I would like to devise a method to kill these processes.  I know I can use this power shell command:

stop-process -Name winword* -force

it works just fine.  However, I'd like to automate it if possible.  I was wonder if it would be possible to put this in a PowerShell script that will do the following:

1. run the stop-process command for winword.exe if a certain total processor threshold is crossed...let's say 75%.
2. close all winword.exe processes older than 2 minutes.
3. send an alert via email when script is run

I'm still learning pshell, and I know this is a little fancier than what I'm used to doing, so i was wondering if anyone had any ideas on how to accomplish these goals.

Thanks!
0
Comment
Question by:patriots
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Expert Comment

by:Brent Challis
ID: 37718009
How do you want to define utilisation?  Is it per processor or per core?  Are you concerned with multi processor (as opposed to multi core) systems?
0
 
LVL 8

Accepted Solution

by:
Brent Challis earned 400 total points
ID: 37719075
Here is a function that test CPU utilisation and kill processes that have been running for longer than a defined time period:

function Kill-Process
{
    [CmdletBinding()]
    param
    (
        [string]$ProcessName="WinWord",
        [int]$UtilisationTheshold=75,
        [int]$ProcessAge=2
    )
    $cpuUtilisation = (Get-WMIObject win32_processor).LoadPercentage
    Write-Verbose $pu
    if ($cpuUtilisation -ge $UtilisationTheshold)
    {
        try
        {
            $processes = Get-Process $ProcessName
            foreach ($process in $processes)
            {
                $runningTime = ((Get-Date) - $process.StartTime).TotalMinutes
                if ($runningTime -ge $ProcessAge)
                {
                    $process | Stop-Process
                }  
            }
        }
        catch
        {
            Write-Verbose $_
        }
    }
    else
    {
        Write-Verbose "Utilisation under threshold $UtilisationTheshold"
    }
    Write-Verbose "Complete."
}

Kill-Process -verbose
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 37734826
I agree with above solution, but would "improve" the code in the try block to make it more "powershellish":
Get-Process $ProcessName |
  ? { ((Get-Date)-$_.StartTime).TotalMinutes -ge $ProcessAge } |
  stop-process

Open in new window

0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to install and use the NTBackup utility that comes with Windows Server.
For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question