简体   繁体   English

如何强制卸载windows服务

[英]How to force uninstallation of windows service

I installed a windows service using installUtil.exe.我使用 installUtil.exe 安装了 Windows 服务。

After updating the code I used installUtil.exe again to install the service w/o uninstalling the original version first.更新代码后,我再次使用 installUtil.exe 来安装服务,而无需先卸载原始版本。

When I now try to uninstall the service, installUtil.exe completes the uninstall successfully, but the service still appears.当我现在尝试卸载该服务时,installUtil.exe 成功完成了卸载,但该服务仍然出现。

If I try to change its properties, I receive the message 'service is marked for deletion'.如果我尝试更改其属性,则会收到消息“服务已标记为删除”。

How can I force the deletion (preferrably w/o restarting the server)?如何强制删除(最好不重新启动服务器)?

过去让我感到困扰的一件事是,如果您正在运行服务查看器,则会阻止服务被完全删除,因此请提前关闭

You don't have to restart your machine.您不必重新启动机器。 Start cmd or PowerShell in elevated mode.以提升模式启动 cmd 或 PowerShell。

sc.exe queryex <SERVICE_NAME>

Then you'll get some info.然后你会得到一些信息。 A PID number will show.将显示 PID 编号。

taskkill /pid <SERVICE_PID> /f

Where /f is to force stop.其中 /f 是强制停止。

Now you can install or launch your service.现在您可以安装或启动您的服务。

well, you can use SC.EXE to delete any windows Service forcefully if un-install doesnt removes by any chance.好吧,如果 un-install 万一没有删除,您可以使用 SC.EXE 强行删除任何 Windows 服务。

sc delete <Service_Name>

Read more on "MS Techno Blogging" Deleting Services Forcefully from Services MMC阅读有关“MS Techno Blogging” 从服务 MMC 中强行删除服务的更多信息

I know this isn't going to help, but it might help someone in the future.我知道这不会有帮助,但它可能会在未来帮助某人。

I've just had the same problem, closing and re-opening the services manager removed both the entry from the registry and completed the uninstall of the service.我刚刚遇到了同样的问题,关闭并重新打开服务管理器从注册表中删除了两个条目并完成了服务的卸载。

Previous to that, refreshing the services manager hadn't helped.在此之前,刷新服务管理器并没有帮助。

sc delete sericeName

Just make sure the service is stopped before doing this.在执行此操作之前,请确保服务已停止。 I have seen this work most times.我大部分时间都看过这部作品。 There are times where I have seen windows get stuck on something and it insists on a reboot.有时我看到 Windows 卡在某些东西上并且它坚持要重新启动。

Close cmd and services window if opened, then start cmd again by right clicking and selecting run as administrator.关闭 cmd 和服务窗口(如果打开),然后通过右键单击并选择以管理员身份运行再次启动 cmd。 If sc delete serviceName does not work or anything does not work.如果sc delete serviceName不起作用或任何东西都不起作用。

http://weblogs.asp.net/avnerk/archive/2007/09/05/windows-services-services-msc-and-the-quot-this-service-is-marked-for-deletion-quot-error.aspx http://weblogs.asp.net/avnerk/archive/2007/09/05/windows-services-services-msc-and-the-quot-this-service-is-marked-for-deletion-quot-error。 aspx

This worked for me这对我有用

$a = Get-WmiObject Win32_Service | Where-Object {$_.Name -eq 'psexesvc'}
$a.Delete()

Unfortunately, you need to restart the server.不幸的是,您需要重新启动服务器。 That should remove the "deleted" service.那应该删除“已删除”的服务。

Just in case this answer helps someone: as found here , you might save yourself a lot of trouble running Sysinternals Autoruns as administrator.以防万一这个答案对某人有帮助:如在此处找到的,您可以以管理员身份运行Sysinternals Autoruns 时省去很多麻烦。 Just go to the "Services" tab and delete your service.只需转到“服务”选项卡并删除您的服务。

It did the trick for me on a machine where I didn't have any permission to edit the registry.它在我没有任何编辑注册表权限的机器上为我解决了问题。

您可以手动删除位于HKLM\\SYSTEM\\CurrentControlSet\\Services的注册表项,之后您将不会在服务控制管理器中看到该服务(需要刷新或重新打开才能看到更改),您可以重新安装服务照常。

The following will work without restarting the machine:以下将在不重新启动机器的情况下工作:

  1. Search the Registry \\ HKEY_LOCAL_MACHINE for < Your Service Name > (both keys and values)在注册表 \\ HKEY_LOCAL_MACHINE 中搜索 < 您的服务名称 >(键和值)
  2. Set "Legacy" value to 0将“传统”值设置为 0

I am late, but would like to add an alternative, which may look strange, but I didn't see another way:我迟到了,但想添加一个替代方案,这可能看起来很奇怪,但我没有看到另一种方式:

