Determine the Start Time of a Windows Service with PowerShell
Recently, I was asked to setup a scheduled task or job to restart specific services on certain servers each night and while that's simple, how do you know for sure the services were indeed restarted? One way is to determine how long a service has been running or when they were started. The dilemma is the necessary information isn't available using the Get-Service cmdlet or with CIM or WMI using the Get-CimInstance or Get-WmiObject cmdlets with the Win32_Service class.
The good news is that every Windows service that's running has an underlying process and the start time of a process can be determined with either the Get-Process cmdlet or the WMI Win32_Process class. All you have to do us match up the services to the corresponding process.
Get-Service
doesn't include the process id so I had to resort to using WMI to retrieve the service
information so the process id can be retrieved. A PowerShell one-liner can be used to retrieve the
information I was looking for.
1Get-CimInstance -ClassName Win32_Service -PipelineVariable Service |
2ForEach-Object {
3 Get-Process -Id $_.ProcessId |
4 Select-Object -Property @{label='Status';expression={$Service.State}},
5 @{label='Name';expression={$Service.Name}},
6 @{label='DisplayName';expression={$Service.DisplayName}},
7 StartTime
8}
That one-liner could simply be run inside of Invoke-Command to retrieve the same information from remote systems, but I decided to turn it into a function instead. My first iteration was simple:
1function Get-MrService {
2 [CmdletBinding()]
3 param (
4 [string]$Name
5 )
6
7 $Params = @{}
8
9 if ($PSBoundParameters.Name) {
10 $Params.Filter = "Name = '$Name'"
11 }
12
13 $Services = Get-CimInstance -ClassName Win32_Service @Params
14
15 foreach ($Service in $Services) {
16 $Process = Get-Process -Id $Service.ProcessId
17
18 [pscustomobject]@{
19 Status = $Service.State
20 Name = $Service.Name
21 DisplayName = $Service.DisplayName
22 StartTime = $Process.StartTime
23 }
24 }
25
26}
When adding remoting within the function itself, I ran into a problem where the services were retrieved from the remote system, but it was trying to match them up to local processes so a little rework was required.
In the end, I decided to use CIM sessions for remote connectivity so the part of the function which
previously used the Get-Process
cmdlet was changed to use Get-CimInstance
with the
Win32_Process
class.
1#Requires -Version 3.0
2function Get-MrService {
3
4<#
5.SYNOPSIS
6 Gets the services on a local or remote computer.
7
8.DESCRIPTION
9 The Get-MrService function gets objects that represent the services on a local computer or on a remote computer,
10 including running and stopped services. You can direct this function to get only particular services by specifying
11 the service name of the services.
12
13.PARAMETER Name
14 Specifies the service names of services to be retrieved. Wildcards are permitted. By default, this function gets
15 all of the services on the computer.
16
17 .PARAMETER CimSession
18 Specifies the CIM session to use for this cmdlet. Enter a variable that contains the CIM session or a command that
19 creates or gets the CIM session, such as the New-CimSession or Get-CimSession cmdlets. For more information, see
20 about_CimSessions.
21
22.EXAMPLE
23 Get-MrService -Name bits, w32time
24
25.EXAMPLE
26 Get-MrService -CimSession (New-CimSession -ComputerName Server01, Server02) -Name Win*
27
28.INPUTS
29 None
30
31.OUTPUTS
32 PSCustomObject
33
34.NOTES
35 Author: Mike F Robbins
36 Website: http://mikefrobbins.com
37 Twitter: @mikefrobbins
38#>
39
40 [CmdletBinding()]
41 param (
42 [ValidateNotNullOrEmpty()]
43 [string[]]$Name = '*',
44
45 [Microsoft.Management.Infrastructure.CimSession[]]$CimSession
46 )
47
48 $ServiceParams = @{}
49
50 if ($PSBoundParameters.CimSession) {
51 $ServiceParams.CimSession = $CimSession
52 }
53
54 foreach ($n in $Name) {
55 if ($n -match '\*') {
56 $n = $n -replace '\*', '%'
57 }
58
59 $Services = Get-CimInstance -ClassName Win32_Service -Filter "Name like '$n'" @ServiceParams
60
61 foreach ($Service in $Services) {
62
63 if ($Service.ProcessId -ne 0) {
64 $ProcessParams = @{}
65
66 if ($PSBoundParameters.CimSession) {
67 $ProcessParams.CimSession = $CimSession | Where-Object ComputerName -eq $Service.SystemName
68 }
69
70 $Process = Get-CimInstance -ClassName Win32_Process -Filter "ProcessId = '$($Service.ProcessId)'" @ProcessParams
71 }
72 else {
73 $Process = ''
74 }
75
76 [pscustomobject]@{
77 ComputerName = $Service.SystemName
78 Status = $Service.State
79 Name = $Service.Name
80 DisplayName = $Service.DisplayName
81 StartTime = $Process.CreationDate
82 }
83
84 }
85
86 }
87
88}
I also ran into a problem where all of the systems were being queried for the process id of every service instead of just the ones running on that particular system. I narrowed down which CIM sessions were being queried which resolved that problem.
1Get-MrService -Name msdtc, bits, w32time, winrm |
2Format-Table -AutoSize
3
4$CimSession = New-MrCimSession -ComputerName dc01, sql08, sql14
5
6Get-MrService -CimSession $CimSession -Name msdtc, bits, w32time, winrm |
7Sort-Object -Property ComputerName, Name |
8Format-Table -AutoSize
Although this function requires PowerShell 3.0 or higher on the system it's being run from, since I chose to use CIM sessions for remote connectivity instead of simply wrapping the commands inside of PowerShell remoting, I'm able to target machines using either WSMan or DCom. For more information on that topic, see my Targeting Down Level Clients with the Get-CimInstance PowerShell Cmdlet blog article.
The previous example takes advantage of my New-MrCimSession
function which automatically
determines if a system is able to use WSMan otherwise it automatically falls back to using DCom.
More information about that function can be found in my
PowerShell Function to Create CimSessions to Remote Computers with Fallback to Dcom
blog article.
The Get-MrService
function shown in this blog article can be downloaded from
my PowerShell repository on GitHub.
µ