#start the following Windows services in the specified order:
[Array] $Services = ‘abc’,’abc’;
#The KSASvcChk checks the status of both Kaseya services (Kaseya Agent:’KAKSAAS143744567159389′, Kaseya Agent Endpoint:’KAENDKSAAS143744567159389′) and outputs the status of both
function KSASvcChk
{
#Service Name paramater
param($ServiceName)
$arrService = Get-Service -Name $ServiceName
if ($arrService.Status -ne “Running”)
{
Write $false
}
if ($arrService.Status -eq “running”)
{
Write $true
}
else {
Write $false
}
}
#The KSASvcStart function checks to ensure the Kaseya services are started, and if not, attempts to start them. If unsuccessful, the function stops and the script proceeds
function KSASvcStart
{foreach($ServiceName in $Services)
{
$arrService = Get-Service -Name $ServiceName
write-host $ServiceName
if ($arrService.Status -ne ‘Running’)
{
Start-Service $ServiceName
write-host $arrService.status
write-host ‘Service starting’
Start-Sleep -seconds 5
$arrService.Refresh()
if ($arrService.Status -eq ‘Running’)
{
Write-Host ‘Service is now Running’
}
}
} }
#The KSAInstall function copies the Kaseya installer from the network share, executes silently (flags built into package), and cleans up
function KSAInstall
{
Copy-Item -path \xxxx\KaseyaAgent\kcssetup.exe -Destination C:\temp\
C:\temp\KcsSetup.exe
Start-Sleep -Seconds 115
Remove-Item C:\temp\KcsSetup.exe -Force
Remove-Item C:\temp\kaseyainstall.ps1 -Force
}
#The KSALog function is initiated in the event that all heal attempts fail, creating a text file reporting the time of failure, status of services, and status of service executables
function KSALog {
$hostname = hostname
$ServiceStatus = Get-Service $Services | Select Displayname,Name,Status
[Array] $ServicePath = “C:\Program Files (x86)\Kaseya\KSAAS143744567159389\AgentMon.exe”,”C:\Program Files (x86)\Kaseya\KSAAS143744567159389\Endpoint\KaseyaEndpoint.exe”
Start-Transcript -Path C:\temp\kaseya_status.txt -Append
Write-Host “Hostname: $hostname”
Get-Date
Write-Host “Service Status (If blank, no service exists):”
$ServiceStatus
Write-Host “Exists: AgentMon.exe, KaseyaEndpoint.exe”
Test-Path $ServicePath
Stop-Transcript
}
#The functions are executed below. If the status of either service is not “running” or the service does not exist, the functions will attempt to start the stopped service, or if not possible, reinstall Kaseya
$status = KSASvcChk $Services
If ($status -contains $false){
KSASvcStart
}
Else {
Exit
}
$status = KSASvcChk $Services
If ($status -contains $false){
KSAInstall
}
Else {
Exit
}
$status = KSASvcChk $Services
If ($status -contains $false){
KSALog
}
Else {
Exit
}
Leave a comment