Windows Service Validation Check to Inhibit Service Start

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 5 years and 5 days 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
James99
Posts: 5
Last visit: Mon Nov 04, 2019 1:37 pm

Windows Service Validation Check to Inhibit Service Start

Post by James99 »

Hello,

I've been playing a bit with this and overall get how it works and have created/installed a basic service without issue:

https://www.sapien.com/blog/2017/07/12/ ... owershell/

I'm sure I'm just missing something but I'm trying to prevent the service from starting should something like a file/directory/reg key not be present. As as an example, one thing I've tried is this:

if (-Not (Test-Path "C:\ValidationFolder")) {Exit}

But I've also tried triggering the function Stop-MyService, creating my own functions, inserting code like the above within, etc.

I'm running PrimalScript 2019 v7.4.125.0

Thx!
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 Validation Check to Inhibit Service Start

Post by jvierra »

Services that have been newly installed do not start automatically.

Unfortunately what you are trying to ask is too vague. Why are you trying to stop a service? What does that have to do with the article about how to create a service?

When deploying a service the service will be installed. It will only be started if you use a separate action to start the service.

All registry keys required by a service are created by the service installer code. There are no associated folders for a service. If you service requires a file or folder then you need to create it in the deployment steps.

Without more complete information it is difficult to know what your exact issue is.
James99
Posts: 5
Last visit: Mon Nov 04, 2019 1:37 pm

Re: Windows Service Validation Check to Inhibit Service Start

Post by James99 »

I'm thinking way past service installation as in live/production.... We have some other apps in place that will look to the presence & status of the service to determine whether they are still authorized to run or not. If the service is not present, or present but not running, the other tools will not run. For this one role of many the service will perform, it's essentially acting as a licensing mechanism.

While the activation/check-in mechanism within the service will ultimately be more sophisticated, for the moment we're looking to check a few things within the operating system local to the service itself.

Another way to think about the ask is this - we want the service to run through some prerequisite checks and should they not be satisfied, we'd like for the service to shutdown.

Hope this helps!
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 Validation Check to Inhibit Service Start

Post by jvierra »

This is done by setting the service dependencies for the dependent services. The dependent service will not run if a dependency is not available. The issue will be logged to the event log. There is no reason to use a service to do this as the capability is built into Windows.
James99
Posts: 5
Last visit: Mon Nov 04, 2019 1:37 pm

Re: Windows Service Validation Check to Inhibit Service Start

Post by James99 »

jvierra wrote: Fri Mar 22, 2019 10:54 am This is done by setting the service dependencies for the dependent services. The dependent service will not run if a dependency is not available. The issue will be logged to the event log. There is no reason to use a service to do this as the capability is built into Windows.
This would apply only if we were making the service we're creating dependent on other services, only in our case there is only one service involved. The other apps involved are all command line custom system management apps and they look to both the presence and status of our windows service to determine whether or not they're authorized to run, which brings me back to the original question - How/where can 'prerequsite check' code be added into PrimalScript's services template/packager that will allow us to prevent the service from starting should pre-reqs not be met.
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 Validation Check to Inhibit Service Start

Post by jvierra »

What prereqs are you referring to? A service can check anything during start and abort the start. Just add the code that checks into the start event and then abort the start event if the checks fail.
James99
Posts: 5
Last visit: Mon Nov 04, 2019 1:37 pm

Re: Windows Service Validation Check to Inhibit Service Start

Post by James99 »

jvierra wrote: Sat Mar 23, 2019 11:24 am What prereqs are you referring to? A service can check anything during start and abort the start. Just add the code that checks into the start event and then abort the start event if the checks fail.
The prereq can be absolutely anything we might wish to test for and creating code for that is not normally an issue. What is an issue is figuring out how to prevent the service from starting when the prereq test fails...

As I've been experimenting with this, I've used this simple test that works as expected under normal circumstances. That said no matter where I've plugged this into the script and included functions within (including calling the stop function), the service starts no matter what:

if (-Not (Test-Path "C:\TestDirectory")) {Exit}

Here'e another test that works fine outside of the services script template:
  1.     if (-Not (Test-Path "C:\TestDirectory")) {
  2.     $global:bRunService = $false
  3.     $results = 'Directory Not Present'
  4.     } Else {
  5.     $global:bRunService = $true
  6.     $results = 'All is good'
  7.     }
  8. $results


Which I then plugged into the service template as shown here:
  1. function Start-MyService
  2. {
  3.     # Place one time startup code here.
  4.     # Initialize global variables and open connections if needed
  5.     if (-Not (Test-Path "C:\TestDirectory")) {
  6.     $global:bRunService = $false
  7.     } Else {
  8.     $global:bRunService = $true
  9.     }
  10.     $global:bServiceRunning = $false
  11.     $global:bServicePaused = $false;
  12. }
Helpful?
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 Validation Check to Inhibit Service Start

Post by jvierra »

You cannot exit from q service. You can only set the run flag to end the service. When you want to not run the service set the run flag to false and return from the method. The service will terminate immediately.
James99
Posts: 5
Last visit: Mon Nov 04, 2019 1:37 pm

Re: Windows Service Validation Check to Inhibit Service Start

Post by James99 »

jvierra wrote: Sat Mar 23, 2019 7:14 pm You cannot exit from q service.
This makes sense, thank you!

jvierra wrote: Sat Mar 23, 2019 7:14 pm You can only set the run flag to end the service. When you want to not run the service set the run flag to false and return from the method. The service will terminate immediately.
My interpretation of this is -- $global:bRunService = $false -- only because nothing I've tried along these lines has worked, I'm obviously not even close to being correct. If you could add a bit more clarity to your answer within the context of the services template, it would be greatly appreciated.
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 Validation Check to Inhibit Service Start

Post by jvierra »

Yes. That is the run flag. Look a the processing loop. It will only enter the loop if the flag is true. When false, the service exits.

Just that simple.
This topic is 5 years and 5 days 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