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

71 lines
2.6 KiB
PowerShell

function Start-FileBeatsService {
<#
.SYNOPSIS
Start the filebeats service on a given machine.
.DESCRIPTION
Start the filebeats service on a given machine. This function has the side effect of clearing a registry file if needed.
.PARAMETER ServiceName
Additional refining value for searching for the service. When not present, defaults to checking the paths returned by Get-FileBeatsPath
.PARAMETER Timeout
How long to wait on startup. Defaults to 5 seconds
.EXAMPLE
Start-FileBeatsService
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$ServiceName,
$Timeout = 5
)
$logLead = (Get-LogLeadName)
$filebeatServices = (Get-FileBeatsService -SearchPrefix $ServiceName)
if (Test-IsCollectionNullOrEmpty $filebeatServices) {
Write-Warning "$logLead : Filebeats not installed. Nothing to do."
return
}
foreach ($filebeatService in $filebeatServices) {
$ServiceName = $filebeatService.Name
try {
(Start-AlkamiService -ServiceName $ServiceName -Timeout $Timeout) | Out-Null
Write-Host "$logLead : Service [$ServiceName] started successfully"
} catch {
# If anything throws in this catch, that's okay. We want to let the user know that we have an issue.
# This should be automated, so anything that's happening ought to go to TC logs
# I'm not try-catching tho because I want this to fail miserably if this can't do the thing
Write-Warning "$logLead : Service [$ServiceName] not started successfully, attempting intervention"
Write-Host "$logLead : Ensuring service is stopped"
(Stop-AlkamiService $ServiceName) | Out-Null
# get the data path in the same folder as the exepath
$exePath = $filebeatService.ExePath
$dataPath = (Join-Path (Split-Path $exePath) "data")
if (Test-Path $dataPath) {
Write-Host "$logLead : Clearing potentially corrupted files from $dataPath"
(Remove-FileSystemItem -Path $dataPath -Recurse -Force) | Out-Null
} else {
Write-Host "$logLead : Data path not found at [$dataPath]"
}
# Ensure folder exists
(New-Item -Path $dataPath -ItemType Directory -Force) | Out-Null
Write-Host "$logLead : Files cleared, attempting to start again"
(Start-AlkamiService -ServiceName $ServiceName -Timeout $Timeout) | Out-Null
Write-Host "$logLead : Service [$ServiceName] started successfully"
}
}
}