我尝试用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.

问题依然存在。

下一步是什么?


当前回答

关闭当前打开的每个窗口,然后运行以下命令解决了我的问题:

taskkill /F /IM mmc.exe

其他回答

根据这里的一些答案所建议的关闭服务控制台确实允许我删除服务。在我的场景中,这只是一个短期的修复,因为所有后续的重新安装和删除服务都需要我采取这些额外的步骤。回顾我的网页。配置文件时,发现有一个错误,一旦修复,允许我轻松删除服务,而无需额外关闭服务控制台步骤。

当我使用应用程序验证器验证我的win服务时,我遇到了这个问题。即使在我关闭App Ver后,我的服务也被阻止删除。只有从App Ver中删除服务才能解决这个问题,并且服务被立即删除。看起来有些进程还在使用你的服务在你试图删除一个之后。

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

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

最有可能的是,删除服务失败是因为

protected override void OnStop()

停止服务时抛出错误。将内容包装在try catch中可以防止标记删除错误

protected override void OnStop()
{
            try
            {
                //things to do
            }
            catch (Exception)
            {
            }

}

以下步骤:

步骤1到位置C:\Windows\Microsoft.NET\Framework\v4.0.30319

步骤2执行命令:installutil /u full-path/servicename.exe

步骤3关闭服务面板并重新打开

步骤4执行命令:installutil full-path/servicename.exe