Creating a Windows Service that uses a parameter

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.
User avatar
wayne_Avery
Posts: 30
Last visit: Wed Nov 17, 2021 8:07 pm

Creating a Windows Service that uses a parameter

Post by wayne_Avery »

Product, version and build: PrimalScript 2020, Version 7.7.142
Operating system: Windows 10 Enterprise
PowerShell version: v5
32 or 64 bit OS: 64 Bit

Hi, I am all googled out and am seeking help/advice.
I have a PS script that will monitor a folder for new files, this works fine.
I wish to turn it into a Windows Service and am using the PrimalScript 2020 PowerShell Service template. I want to be able to use this anywhere so have opted for the script to bring in data on start from an xml file, this is where I am lost. Below is some of my script that shows the start parameter but the service when starting does not recognise it as I receive a app log entry "Line 2: Cannot process command because of one or more missing mandatory parameters: Start." The parameter is the path to the xml file. I suspect I am putting this in the wrong place but cannot find how it should look.
.......................
function Start-MyService
{
# [CmdletBinding()]
param(
[Parameter(ParameterSetName='Start', Mandatory=$true)]
[string]$Start
)
................

Can anybody shine some light on this for me?
Thanks in advance
Wayne

User avatar
Alexander Riedel
Posts: 7812
Last visit: Fri Dec 03, 2021 11:13 am
Answers: 7
Been upvoted: 12 times

Re: Creating a Windows Service that uses a parameter

Post by Alexander Riedel »

Yeah, it doesn't work that way. You cannot just redefine the functions with the parameters your like. They need to stay the way they are.
Typically for a Windows application that needs configuration, you put this as a file in a predefined folder.
E.g. C:\ProgramData\<Service name>\config.xml
and you load it from there on startup. Typically you place a default configuration file there when you install the service.
Please be aware that you cannot put that file in a user specific folder, as a service usually runs under the system account.
Alexander Riedel
SAPIEN Technologies, Inc.