Start Me Up! Get the Start Time of a Process Using PowerShell

(As soon as I though of this post name, I had this song stuck in my head. I’m sure (hopeful!) most of you are probably old enough to remember the Windows 95 Commercial. It’s a great song and a short post so if you want a bit of background music while you’re reading then open this in a new tab and enjoy.)

Recently I wanted to get the start times for a few SQL instances I have running. I immediately thought of the Get-Process cmdlet, so I checked out the members of get-process:

get-process|get-member

2013-07-11 10_31_42-powerShell v2

Looking through this list, I noticed that there was a start-time Property.

startTime

Bingo! That’s the one I need. I know that what I am looking for is the sqlservr process, so I define the executable name and pipe that into Select-Object to get the pertinent information.

Get-Process sqlservr | Select-Object name, starttime, id, productversion 

2013-07-11 10_38_58-powershell

Let’s make things look neater by piping into the Format-List cmdlet.

2013-07-11 10_40_36-powerhsell

Get-Process sqlservr | Select-Object name, starttime, id, productversion | Format-List

Tidy. So this also works for everything right?

2013-07-11 10_47_43-powershell

Wrong. It looks like that you need to be running PowerShell as an administrator to access the details via Get-Process. So certain processes don’t return the full list if you don’t have the correct permissions. Fortunately there is a workaround to get the start time, I’ve yet to find a solution that supplies the product version. In this case we use the Get-WmiObject cmdlet. It’s still the de-facto sysadmin cmdlet to use in routine tasks.


$a =  gwmi win32_process | ? { $_.name -eq "msmdsrv.exe" }
$a | Select-Object name, processId, @{Name="StartTime"; Expression={ $_.ConvertToDateTime( $_.CreationDate )}} | Format-List

2013-07-11 11_45_16-powershellThis also works for Reporting Services.

2013-07-11 11_47_26-powershell

Here’s the scripts if you want to run this remotely:


Get-Process -computername server01 sqlservr| Select-Object name, starttime, id, productversion | Format-List

 


$a =  gwmi win32_process -computername $server01 | ? { $_.name -eq "msmdsrv.exe" }
$a | Select-Object name, processId, @{Name="StartTime"; Expression={ $_.ConvertToDateTime( $_.CreationDate )}} | Format-List

If anyone does know of a way to get the Product Version then let me know as it’s super useful to know what version I am running without going to admin mode.

Author: Richie Lee

Full time computer guy, part time runner. Full time Dad, part time blogger. Knows a thing or two about Pokémon. Knows too much about SQL Agent. Writer of fractured sentences. Maker of the best damn macaroni cheese you've ever tasted.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s