Windows Service - Stop Service - Restart Service

Ask your 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.
This topic is 4 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
DanielStockinger
Posts: 5
Last visit: Mon Nov 30, 2020 10:10 pm

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: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

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
Last visit: Mon Nov 30, 2020 10:10 pm

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Mon Nov 30, 2020 10:10 pm

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

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
Last visit: Mon Nov 30, 2020 10:10 pm

Re: Windows Service - Stop Service - Restart Service

Post by DanielStockinger »

thanks for the suggestion
This topic is 4 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked