Cardlytics
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,Mic rosoft.Pow erShell.Co mmands.New TimeSpanCo mmand
-------------------------- ---------
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
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,Mic
--------------------------
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$tocheck = "explorer"
try {
$processes = (get-process $tocheck).StartTime
foreach ($process in $processes)
{
new-timespan -start $process
}
}
catch {
write-host("process not found")
}
ASKER
Thank you for the answers. It got me exactly what I needed.
ASKER
http://gnawgnu.blogspot.com/2009/09/powershell-script-to-kill-process-by.html