Page 1 of 1

Terminating Windows Service Issues Error Message

Posted: Sun Oct 06, 2019 4:15 pm
by gareth.jacobs
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.

Re: Terminating Windows Service Issues Error Message

Posted: Sun Oct 06, 2019 6:29 pm
by Alexander Riedel
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
  1. function Stop-MyService
  2.  
  3. {
  4.  
  5.     $global:bRunService = $false # Signal main loop to exit
  6.  
  7.     $CountDown = 30 # Maximum wait for loop to exit
  8.  
  9.     while($global:bServiceRunning -and $Countdown -gt 0)
  10.  
  11.     {
  12.  
  13.         Start-Sleep -Seconds 1 # wait for your main loop to exit
  14.  
  15.         $Countdown = $Countdown - 1
  16.  
  17.     }
  18.  
  19.     # Place code to be executed on service stop here
  20.  
  21.     # Close files and connections, terminate jobs and
  22.  
  23.     # use remove-module to unload blocking modules
  24.  
  25. }
The code from the template is shown above for your convenience.