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?