I have a DOS batch file that Stops and Starts Windows services. We ran into a situation where this activity failed. When on the server, I launched the server's Service application. I attempted to stop the service and got a popup message. I am theorizing that the popup interactivity is why the stop of the service failed. Is there a way, using the SC command, to disable the popup functionality? Below is the code to stop the service.
sc \\%SERVER% stop %SERVICE_NAME%
Are you set on using sc to stop the service? Perhaps an alternate command would not be stopped by the error dialog. I would suggest trying net stop "Service Name", however that would not allow you to stop the service remotely (without 3rd party tools).
If PowerShell is an option, try this (combining ideas from this SO thread with this one):
Start-Service -inputobject $(get-service -ComputerName %SERVER% -Name %SERVICE_NAME%) -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
Also, you could try using wmic to stop the service like this:
wmic /node:"%SERVER%" service where name="%SERVICE_NAME%" call stopservice
Related
I am doing some automated uninstalls of Autodesk software, and Autodesk has once again screwed the pooch with their uninstalls. Their uninstall is supposed to do reference counting on certain shared components, like their Single Signor Service, Autodesk Genuine Service, Licensing service, etc. The problem is, when you are uninstalling that last ADSK product, the uninstaller is too stupid to stop the service, so their uninstaller fails with a 1603 fatal error. Last year you could stop the services before you started the uninstall, but this year I am getting this error
Stop-Service : Service 'Autodesk Access Service Host (Autodesk Access Service Host)' cannot be stopped due to the following error: Cannot open Autodesk Access Service Host service on computer '.'.
When using this code
Get-Service -Name "Autodesk Access Service Host" | Stop-Service -Force -NoWait
I have verified with
(Get-Service -Name "Autodesk Access Service Host").CanStop
that service can be stopped. At least according to the property.
I also tried
Start-Process "$env:WINDIR\system32\sc.exe" \\.,stop,"Autodesk Access Service Host" -NoNewWindow -Wait
while ((Get-Service -ComputerName '.' -Name "Autodesk Access Service Host" |
Select -ExpandProperty Status) -ne 'Stopped') {
Write-Host "Waiting for service to stop..."
Start-Sleep -Seconds 10
}
And that has run for 15 minutes with no results. Interestingly I CAN disable the service, but I really don't want that. I just want to stop it temporarily, so IF the Autodesk uninstall that is running is the last one with a dependency on this service will uninstall it correctly and returns the correct exit code of 0.
EDIT: I tried
sc stop "Autodesk Access Service Host"
from an elevated command prompt and that shows
STATE : 3 STOP_PENDING
(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
so not really sure how to take STOP_PENDING along with NOT_STOPPABLE, nor why this would say NOT_STOPPABLE when the property above shows true.
I see some questions about this topic, but I cannot get it working
Get-Service -Name Spooler -ComputerName (Get-Content c:\tmp\scripts\Servers\iservers.txt) |
Stop-Service -PassThru | Set-Service -StartupType Disabled -whatif
The code executes for each server on the txt file, and stops de service, but not disable the service.
Any help to get it work and/or Troubleshooting???
Regards.
How to approach this kind of problem
In automation, we work up to complexity, meaning you should start simply and then add on more features until you see where it breaks.
Right now, you're trying to do a bunch of operations in one single line:
Load a list of computers and
Reach out to the computers and Stop a service and
Also while doing this, set the service to not automatically start.
There are a lot of problems you can run into, like "what happens if these PCs aren't enabled for remoting", or "what if you need a different account to handle stopping or disabling a service".
When you're trying to figure it all out in one-line, you're in for a bad and frustrating time.
How to fix it
Start simply. Start with one computer that's nearby and definitely turned on.
Begin with reading a service. Can you even get this operation to run?
Get-Service -ComputerName SomePC123 Spooler
Status Name DisplayName
------ ---- -----------
Running spooler Print Spooler
If you run into an error, then first figure out how to be able to remote into that one PC and see if the Print Spooler is running. Then, you will know what steps to deploy to all of your machines to prepare them for remoting.
Then, once you can check if a service is running, you can add on the next step, try to stop the service.
So your code would start to look like this:
$computers = get-content .\someTextFile.txt
forEach($computer in $computers){
$service = Get-Service -ComputerName $computer Spooler
"status of spooler on $computer is $($service.Status), with start type of $($service.StartType)"
#todo, set start type to Disabled...
}
Eventually, you will have migrated each step out of the one-liner and you'll know where and why any given command is failing. This is the way.
I'm running a command in powershell
blockchain-wallet-service start --port 3000
output looks like this
159243453597 - info: blockchain.info wallet service v0.26.3 running on http://127.0.0.1:3000
which occupies cli and prevents from running other commands after.
I created a new windows service using New-Service and it autostarts on reboot. BUT it doesn't respond to windows after it starts and windows shuts it down since it thinks it's dead. But it actually works the problem is the output. it just stays on
info: blockchain.info wallet service v0.26.3 running on http://127.0.0.1:3000
Forever. How do I go around it?
Installed service using NSSM.
Powershell_script.ps1
$serviceName = 'MyService'
nssm install $serviceName "C:\Users\Administrator\AppData\Roaming\npm\blockchain-wallet-service.cmd" "start"
nssm status $serviceName
Start-Service $serviceName
Get-Service $serviceName
Documentation used: git.nssm gist.nssm nssm.cc
P.S. Unfortunately I couldn't install it using New-Service option, as blockchain-wallet-service never response back to the windows service as I understood...
Before anyone says anything about access rights...
I have full access to services and can manually stop both the Avecto Defendpoint Service and the Avecto Defendpoint ePO Interface. And, all users who will be using this application will also have admin rights.
Im trying to find a way to stop the Avecto Defendpoint service through PowerShell. I've tried...
Right-click on PowerShell (Run as admin)
Stop-Service -Name "Avecto Defendpoint Service"
With this as a response...
Stop-Service : Service 'Avecto Defendpoint Service (Avecto Defendpoint
Service)' cannot be stopped due to the following error: Cannot open Avecto
Defendpoint Service service on computer '.'.
I've also tried using the -Force parameter with the same result. If I manually disable Avecto prior to running the PowerShell I can manually start the service back up again and THEN I can kill it as intended from the PowerShell; but this defeats the purpose of embedding the functionality into the program.
Any thoughts or suggestions?
You can use the following to stop Defendpoint from working:
Stop-Service -InputObject "Avecto Defendpoint Service" -Force
Once you are finished and need to re-enable it, use this:
Start-Service -InputObject "Avecto Defendpoint Service"
I have three servers, let's call them Deploy1, Deploy2, Target.
All servers are running Windows Server 2008R2, fully updated.
A domain user, admin1, is configured as administrator on all servers, and this is the user I'm running all the commands with.
The following command works on Deploy1:
Get-Service "MyService" -ComputerName Target | Stop-Service
When running the same command on Deploy2, the command fails with the following message:
Cannot find any service with service name 'MyService'.
On Deploy2, the following command works, and displays the service and its status.
Get-Service "MyService" -ComputerName Target
Now, I know there are other ways to stop/start services via PowerShell, but I like this one as it automatically waits for the server to actually stop/start.
So what could be wrong with Deploy2?
Powershell v2.0 has a bug (feature?) in how the object returned by Get-Service is implemented. It does not actually set the ComputerName property correctly. Because of this, it can only affect local services. If you upgrade to Windows Management Framework 3.0 (and consequently Powershell v3) the bug is fixed and will work correctly.
Does this work? If not, is there an error produced?
(Get-Service "MyService" -ComputerName Target).Stop()