Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

More with Service Uptime

Posted on February 16, 2007August 5, 2009

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:

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline
Technorati tags: PowerShell, Scripting, Services, Uptime, Pipeline, TipSheet, WMI

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “More with Service Uptime”

  1. Jeffery Hicks says:
    February 20, 2007 at 8:54 pm

    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

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d