I have been trying to stop and remove a windows service on a remote server using powershell, specifically through a PostDeploy.ps1 automatically called through Octopus Deploy. I have tried using the following as described here:
$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service.delete()
When the script runs, I get this:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :
but the service still remains. I have also tried using
net stop "my service"
followed by
sc.exe delete "my service"
but get access denied messages. I can delete and remove the service when I use these commands manually on the box using run as administrator.
I have also tried doing it as the example describes on the octopus deploy pages here.
For information I am able to install and start the service through powershell.
Can I add something to my ps1 file to force the commands to run as Administrator, or is there something else I need to do?
The Octopus agent on the deployment target machine is what executes the Powershell scripts. So you will need to make sure that the user the Octopus agent runs as has been set up with the appropriate permissions to manage other services:
See these articles:
1. http://octopusdeploy.com/documentation/configuration/custom-user
2. http://help.octopusdeploy.com/discussions/problems/64-installing-tentacle-under-workgroup-user-or-domain-accoun
3. http://octopusdeploy.com/documentation/security/octopus-tentacle