Hey @rachel.dong,
Thank you for uploading the log and sorry it took some time to get back to you.
I took a look at the task log and unfortunately the only information it gives is what you have put in the original post:
CloseError: Service ‘service_name (service_name)’ cannot be stopped due to the following error: Cannot open service_name service on computer ‘.’.
I did some googling at this is not an Octopus error it is a windows error and it does seem like you need to run PowerShell or command prompt as an administrator (ie with elevated privileges) to start / stop a service, this is one of the articles I found.
I tested this on my local machine with my local admin account and if I don’t run the PowerShell session with elevated privileges I get the same error as you are seeing, if I ‘Run As Administrator’ it does stop the service.
Unfortunately there is not an easy way to change permissions of the session within the initial PowerShell window (ie Octopus itself will not run the PowerShell commands as an administrator) and it is very difficult to change a normal PowerShell session into an administrative one unless you do it within Windows itself (ie right click PowerShell and select Run As Administrator).
We have come across this a few times and we have not found a clean way of doing this (its a common bug bear on the internet) so unfortunately this is going to take some playing around on your end to get this to work.
Even if the service account you are using has Local Admin permissions on that machine you still need to run the PowerShell session as administrator in order to start / stop a service.
I did find this article (specifically near the end where it mentions Running PowerShell as Administrator with the Command Line, Creating a Batch File to Run PowerShell as Administrator and also Creating a Scheduled Task to Run PowerShell as Administrator, the latter seems to work for some people but you would need to speak to your security team as it may affect a security audit).
I do send that article to customers that have this issue so hopefully you find something useful in there but that is all I can provide I am afraid as this is a PowerShell limitation not Octopus.
I also found these three articles which mentions installing a tool and changing permissions of that individual service on that machine but I have not tested this. Check if you are allowed to download third party tools on your network.
I am sorry I cannot help you further here but hopefully some tinkering with the script will get you on the right track, if you do manage to get it working let us know as it will benefit other customers.
Kind Regards,
Clare