• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1005
  • Last Modified:

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
2 Solutions
CardlyticsAuthor Commented:
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.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Your command only works if exactly one process is found. No process or more than one will result in above error.
Chris DentPowerShell DeveloperCommented:
You just need to turn it around because the problem is likely exactly as Qlemo suggests.

This will show you all long-running processes. I imagine you'll want to put the name back on the process for targetting.
Get-Process  |
  Select-Object Name, ID, StartTime, @{n='RunningFor';e={ New-TimeSpan -Start $_.StartTime }} |
  Where-Object { $_.RunningFor -gt (New-TimeSpan -Days 1) }

Open in new window

Once you have it targetting the right things, and assuming it finds what you want, you can stop processes with this:
Get-Process  |
  Select-Object Name, ID, StartTime, @{n='RunningFor';e={ New-TimeSpan -Start $_.StartTime }} |
  Where-Object { $_.RunningFor -gt (New-TimeSpan -Days 1) } |

Open in new window


David Johnson, CD, MVPOwnerCommented:
$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

CardlyticsAuthor Commented:
Thank you for the answers. It got me exactly what I needed.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now