Powershell service script

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ali.sobh
Posts: 4
Last visit: Wed Jan 09, 2019 8:15 am

Powershell service script

Post 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.
Attachments
TheExecutioner.ps1
without using service
(15.44 KiB) Downloaded 114 times
TheExecutionerTest.ps1
with using service
(35.28 KiB) Downloaded 96 times
Last edited by ali.sobh on Fri Aug 24, 2018 7:32 am, edited 1 time in total.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Powershell service script

Post 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.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
ali.sobh
Posts: 4
Last visit: Wed Jan 09, 2019 8:15 am

Re: Powershell service script

Post by ali.sobh »

Hi,
I get that when i start the service from service.mcs
User avatar
ali.sobh
Posts: 4
Last visit: Wed Jan 09, 2019 8:15 am

Re: Powershell service script

Post by ali.sobh »

I might not have coded the second script (theexecutionertest) correctly to use the built-in functions for the service.
User avatar
ali.sobh
Posts: 4
Last visit: Wed Jan 09, 2019 8:15 am

Re: Powershell service script

Post 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?
Attachments
TheExecutioner.xml
config
(1.57 KiB) Downloaded 103 times
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Powershell service script

Post 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.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Powershell service script

Post 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.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked