0

Checking Process Age Powershell Script

The other day I stumbled across a powershell that is useful for checking out the age of a process running on a computer, unfortunately I cannot remember where I discovered it and therefore cannot give the relevant credit to the creator.

I can here many of you say, about why is this Powershell script important and why have I placed it alongside the Veeam information?

The answer to these questions is simple, the powershell script is especially useful when using Veeam Backup & Replication and it was due to a situation I had when using Veeam Backup & Replication that allowed me to look for the script in the first place.

With Veeam Backup & Replication v6, the Management Server communicates with the Proxies and starts up the VeeamAgent.exe process when backing up or replicating.  Each task that is running, will create a process like this.  In some circumstances, a Backup job may freeze even though other jobs are running through smoothly.  I knew that if you kill the VeeamAgent.exe process, it would stop the backup job and allow you to restart it later… unfortunately I didn’t want to stop all VeeamAgent.exe processes on the proxy as that would kill the backup jobs that are working fine.  If I knew the age of the VeeamAgent.exe processes and compare the start time with the start time of the virtual machine backup that has frozen… I would then be able to kill just that process and allow the other backup tasks to continue running.

So here is the script:

[cmdletbinding()]

param(

$ComputerName=$env:COMPUTERNAME,

[parameter(Mandatory=$true)]

$ProcessName

)

$Processes = Get-WmiObject -Class Win32_Process -ComputerName $ComputerName -Filter “name=’$ProcessName'”

if($Processes) {

foreach ($process in $processes) {

$processid = $process.handle

$processcreationtime = $Process.Converttodatetime($Process.creationdate)

write-host “`nThe $ProcessName `($processid`) process creation time is $processcreationtime”

}

} else {

write-host “`nNo Process found with name $ProcessName”

}

write-host “”

 

This script should be saved in a .ps1 file and stored somewhere on your network.  To run the script, start up Powershell.  I find it easier to browse to the folder where the script is located and execute the following command:

./nameofscript.ps1 -ComputerName NameofComputer -ProcessName NameofProcess

So for in my environment, I actually have it available on the server that is the Veeam Proxy and therefore I start it as the following:

./get-processage.ps1 -ProcessName VeeamAgent.exe

 

At this poing you will see something similar to below:

At this point you compare the date and time of the processes with the start time of the frozen virtual machine backup, you can then start up a command prompt on the Proxy server and run the following command to kill the process:

taskkill /PID processnumber /F

The process number is the item in brackets and therefore in our example above, if we wanted to kill off the oldest process, we would run the following command:

taskkill /PID 948 /F

Seconds later the backup task will fail, at which point you can investigate the reason for it freezing and restart your backup task again.

Leave a Reply

Your email address will not be published. Required fields are marked *