Windows Service Validation Check to Inhibit Service Start

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
James99
Posts: 5
Joined: Fri Mar 22, 2019 3:10 pm

Windows Service Validation Check to Inhibit Service Start

Post by James99 » Fri Mar 22, 2019 8:36 am

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!

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Windows Service Validation Check to Inhibit Service Start

Post by jvierra » Fri Mar 22, 2019 9:20 am

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
Joined: Fri Mar 22, 2019 3:10 pm

Re: Windows Service Validation Check to Inhibit Service Start

Post by James99 » Fri Mar 22, 2019 10:27 am

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!

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Windows Service Validation Check to Inhibit Service Start

Post by jvierra » 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.

James99
Posts: 5
Joined: Fri Mar 22, 2019 3:10 pm

Re: Windows Service Validation Check to Inhibit Service Start

Post by James99 » Sat Mar 23, 2019 11:10 am

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.

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Windows Service Validation Check to Inhibit Service Start

Post by jvierra » 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.

James99
Posts: 5
Joined: Fri Mar 22, 2019 3:10 pm

Re: Windows Service Validation Check to Inhibit Service Start

Post by James99 » Sat Mar 23, 2019 6:58 pm

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:

Code: Select all

if (-Not (Test-Path "C:\TestDirectory")) {
$global:bRunService = $false
$results = 'Directory Not Present'
} Else {
$global:bRunService = $true
$results = 'All is good'
}
$results


Which I then plugged into the service template as shown here:

Code: Select all

function Start-MyService
{
# Place one time startup code here.
# Initialize global variables and open connections if needed
if (-Not (Test-Path "C:\TestDirectory")) {
$global:bRunService = $false
} Else {
$global:bRunService = $true
}
$global:bServiceRunning = $false
$global:bServicePaused = $false;
}
Helpful?

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Windows Service Validation Check to Inhibit Service Start

Post by jvierra » Sat Mar 23, 2019 7:14 pm

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
Joined: Fri Mar 22, 2019 3:10 pm

Re: Windows Service Validation Check to Inhibit Service Start

Post by James99 » Sat Mar 23, 2019 7:38 pm

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.

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Windows Service Validation Check to Inhibit Service Start

Post by jvierra » Sat Mar 23, 2019 8:07 pm

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.

Locked