Get-service

From UVOO Tech Wiki
Jump to navigation Jump to search
function Get-ServiceUptime
{
  param([string]$Name)

  # Prepare name filter for WQL
  $Name = $Name -replace "\\","\\" -replace "'","\'" -replace "\*","%"

  # Fetch service instance
  $Service = Get-CimInstance -ClassName Win32_Service -Filter "Name LIKE '$Name'"

  # Use ProcessId to fetch corresponding process
  $Process = Get-CimInstance -ClassName Win32_Process -Filter "ProcessId = $($Service.ProcessId)"

  # Calculate uptime and return
  return (Get-Date) - $Process.CreationDate
}
$uptime = Get-ServiceUptime -Name MyServiceName
# or
$uptime = Get-ServiceUptime -Name MyServiceN*