我尝试用sc delete <服务名>删除一个Windows服务,并遇到以下错误:

[SC] DeleteService FAILED 1072: 指定的服务已标记为删除。

我已经做了:

Stopped the service, obviously. The sc queryex "<service name>" gives the following result: SERVICE_NAME: Stub service TYPE : 10 WIN32_OWN_PROCESS STATE : 1 STOPPED WIN32_EXIT_CODE : 1067 (0x42b) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x0 WAIT_HINT : 0x0 PID : 0 FLAGS : Ensured that Microsoft Management Console is closed (taskkill /F /IM mmc.exe), Ensured that Event Viewer is closed, Removed the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<service name> from the registry. Due to this removal, services.msc still shows the service (with a name, but no status or startup type), but the description is “<Failed to Read Description. Error Code: 2 >”. When attempting to view the properties, “The system cannot find the file specified.” is shown five times.

问题依然存在。

下一步是什么?


当前回答

在我的例子中,服务名称是“Monitor”,它也被一个名为“Monitor”的windows服务使用,当我试图更新我的服务时,我尝试卸载它们,安装程序试图删除windows服务“Monitor”,但它不能,安装总是回滚。

我最终将我的服务重命名为其他东西

其他回答

在我的情况下,这是由创建eventLog源时未处理的异常引起的。使用try catch来确定原因。

在我的情况下,我执行taskkill /f /im dongleserver.exe, 其中dongleserver.exe是我程序的exe文件。

然后我就可以重新安装我的程序了。

可能有几种原因导致服务卡在“标记为删除”中。

SysInternals' Process Explorer is opened. Closing it should lead to automatic removal of the service. Task Manager is opened. Microsoft Management Console (MMC) is opened. To ensure all instances are closed, run taskkill /F /IM mmc.exe. Services console is opened. This is the same as the previous point, since Services console is hosted by MMC. Event Viewer is opened. Again, this is the same as the third point. The key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\{service name} exists. Someone else is logged into the server and has one of the previously mentioned applications opened. An instance of Visual Studio used to debug the service is open.

有时,在通过PowerShell远程会话脚本删除服务时可能会发生这种情况,特别是当您试图多次删除服务时。在这种情况下,尝试在删除之前重新创建会话:

Remove-PSSession -Session $session
$newSession = New-PSSession -ComputerName $Name  -Credential $creds -ErrorAction Stop
Enter-PSSession $newSession

这对我很有用。

打开任务管理器 选择服务选项卡 选择有问题的服务 右键单击并选择“查看详细信息” 右键单击该服务并选择“结束流程树”

结束流程树将结束该流程以及该流程创建的所有流程。

然后重新安装服务。