Page 1 of 2

New PowerShell Service Template

Posted: Mon Mar 12, 2018 9:50 am
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?

Re: New PowerShell Service Template

Posted: Mon Mar 12, 2018 9:54 am
by Alexander Riedel
Yes, naming the functions accordingly and packaging it as a service should work fine.

Re: New PowerShell Service Template

Posted: Mon Mar 12, 2018 11:02 am
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?

Re: New PowerShell Service Template

Posted: Mon Mar 12, 2018 11:59 am
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
	}
}

Re: New PowerShell Service Template

Posted: Mon Mar 12, 2018 8:52 pm
by Alexander Riedel
Looks fine. Please describe exactly what you have done with it to run it as a service.

Re: New PowerShell Service Template

Posted: Tue Mar 13, 2018 1:36 pm
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.

Re: New PowerShell Service Template

Posted: Tue Mar 13, 2018 2:48 pm
by Alexander Riedel
Have you verified in the services pane that your service is there and running?

Re: New PowerShell Service Template

Posted: Tue Mar 13, 2018 7:21 pm
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.

Re: New PowerShell Service Template

Posted: Wed Mar 14, 2018 7:05 am
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 3690 times
Service 2.png
Service 2.png (389.36 KiB) Viewed 3690 times
Verify that this runs as desired on your system. Note that the service name is 'PowerShell Test Service'

Re: New PowerShell Service Template

Posted: Wed Mar 14, 2018 8:07 am
by rmerrill
That does run as desired, yes.
If I build the included code in the zip, it fails to operate properly.