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

53 lines
1.8 KiB
PowerShell

function Get-WindowsServiceApplicationPath {
<#
.SYNOPSIS
Gets the folder path to a Windows service's application executable
.PARAMETER ServiceName
Name of the service to get the path to the folder that contains the exe
.NOTES
Previously this function called another function that would automagically select
the first result if there were more than one matching service name. This could be
semi-random and non-useful. Instead of continuing that, we will not act on a
non-deterministic or non-specific result.
#>
[CmdletBinding()]
[OutputType([string])]
param(
[Parameter(Mandatory = $true)]
[string]$ServiceName
)
$logLead = Get-LogLeadName
# Using Get-ServiceInfoByCIMFragment to keep the Exe-trimming to one place
$serviceInfo = Get-ServiceInfoByCIMFragment -QueryFragment $serviceName
if ($null -eq $serviceInfo) {
Write-Warning "$logLead : Unable to locate service info"
return $null
}
# Get-ServiceInfoByCIMFragment returns an array of hashtables, unless...
# only one service is found, and then the array is magically unboxed to a hashtable
# either way, directly accessing the ExePath lets us check the count of ExePaths
# More than one is not useful, or actionable, because we cannot predict what it
# will be
if ($service.ExePath.Count -gt 1) {
Write-Warning "$logLead : More than one service match the name $serviceName"
Write-Warning "$logLead : Use a more precise name"
return $null
}
$fullAppPath = $serviceInfo.ExePath
Write-Host "$logLead : Application Path is $fullAppPath"
$directoryOnly = Split-Path -Path $fullAppPath -Parent
Write-Host "$logLead : Directory is $directoryOnly"
return $directoryOnly
}