Windows Service - Stop Service - Restart Service

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
DanielStockinger
Posts: 5
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Fri Dec 27, 2019 1:29 am

Windows Service - Stop Service - Restart Service

Post 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

User avatar
Alexander Riedel
Posts: 7265
Joined: Tue May 29, 2007 4:43 pm

Re: Windows Service - Stop Service - Restart Service

Post 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.
Alexander Riedel
SAPIEN Technologies, Inc.

DanielStockinger
Posts: 5
Joined: Fri Dec 27, 2019 1:29 am

Re: Windows Service - Stop Service - Restart Service

Post 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

jvierra
Posts: 14213
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Windows Service - Stop Service - Restart Service

Post by jvierra »

Services cannot display a UI because they are not run in an interactive session.

DanielStockinger
Posts: 5
Joined: Fri Dec 27, 2019 1:29 am

Re: Windows Service - Stop Service - Restart Service

Post 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

jvierra
Posts: 14213
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Windows Service - Stop Service - Restart Service

Post 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.

User avatar
Alexander Riedel
Posts: 7265
Joined: Tue May 29, 2007 4:43 pm

Re: Windows Service - Stop Service - Restart Service

Post 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.
Alexander Riedel
SAPIEN Technologies, Inc.

DanielStockinger
Posts: 5
Joined: Fri Dec 27, 2019 1:29 am

Re: Windows Service - Stop Service - Restart Service

Post by DanielStockinger »

thanks for the suggestion

Locked