ps/Modules/Alkami.PowerShell.Services/Public/Get-ServiceInfoByCIMFragment.ps1
2023-05-30 22:51:22 -07:00

68 lines
2.4 KiB
PowerShell

Function Get-ServiceInfoByCIMFragment {
<#
.SYNOPSIS
Get the services of all services matched by the CIM fragment passed in.
.PARAMETER QueryFragment
A name or path fragment to match against
.PARAMETER ForceExact
Don't look for something "like" the QueryFragment, but _only_ this value
.OUTPUTS
Will return an array of any services found
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[Alias("Fragment")]
[string]$QueryFragment = "",
[switch]$ForceExact
)
$logLead = (Get-LogLeadName)
# I want to ensure that anything with a slash is escaped if present
# However, if it came in double escaped already I just quadded it
# This is the lazier version of inspecting all \\ vs \ in the string.
$pathifiedFragment = $QueryFragment.Replace('\','\\').Replace('\\\\','\\')
Write-Verbose "$logLead : Looking for anything that matches [$pathifiedFragment]"
$filter = "PathName like '%{0}%' OR Name like '%{0}%'" -f $pathifiedFragment
if ($ForceExact) {
$filter = "PathName like '{0}' OR Name like '{0}'" -f $pathifiedFragment
}
$cimServices = (Get-CIMInstance Win32_Service -Filter $filter)
$returnResults = @()
foreach($cimService in $cimServices) {
Write-Verbose "$logLead : Found [$($cimService.Name)] at [$($cimService.Path)]"
$rawExePath = ($cimService.PathName.Remove($cimService.PathName.LastIndexOf(".exe")) + ".exe").Replace('"', '')
$returnResults += @{
Name = $cimService.Name
DisplayName = $cimService.DisplayName
Path = $cimService.PathName
ExePath = $rawExePath
# SRE-18952 - We may be breaking things we don't expect - cbrand 2023-01-11
# This is due to how we need to do better path matching for HTTP service names
ParentFolder = Split-Path -Path $rawExePath -Parent
Started = $cimService.Started
State = $cimService.State
Status = $cimService.Status
StartMode = $cimService.StartMode
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = $cimService.StartMode
ProcessId = $cimService.ProcessId
InstallDate = $cimService.InstallDate
UserName = $cimService.StartName
}
}
return $returnResults
}