New PowerShell Service Template

Support for all customers who have purchased a SAPIEN PowerShell Studio product license. This forum does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE 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.
User avatar
rmerrill
Posts: 10
Joined: Mon Aug 14, 2017 1:30 pm

New PowerShell Service Template

Post by rmerrill » Mon Mar 12, 2018 9:50 am

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: 6710
Joined: Tue May 29, 2007 4:43 pm

Re: New PowerShell Service Template

Post by Alexander Riedel » Mon Mar 12, 2018 9:54 am

Yes, naming the functions accordingly and packaging it as a service should work fine.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
rmerrill
Posts: 10
Joined: Mon Aug 14, 2017 1:30 pm

Re: New PowerShell Service Template

Post by rmerrill » Mon Mar 12, 2018 11:02 am

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
Joined: Mon Aug 14, 2017 1:30 pm

Re: New PowerShell Service Template

Post by rmerrill » Mon Mar 12, 2018 11:59 am

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: 6710
Joined: Tue May 29, 2007 4:43 pm

Re: New PowerShell Service Template

Post by Alexander Riedel » Mon Mar 12, 2018 8:52 pm

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
Joined: Mon Aug 14, 2017 1:30 pm

Re: New PowerShell Service Template

Post by rmerrill » Tue Mar 13, 2018 1:36 pm

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: 6710
Joined: Tue May 29, 2007 4:43 pm

Re: New PowerShell Service Template

Post by Alexander Riedel » Tue Mar 13, 2018 2:48 pm

Have you verified in the services pane that your service is there and running?
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
rmerrill
Posts: 10
Joined: Mon Aug 14, 2017 1:30 pm

Re: New PowerShell Service Template

Post by rmerrill » Tue Mar 13, 2018 7:21 pm

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: 6710
Joined: Tue May 29, 2007 4:43 pm

Re: New PowerShell Service Template

Post by Alexander Riedel » Wed Mar 14, 2018 7:05 am

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 1905 times
Service 2.png
Service 2.png (389.36 KiB) Viewed 1905 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 30 times
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
rmerrill
Posts: 10
Joined: Mon Aug 14, 2017 1:30 pm

Re: New PowerShell Service Template

Post by rmerrill » Wed Mar 14, 2018 8:07 am

That does run as desired, yes.
If I build the included code in the zip, it fails to operate properly.