Page 1 of 1

Creating a Windows Service that uses a parameter

Posted: Thu Aug 27, 2020 6:19 pm
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()]
[Parameter(ParameterSetName='Start', Mandatory=$true)]

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

Re: Creating a Windows Service that uses a parameter

Posted: Fri Aug 28, 2020 6:56 am
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.