Link to home
Start Free TrialLog in
Avatar of Cardlytics
CardlyticsFlag for United States of America

asked on

Powershell new-timespan piping to get-process

Using the command below I get results for some processes and not others.
             new-timespan -start (get-process PROCESSNAME).starttime

For example... It works correctly when I run :
----------------------------------
new-timespan -start (get-process explorer).starttime


Days              : 0
Hours             : 3
Minutes           : 6
Seconds           : 25
Milliseconds      : 775
Ticks             : 111857754751
TotalDays         : 0.129464993924769
TotalHours        : 3.10715985419444
TotalMinutes      : 186.429591251667
TotalSeconds      : 11185.7754751
TotalMilliseconds : 11185775.4751

------------------------------------------
But it doesn't work when I run :
-----------------------------------
new-timespan -start (get-process chrome).starttime
   New-TimeSpan : Cannot bind parameter 'Start' to the target. Exception setting "Start": "Object reference not set to
   instance of an object."
   At line:1 char:20
   +new-timespan -start <<<<  (get-process chrome).starttime
       + CategoryInfo          : WriteError: (:) [New-TimeSpan], ParameterBindingException
       + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand
-----------------------------------


The problem I am trying to address is that on our vmware server we get multiple "java" processes that open and then never close when we launch the web client and they each take half a gig of RAM. After a handful of these, the memory gets used up on the server. I want to leverage the command above and say that if the process has been running longer than 24 hours then end it.


Ideas on how to approach this issue. I don't have to use the new-timespan command. It was just the solution I landed on first. I am open to whatever the correct solution is.


Original reference was : http://blogs.technet.com/b/heyscriptingguy/archive/2013/02/27/powertip-use-powershell-to-easily-find-how-long-a-process-runs.aspx
Avatar of Cardlytics
Cardlytics
Flag of United States of America image

ASKER

Update : I found a separate powershell that will help me but I'd still like to know why what I was trying didn't work. Please answer my question if possible. This is still a learning experience for me.

http://gnawgnu.blogspot.com/2009/09/powershell-script-to-kill-process-by.html
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$tocheck = "explorer"
try {
    $processes = (get-process $tocheck).StartTime
    foreach ($process in $processes)
        {
        new-timespan -start $process
        }
    }
catch {
write-host("process not found")
}

Open in new window

Thank you for the answers. It got me exactly what I needed.