Difference between revisions of "Get-service"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "``` function Get-ServiceUptime { param([string]$Name) # Prepare name filter for WQL $Name = $Name -replace "\\","\\" -replace "'","\'" -replace "\*","%" # Fetch serv...")
 
(No difference)

Latest revision as of 15:22, 29 June 2022

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*