Page 1 of 1

Powershell service script

Posted: Fri Aug 24, 2018 6:38 am
by ali.sobh
Hello,
I am having a hard time to understand how the PowerShell Service Script works.
I have tried the ways in those 2 links:
https://www.sapien.com/blog/2017/07/12/ ... owershell/
https://www.sapien.com/blog/2018/02/27/ ... ript-2018/
Whenever I start the service i get the "access denied error 5" error message.
The Powershell script itself works perfectly and launches correctly, and does what it has to do whenever i launch the .ps1.
When i modify it as it says in "new-powershell-service-template-for-primalscript-2018" or create a service .exe as in "write-a-windows-service-in-powershell" I get the error message when launching the service.

Re: Powershell service script

Posted: Fri Aug 24, 2018 6:48 am
by Alexander Riedel
If you post more than a few lines of code, please post it with the pertinent files as an attachment.
"Whenever I start the service i get the "access denied error 5" error message."
You get that from windows when it attempts to start the service, when you attempt to register the service or from your code in some log or the event log.
Please be more specific.

Re: Powershell service script

Posted: Fri Aug 24, 2018 7:29 am
by ali.sobh
Hi,
I get that when i start the service from service.mcs

Re: Powershell service script

Posted: Fri Aug 24, 2018 7:34 am
by ali.sobh
I might not have coded the second script (theexecutionertest) correctly to use the built-in functions for the service.

Re: Powershell service script

Posted: Fri Aug 24, 2018 8:34 am
by ali.sobh
I forgot to add that the variables/config are in a xml file.
should i package it with the ps1? if yes how?

Re: Powershell service script

Posted: Fri Aug 24, 2018 8:55 am
by Alexander Riedel
If you cannot start a service it is usually a permission problem.
Check if you can start/stop other services. Check if you have a group policy that requires executables to be signed. Check if the resulting is in a folder from which execution is allowed. If the exe is in a folder that is a user folder, the system may not find it.

Re: Powershell service script

Posted: Fri Aug 24, 2018 9:00 am
by mxtrinidad
Currently, you got the code in the wrong place. The access error you are getting is due to the $credential not been global. Anything that execute needs to be inside the try/catch and not outside.

If you got a script that runs. Try executing the script file inside the service template.

Check out the recent video on creating a PowerShell Service: https://www.sapien.com/blog/2018/06/25/ ... malscript/

It will give you hint on where to add to line to execute the script file.

Give it a try and let us know.