Page 1 of 1

Windows Service - Stop Service - Restart Service

Posted: Wed Jan 01, 2020 10:25 pm
by DanielStockinger
Hi,

Happy New Year to everyone.

First Question:
I would like to do some Checks in the Invoke-MyService Function, when the check fail, i would like to Stop the Service.
Is this possible?
This is, what i tried, but this doesn't work.
In the EventViewer => Application I found the Error Message from the THROW, but the Service is still on "Running"


function Invoke-MyService
{
$global:bServiceRunning = $true

while ($global:bRunService)
{
try
{
if($global:bServicePaused -eq $false) #Only act if service is not paused
{
# Check if Configuration File exists
if (-not (Test-Path -Path $XMLReportConfigurationPath))
{
$global:bRunService = $false
throw ("Configuration XML not Found!")
}


Second Question:
Is it possible to restart the Windows Service every 24 hours?

Thanks

Re: Windows Service - Stop Service - Restart Service

Posted: Wed Jan 01, 2020 11:47 pm
by Alexander Riedel
Setting the bRunService variable would only stop the script, but not the outer service layer.
Use
Stop-Service -Name "<Your Service>"

As for the second question, schedule a script to run every 24 hours, check if the service is running, if not, start it with Start-Service otherwise stop it, then start it again.

Re: Windows Service - Stop Service - Restart Service

Posted: Tue Jan 14, 2020 4:27 am
by DanielStockinger
Hi,

thanks for your Answer. This make sense.

Is it possible to show a Messagebox if the Service failed to start?

Thanks

Regards
Daniel

Re: Windows Service - Stop Service - Restart Service

Posted: Tue Jan 14, 2020 6:20 am
by jvierra
Services cannot display a UI because they are not run in an interactive session.

Re: Windows Service - Stop Service - Restart Service

Posted: Tue Jan 14, 2020 11:54 pm
by DanielStockinger
Thank you for the informations. There is already a message box for services if they cannot start.
Please see the Image.
http://social.technet.microsoft.com/For ... ile/438184

I would like to do a few PreChecks, and if they were unsuccessful,
I would like to display such a messagebox.

Regards
Daniel

Re: Windows Service - Stop Service - Restart Service

Posted: Wed Jan 15, 2020 12:10 am
by jvierra
That message box is produced by the system and is not part of the service. Think about it, iIf a service can't start how can it display anything. A process can only display output if it can run.

Re: Windows Service - Stop Service - Restart Service

Posted: Wed Jan 15, 2020 12:27 am
by Alexander Riedel
Create an external tool that does your pre-checks, starts the service if successful, displays a message if not.
Such a tool would run in a user context and can display messages etc.

Re: Windows Service - Stop Service - Restart Service

Posted: Wed Jan 15, 2020 12:28 am
by DanielStockinger
thanks for the suggestion