115 lines
4.9 KiB
PowerShell
115 lines
4.9 KiB
PowerShell
function Get-ChocolateyServices {
|
|
<#
|
|
.SYNOPSIS
|
|
Returns Service Objects for Services Running out of the Chocolatey Path. By default it does not return disabled services. Defaults to running against the current machine.
|
|
|
|
.DESCRIPTION
|
|
Returns Service Objects for Services Running out of the Chocolatey Path. By default it does not return disabled services but can be overridden by using the -includeDisabled switch. Defaults to running against the local machine but can be overridden by passing the RemoteServer parameter.
|
|
|
|
.PARAMETER IncludeDisabled
|
|
[switch] When supplied, the function returns disabled services in the output
|
|
|
|
.PARAMETER RemoteServer
|
|
[string When supplied executes against a single remote machine. Defaults to localhost
|
|
|
|
.EXAMPLE
|
|
Get-ChocolateyServices
|
|
|
|
[Get-ChocolateyServices] : Finding services installed out of the chocolatey path: C:\ProgramData\chocolatey
|
|
[Get-ChocolateyServices] : Found 4 chocolatey services.
|
|
|
|
ProcessId Name StartMode State Status ExitCode
|
|
--------- ---- --------- ----- ------ --------
|
|
0 Alkami.Services.Subscriptions.Host Auto Stopped OK 0
|
|
0 Alkami.MicroServices.AccountBalanceSync1.Processor.Host Auto Stopped OK 0
|
|
0 Alkami.MicroServices.AccountBalanceSync2.Processor.Host Auto Stopped OK 0
|
|
0 Alkami.MicroServices.Accounts.Service.Host Auto Stopped OK 0
|
|
|
|
.EXAMPLE
|
|
Get-ChocolateyServices -IncludeDisabled
|
|
|
|
[Get-ChocolateyServices] : Finding services installed out of the chocolatey path: C:\ProgramData\chocolatey
|
|
[Get-ChocolateyServices] : Found 5 chocolatey services.
|
|
|
|
ProcessId Name StartMode State Status ExitCode
|
|
--------- ---- --------- ----- ------ --------
|
|
0 Alkami.Services.Subscriptions.Host Auto Stopped OK 0
|
|
0 Alkami.MicroServices.AccountBalanceSync1.Processor.Host Auto Stopped OK 0
|
|
0 Alkami.MicroServices.AccountBalanceSync2.Processor.Host Auto Stopped OK 0
|
|
0 Alkami.MicroServices.Accounts.Service.Host Auto Stopped OK 0
|
|
0 Alkami.MicroServices.AchTemplates.Service.Host Disabled Stopped OK 0
|
|
|
|
#>
|
|
[CmdletBinding()]
|
|
Param(
|
|
[switch]$IncludeDisabled,
|
|
#defaults to local computer if not specified
|
|
[string]$RemoteServer = "localhost"
|
|
)
|
|
|
|
$logLead = (Get-LogLeadName)
|
|
$chocoInstallPath = (Get-ChocolateyInstallPath)
|
|
Write-Host "$loglead : Finding services installed out of the chocolatey path: $chocoInstallPath"
|
|
|
|
$chocoPathFilter = $chocoInstallPath.Replace("\", "\\")
|
|
$disabledFilter = if ($IncludeDisabled.IsPresent) {
|
|
""
|
|
} else {
|
|
|
|
"AND StartMode <> 'Disabled'"
|
|
}
|
|
|
|
$isLocal = Compare-StringToLocalMachineIdentifiers $RemoteServer
|
|
$cimFilter = "PathName LIKE '%$chocoPathFilter%' $disabledFilter"
|
|
Write-Verbose "$logLead : Using CIMInstance Filter: $cimFilter"
|
|
|
|
# Including ComputerName in this call adds ~2.5s -- even to localhost. Don't change this
|
|
if ($isLocal) {
|
|
|
|
[array]$services = Get-CIMInstance win32_service -Filter $cimFilter
|
|
} else {
|
|
|
|
[array]$services = Get-CIMInstance win32_service -ComputerName $RemoteServer -Filter $cimFilter
|
|
}
|
|
|
|
# Sort subscription service to the front just in case this list is being used to start services.
|
|
$subscriptionServiceName = "Alkami.Services.Subscriptions.Host"
|
|
$subscriptionService = $services | Where-Object { $_.Name -like $subscriptionServiceName }
|
|
if($subscriptionService)
|
|
{
|
|
$updatedList = @($subscriptionService)
|
|
$updatedList += ($services | Where-Object { $_.Name -notlike $subscriptionServiceName })
|
|
[array]$services = $updatedList
|
|
}
|
|
|
|
if (Test-IsCollectionNullOrEmpty $services) {
|
|
|
|
Write-Warning "$logLead : No Chocolatey Services Found in: $chocoInstallPath"
|
|
return $null
|
|
}
|
|
|
|
# Write-Warning for each Service With No Files on Disk
|
|
foreach ($service in $services) {
|
|
|
|
if ($service.PathName -match '"') {
|
|
|
|
$path = $service.PathName.split('"')[1]
|
|
} else {
|
|
|
|
$path = $service.PathName
|
|
}
|
|
|
|
if(!(Test-Path $path)) {
|
|
|
|
Write-Warning "Service $($service.Name) has nonexistent file path `"$path`". Was it improperly uninstalled? Delete this service!"
|
|
continue
|
|
}
|
|
}
|
|
|
|
if (!(Test-IsCollectionNullOrEmpty $services)) {
|
|
|
|
Write-Host "$loglead : Found $($services.count) chocolatey services."
|
|
}
|
|
|
|
return $services
|
|
} |