Pergunta

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?

Foi útil?

Solução

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

Outras dicas

If some commands need to run as Administrator, you might use Scheduled Tasks to schedule the script and get it run as System.

You might also start PowerShell as Admin, even withing PowerShell using:

Start-Process -verb runas PowerShell

You might try this:

Start-Process -verb runas PowerShell PostDeploy.ps1

If you have the .ps1 script working locally, then simply use the PsExec tool to run the script on remote machine as follows:

- Create a share on your local machine and put both the psexec.exe and the .ps1 script in it
- Run the following command - change to direct in cmd where the psexec file is:

PsExec.exe \\REMOTECOMPUTER -u -p \\mypc\shared\PostDeploy.ps1

The -u switch (Specify a domain user name for login to remote computer that has administrative right or Domain Admin privileges) The -p switch (Specify password for the Administrative/Domain account as specified above. If you omit this you will be prompted to enter a hidden password)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top