New PowerShell Service Template

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
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
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.
This topic is 6 years and 1 week 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.
User avatar
rmerrill
Posts: 10
Last visit: Tue May 22, 2018 7:07 am

New PowerShell Service Template

Post by rmerrill »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: PowerShell Studio 2018 v5.5.149
32 or 64 bit version of product: 64
Operating system: server 2016
32 or 64 bit OS: 64

*** Please add details and screenshots as needed below. ***

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

I was looking at this blog post:
https://www.sapien.com/blog/2018/02/27/ ... ript-2018/

Looked for more information for powershell studio and saw this:
viewtopic.php?f=12&t=12396#p66851

where he references the first article and says this:
If you use the functions mentioned in the following article, you can respond to calls to close the service.

Do I need to import anything or if I just name the functions the same as in the article and set the deploy settings to a service it will just work?
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: New PowerShell Service Template

Post by Alexander Riedel »

Yes, naming the functions accordingly and packaging it as a service should work fine.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
rmerrill
Posts: 10
Last visit: Tue May 22, 2018 7:07 am

Re: New PowerShell Service Template

Post by rmerrill »

is there any way you could open one of those templates, save it as a .ps1 and send it to me so I don't have to type all the comment code in there?
User avatar
rmerrill
Posts: 10
Last visit: Tue May 22, 2018 7:07 am

Re: New PowerShell Service Template

Post by rmerrill »

Trying it with this really simple set of code does not seem to work, I do not see any events in the application log:

Code: Select all

function Start-MyService
{
	$global:bRunService = $true
	$global:bServiceRunning = $false
	$global:testvar = 0
}

function Invoke-MyService
{
	$global:bServiceRunning = $true
	while ($global:bRunService)
	{
		try
		{
			$global:testvar = $global:testvar + 1
			Write-Host "Testvar is $global:testvar"
		}
		catch
		{
			Write-Host $_.Exception.Message
		}
		Start-Sleep -Seconds 1
	}
	$global:bServiceRunning = $false
}

function Stop-MyService
{
	$global:bRunService = $false
	$countdown = 30
	while ($global:bServiceRunning -and $countdown -gt 0)
	{
		Start-Sleep -Seconds 1
		$countdown = $countdown - 1
	}
}
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: New PowerShell Service Template

Post by Alexander Riedel »

Looks fine. Please describe exactly what you have done with it to run it as a service.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
rmerrill
Posts: 10
Last visit: Tue May 22, 2018 7:07 am

Re: New PowerShell Service Template

Post by rmerrill »

Type out Code

Deploy -> Packager -> Settings

Script Engine:
SAPIEN PowerShell V3 Host (Windows Service)
STA Mode unchecked

Output Settings:
Generate .config file checked
Manifest is embed a default manifest
run mode is current user

Execution Restrictions, version information, build options not touched.

Deploy -> Packager -> Build

put resulting executable in a folder
open command prompt and browse to folder
testservice /i
net start testservice
go look in application event log, see nothing.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: New PowerShell Service Template

Post by Alexander Riedel »

Have you verified in the services pane that your service is there and running?
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
rmerrill
Posts: 10
Last visit: Tue May 22, 2018 7:07 am

Re: New PowerShell Service Template

Post by rmerrill »

services indicates that it is running, get-process says that it is running, and I also see it in the task manager. The event log shows that it was successfully started. I also tried having it append a text file instead of attempting to write to the event log, but the file never is appended.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: New PowerShell Service Template

Post by Alexander Riedel »

I have taken your script and packaged it as a service (V3, 64 bit), all is in the attached zip file.
service 1.png
service 1.png (217.09 KiB) Viewed 4453 times
Service 2.png
Service 2.png (389.36 KiB) Viewed 4453 times
Verify that this runs as desired on your system. Note that the service name is 'PowerShell Test Service'
Attachments
TestService.zip
(623.01 KiB) Downloaded 115 times
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
rmerrill
Posts: 10
Last visit: Tue May 22, 2018 7:07 am

Re: New PowerShell Service Template

Post by rmerrill »

That does run as desired, yes.
If I build the included code in the zip, it fails to operate properly.
This topic is 6 years and 1 week 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.