I knew I wasn't totally satisfied with my recent attempt at listing service uptime. I knew there was a more elegant solution and here it is:
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
$s=Get-WmiObject -query "Select name,processId,state from Win32_service where state='running'"
foreach ($item in $s) {
$p=(Get-Process | Where {$_.id -eq $item.ProcessID}).StartTime
$u=(get-date).Subtract($p)
Write-Host $item.Name `t $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds
}
It made more sense to only make one WMI call to get running services. Now don't get too excited about this. There is yet another tweak to be had. Even though the Get-Wmiobject cmdlet will let me connect to a remote system, the Get-Process cmdlet runs locally. To get this to work completely for a remote system I need to use Get-Wmiobject and query the Win32_process on the remote system.
For the most part that's not a problem. All I have to do is pass the processID of each service to another WMI query for the associated Win32_Process.
$s=Get-WmiObject -query "Select name,processId,state from Win32_service where state='running'" -computer $computer
foreach ($item in $s) {
$query="Select handle,creationdate from win32_process where handle='"+$item.ProcessID+"'"
$p=Get-WmiObject -query $query -computer $computer
The Win32_Process class includes a CreationDate property. However, it is in the unfriendly WMI time format. To PowerShell it is a string that looks like 20070214115312.491614-300. Fortunately, I have a VBSCript function to convert this to a date. I modified the function so it would work in PowerShell. The trick was first building a string type object and then casting it as a datetime object.
Function Convert-WMITime{
param([string]$WMITime)
$yr=[string]$WMITime.Substring(0,4)
$mo=[string]$WMITime.substring(4,2)
$dy=[string]$WMITime.substring(6,2)
$tm=[string]$WMITime.substring(8,6)
$hr=[string]$tm.Substring(0,2)
$min=[string]$tm.substring(2,2)
$sec=[string]$tm.substring(4,2)
#create string
$s="$mo/$dy/$yr "+$hr+":"+$min+":"+$sec
#cast result as DateTime type
$result=[DateTime]$s
return $result
}
To get the uptime I convert the WMITime to a DateTime object that PowerShell understands and then use the same code I've used before:
$start=Convert-WmiTime($p.CreationDate)
$u=(get-date).Subtract($start)
Write-Host $item.Name `t $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds
Here's the complete script. The Convert-WMITime function needs to come before it if you use a script or be a part of your profile.
$computer="dc01"
Write-Host -fore Green -back Black $computer.ToUpper()
$s=Get-WmiObject -query "Select name,processId,state from Win32_service where state='running'" -computer $computer
foreach ($item in $s) {
$query="Select handle,creationdate from win32_process where handle='"+$item.ProcessID+"'"
$p=Get-WmiObject -query $query -computer $computer
$start=Convert-WmiTime($p.CreationDate)
$u=(get-date).Subtract($start)
Write-Host $item.Name `t $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds
}
Again, this is more or less fast & furious coding. There's plenty of room for formatting improvements and other enhancements which I'll leave to you.
I found an even more elegant solutions using ConverttoDateTime. There is a revised post at http://jdhitsolutions.blogspot.com/2007/02/more-with-process-and-service-uptime.html