Powershell service script

Ask your Windows 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.
Locked
User avatar
ali.sobh
Posts: 4
Joined: Tue Jan 23, 2018 8:23 am

Powershell service script

Post by ali.sobh » Fri Aug 24, 2018 6:38 am

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 12 times
TheExecutionerTest.ps1
with using service
(35.28 KiB) Downloaded 13 times
Last edited by ali.sobh on Fri Aug 24, 2018 7:32 am, edited 1 time in total.

User avatar
Alexander Riedel
Posts: 6836
Joined: Tue May 29, 2007 4:43 pm

Re: Powershell service script

Post by Alexander Riedel » Fri Aug 24, 2018 6:48 am

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
Joined: Tue Jan 23, 2018 8:23 am

Re: Powershell service script

Post by ali.sobh » Fri Aug 24, 2018 7:29 am

Hi,
I get that when i start the service from service.mcs

User avatar
ali.sobh
Posts: 4
Joined: Tue Jan 23, 2018 8:23 am

Re: Powershell service script

Post by ali.sobh » Fri Aug 24, 2018 7:34 am

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
Joined: Tue Jan 23, 2018 8:23 am

Re: Powershell service script

Post by ali.sobh » Fri Aug 24, 2018 8:34 am

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 12 times

User avatar
Alexander Riedel
Posts: 6836
Joined: Tue May 29, 2007 4:43 pm

Re: Powershell service script

Post by Alexander Riedel » Fri Aug 24, 2018 8:55 am

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
Site Admin
Posts: 260
Joined: Sun Mar 03, 2013 12:42 pm

Re: Powershell service script

Post by mxtrinidad » Fri Aug 24, 2018 9:00 am

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.

Locked