Kaseya Community

Help in Agent Procedure

This question is not answered

Hello Everyone,

I need help in creating an agent procedure in kaseya which can restart a windows service if found stopped and send a custom email alert if the service fails to restart.

All Replies
  • This should help you get started:

    automationexchange.kaseya.com/.../324

  • Nathan if you can name the service. I will share monitor set for that service, which you can import to your VSA.

  • i have created the service monitor Rajeev for windows time service .It is working for me now but just need a help in creating an Agent procedure for the same with same conditions

  • We provide an application in our RMM Suite that works in concert with the monitor set and an agent procedure that does a fairly advanced bit of logic for service restarts. If the agent fails to restart the service after two tries, the procedure is given the service name and it invokes the application, passing the service name and the alert ID. (The alert ID is simply written to the logs to correlate the event.)

    Once the application starts, it checks for start/stop pending states, monitors the service for a few minutes to determine if it ever starts/stops. If it is a standalone EXE based service, that executable is terminated, the app waits 90 seconds, and it starts the service. It monitors the service for 5 minutes to insure that it starts and remains running before returning a success status to the procedure. If the service fails to start or remain running, it returns a fail status. The status of the corresponding alert event is updated, and we pass a Closed or New status ticket to the PSA to either track the successful remediation or get an engineer to review it.

    This auto-remediates a large number of events for us in our MSP practice.

    Glenn

  • do you want a procedure to set DC to sync time from external sources

    and then set time source for network to be your DC

    I have that procedure already and it works fine for me. You can PM me your email and I can share it to you. You need to do small edits which I can explain in the email.

  • Hello Natan,
    You could go for an easy Kaseya only script (less flexible but extremely simple).

    Note the "Continue on Fail" on the startWindowsService step which is needed to be able to get notification

    (otherwise if the service does not start the procedure will terminate at that step)

    Or you may use the below powershell as a start (more flexibility but more complex)

    You can send the email directly from the script or you can delegate sending an Email to Kaseya, by writing to host and using an #global:psresult# to check if the function succeeded and send an email if not.

    If you decide to use the Powershell to send the email Change From / To and SMTP server as needed.

    (I provided this an an example but commented it out as the example delegates to Kaseya the notification part)

    POWERSHELL SCRIPT CODE

    function WaitUntilServices($searchString, $status)

    {

       # Get all services where DisplayName matches $searchString and loop through each of them.

       foreach($service in (Get-Service -ServiceName $searchString))

       {

           $TimeSpan = New-TimeSpan -Seconds 30

           # Wait for the service to reach the $status or a maximum of 30 seconds

           $service.WaitForStatus($status, $TimeSpan)    

       }

    }

    function FuncCheckAndStartService{

    param($ServiceName)

       $arrService = Get-Service -Name $ServiceName

       if ($arrService.Status -ne "Running"){

          Start-Service -Name $ServiceName

          Try

          {

              WaitUntilServices $ServiceName "Running"

          }

          Catch

          {

          }

       }

       Clear-Host

       #Check again the service to refresh its status

       $arrService = Get-Service -Name $ServiceName

       if ($arrService.Status -ne "Running"){

          #Send-MailMessage -To "myemail@dummy.com" -From "mysender@dummy.com" -Subject "Service could not be started" -SmtpServer "mymailserver.dummy.com"

          Write-Host "Fail to start service"

       } else {

          Write-Host "Success"

       }

    }

    FuncCheckAndStartService -ServiceName $args[0]

    The above for now assumes you would delegate mailing to Kaseya.

    I will also assume you save the above as: CheckAndStartService.ps1

    KASEYA EXPORT PROCEDURE

    <ScriptExport><Procedure name="StartServiceAndNotifyIfFailed" treePres="3" id="117630973" folderId="477685157804603" treeFullPath="myProcedures - alessandrodimarco"><Body description=""><Statement name="WriteFile" continueOnFail="false"><Parameter xsi:type="StringParameter" name="Path" value="#vAgentConfiguration.AgentTempDir#\CheckAndStartService.ps1"/><Parameter xsi:type="StringParameter" name="ManagedFile" value="VSASharedFiles\CheckAndStartService.ps1"/><Parameter xsi:type="BooleanParameter" name="DeleteAfter" value="False"/></Statement><Statement name="Execute Powershell" continueOnFail="false"><Parameter xsi:type="StringParameter" name="Parameter1" value="#vAgentConfiguration.AgentTempDir#\CheckAndStartService.ps1"/><Parameter xsi:type="StringParameter" name="Parameter2" value="Spooler"/><Parameter xsi:type="StringParameter" name="Parameter3" value="True"/></Statement><If description=""><Condition name="CheckVariable"><Parameter xsi:type="StringParameter" name="VariableName" value="#global:psresult#"/><Parameter xsi:type="EnumParameter" name="Condition" value="NotEquals"/><Parameter xsi:type="StringParameter" name="Value" value="Success"/></Condition><Then><Statement name="SendEmail" continueOnFail="false"><Parameter xsi:type="StringParameter" name="To" value="Myself@dummy.com"/><Parameter xsi:type="StringParameter" name="Subject" value="Service Start Failed"/><Parameter xsi:type="MultiLineStringParameter" name="Body" value="Service Start Failed"/></Statement></Then></If></Body></Procedure></ScriptExport>

    You can just change the parameter on the IF and this can be used to check and start "any" windows service.

    EXAMPLE

    Best Regards

    Alex



    Added Simple Kaseya only procedure.
    [edited by: Alessandro Di Marco at 5:13 AM (GMT -7) on Sep 2, 2018]