As I install my Windows Services in a CI process each night, I needed something that works all the time and is completely automated.当我每晚在 CI 过程中安装我的 Windows 服务时,我需要一些可以一直工作并且完全自动化的东西。 For some reason, the services were always marked for deletion for a long time (5 minutes or more) after uninstalling them.出于某种原因,卸载后的服务总是被标记为删除很长时间(5 分钟或更长时间)。 Therefore, I extended the reinstallation batch script to make sure that the service is really deleted (simplified version):因此,我扩展了重装批处理脚本,以确保服务真的被删除(简化版):

REM Stop the service first
net stop My-Socket-Server

REM Same as installutil.exe, just implemented in the service
My.Socket.Server.exe /u

:loop1
    REM Easy way to wait for 5 seconds
    ping 192.0.2.2 -n 1 -w 5000 > nul
    sc delete My-Socket-Server
    echo %date% %time%: Trying to delete service.
    if errorlevel 1072 goto :loop1

REM Just for output purposes, typically I get that the service does not exist
sc query My-Socket-Server

REM Installing the new service, same as installutil.exe but in code
My.Socket.Server.exe /i

REM Start the new service
net start My-Socket-Server

What I can see, is that the service is marked for deletion for about 5 minutes (!) until it finally goes through.我能看到的是,该服务被标记为删除大约 5 分钟(!),直到它最终通过。 Finally, I don't need any more manual interventions.最后,我不再需要任何人工干预。 I will extend the script in the future so that something happens after a certain time (eg notification after 30 minutes).我将在未来扩展脚本,以便在特定时间后发生某些事情(例如 30 分钟后通知)。

Have you try stopping the service before calling uninstall?您是否尝试在调用卸载之前停止服务? I had this problem randomly.我随机遇到了这个问题。 Sometime I could remove it without restarting.有时我可以在不重新启动的情况下删除它。 My guess is that it has to do with the service still running我的猜测是它与仍在运行的服务有关

If you can not stop the service in Services , you can stop your windows service exe in Task Manager .如果在服务中不能停止服务,可以在任务管理器中停止你的windows服务exe。 After that, you can remove the service.之后,您可以删除该服务。

It is also worth noting that this:还值得注意的是:

sc delete "ServiceName"

does not work in PowerShell, sc is an alias for the cmdlet Set-Content in PowerShell.在 PowerShell 中不起作用,sc 是 PowerShell 中 cmdlet Set-Content 的别名。 You need to do:你需要做:

sc.exe delete "ServiceName"

Also make sure that there are no instances of the executable still active (perhaps one that might have been running, for whatever reason, independently of the service).还要确保没有可执行文件的实例仍然处于活动状态(可能是一个可能一直在运行的实例,无论出于何种原因,独立于服务)。

I was opening and closing MMC and looking for the PIDs to kill - but when looking in process explorer there were a couple of extant processes running from a forgotten scheduled batch.我正在打开和关闭 MMC 并寻找要杀死的 PID - 但是在进程资源管理器中查看时,有几个现存进程从一个被遗忘的预定批处理中运行。 Killed them.杀了他们。 Job done.任务完成。

Refreshing the service list always did it for me.刷新服务列表总是对我有用。 If the services window is open, it will hold some memory of it existing for some reason.如果服务窗口是打开的,它会保留一些出于某种原因存在的内存。 F5 and I'm reinstalling again! F5,我又重新安装了!

There are plenty of forum questions in that subject.该主题有很多论坛问题。

I have found the answer in windows api.我在 windows api 中找到了答案。 You don't need to restart the computer after uninstalling the service.卸载服务后无需重新启动计算机。 You have to call:您必须致电:

BOOL WINAPI CloseServiceHandle(
  SC_HANDLE hSCObject
);

That closes the handle of the service.这将关闭服务的句柄。 On windows 7 it solved my problem.在 Windows 7 上它解决了我的问题。 I do:我愿意:

  • stop service停止服务
  • close handle关闭手柄
  • uninstall service卸载服务
  • wait 3 sec等待 3 秒
  • copy new exe to the directory复制新的exe到目录
  • install the service安装服务
  • start service启动服务
  • close handle关闭手柄

I use the following PowerShell cobbled together from a few places for our Octopus Deploy instances when TopShelf messes up or a service fails for some other reason.当 TopShelf 出现问题或服务因某些其他原因失败时,我将以下 PowerShell 从几个地方拼凑到我们的 Octopus Deploy 实例中。

$ServiceName = 'MyNaughtyService'
$ServiceName | Stop-Service -ErrorAction SilentlyContinue
# We tried nicely, now KILL!!!
$ServiceNamePID = Get-Service | Where { $_.Name -eq $ServiceName} # If it was hung($_.Status -eq 'StopPending' -or $_.Status -eq 'Stopping') -and
$ServicePID = (Get-WmiObject Win32_Service | Where {$_.Name -eq $ServiceNamePID.Name}).ProcessID
Stop-Process $ServicePID -Force

This worked for me:这对我有用:

net stop "MyWindowsService"
taskkill /F /IM mmc.exe
sc delete "MyWindowsService"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM