Reputation: 15365
I have an existing Windows service on a remote machine.
Here is what I need to do:
My problem:
I keep getting the following error:
[SC] OpenSCManager FAILED 5:
Access is denied.
Upvotes: 2
Views: 9148
Reputation: 15365
@Daniel Mann asked the right question.
First of all,
sc STOP $(serviceName)
should have been a red flag.
@Daniel Mann asked:
Does the service account that your agent is running under have appropriate permissions [to do this]?
But one should also ask:
Which machine are you looking to execute this against? The build agent?
This was, in fact, my problem. When I tested this release pipeline the first time, the target machine and the build agent just happened to be the same machine.
This was an unfortunate accident, because this was not to be the case for all releases.
Solution:
This is correct:
sc \\$(serviceMachine) STOP $(serviceName)
And @Daniel Mann was correct:
Upvotes: 3
Reputation: 11
There are a couple ways to achieve this, but I'm gonna describe one that I recommend using PowerShell:
Select inline script and paste the code bellow:
Get-Service -DisplayName "YOUR_SERVICE_NAME" | Stop-Service
Repeat step 1 and paste the code below to re-create and start the service:
$params = @{
Name = "YOUR_SERVICE_NAME"
BinaryPathName = "C:\WINDOWS\System32\svchost.exe -k netsvcs"
DependsOn = "NetLogon"
DisplayName = "Test Service"
StartupType = "Manual"
Description = "This is a test service."
}
New-Service @params
You can read more about service creation parameters at Microsoft Docs:
Upvotes: 1