Terminating Windows Service Issues Error Message

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Posts: 13
Joined: Mon May 15, 2017 1:51 pm

Terminating Windows Service Issues Error Message

Post by gareth.jacobs » Sun Oct 06, 2019 4:15 pm

Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.167
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.17763.0

When manually stopping a PowerShell compiled Windows Service using services.msc, Windows reports the following Application Event Log error:

Log Name: Application
Event ID: 1000
Level: Error
Task Category: None

Line 2: The term 'Stop-MyService' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

The Service stops successfully after posting this event.

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

Re: Terminating Windows Service Issues Error Message

Post by Alexander Riedel » Sun Oct 06, 2019 6:29 pm

February last year the PowerShell Service template was updated to include a Stop-MyService function, in order to allow customers to react to a stop service event.
The template was updated for PrimalScript and PowerShell Studio. It would appear your service code was created before that was released.

https://www.sapien.com/blog/2018/02/27/ ... -MyService

Code: Select all

function Stop-MyService
$global:bRunService = $false # Signal main loop to exit
$CountDown = 30 # Maximum wait for loop to exit
while($global:bServiceRunning -and $Countdown -gt 0)
Start-Sleep -Seconds 1 # wait for your main loop to exit
$Countdown = $Countdown - 1
# Place code to be executed on service stop here
# Close files and connections, terminate jobs and
# use remove-module to unload blocking modules
The code from the template is shown above for your convenience.
Alexander Riedel
SAPIEN Technologies, Inc.