4

请帮助我了解使用命令“sc delete”删除 Windows 服务和使用命令“installutil /u”卸载 Windows 服务之间的区别。

经过一番探索后,我意识到要使用 installutil 命令,您必须首先停止 Windows 服务,而 sc delete 命令负责停止服务本身。如有错误请指正。

4

1 回答 1

9

一个相关的 SO问题和答案(特别是两个 - herehere)解释了差异主要是sc delete不需要重新启动并且installutil /u确实如此。链接问题的其他答案也值得检查其他差异。

(小)更新:

正如您在修改后的问题中指出的那样,是的,链接问题的其他答案突出显示的另一个主要区别是,sc delete它将首先停止要删除的服务。

(大)更新:

我不满足于简单地信任链接的问题和答案而不进一步验证:事实证明它们并不完全正确 - 至少就我在 Windows 7 Pro x64 上安装和卸载服务的实验而言。

我看到的是,人们可以在很大程度上体验每种工具所描述的服务卸载;使用命令卸载并不总是遵循这些模式。

可能还有更多内容,但我看到的命令行为的主要决定因素是:

  • 卸载时服务是否正在运行
  • 服务是否将服务(卸载)安装程序实现System.Configuration.Install.Installer (我发现的CodeProject 文章- 诚然认为我多年前阅读过 - 很好地描述了这一点。)

代替表格来概述我发现的条件和结果,这里有一些伪代码来解释:

// pseudocode to explain the empirically observed logic of "sc delete" vs.
// "installutil /u" (on Windows 7 Pro x64)
//
// services developed for the investigative experiment:
//     S[1-4]:    simple Windows services that do *not* implement an
//                (un)installer
//     SWI[1-4]:  simple Windows services that implement an (un)installer
if serviceIsRunning
{
    if "sc delete"
    {
        // Using:   S1, SWI1
        // Result:  The service is not stopped but disabled and marked for deletion, requiring a reboot.
    }
    else // "installutil /u"
    {
        if serviceImplementsInstaller
        {
            // Using:   SWI2
            // Result:  The service is stopped, disabled, and marked for deletion, requiring a reboot.
        }
        else
        {
            // Using:   S2
            // Result:  The service is not stopped, disabled, or marked for deletion - still installed post-reboot.
        }
    }
}
else // serviceIsRunning == false
{
    if "sc delete"
    {
        // Using:   S3, SWI3
        // Result:  The service is deleted - no reboot required.
    }
    else // "installutil /u"
    {
        if serviceImplementsInstaller
        {
            // Using:   SWI4
            // Result:  The service is deleted - no reboot required.
        }
        else
        {
            // Using:   S4
            // Result:  The service is not disabled or marked for deletion - still installed post-reboot.
        }
    }
}

这些结果与sc delete自己的文档一致:

C:\Windows\system32>sc delete
DESCRIPTION:
        Deletes a service entry from the registry.
        If the service is running, or another process has an
        open handle to the service, the service is simply marked
        for deletion.
USAGE:
        sc <server> delete [service name]

installutil /u's 相同:

C:\Windows\system32>installutil /u
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Usage: InstallUtil [/u | /uninstall] [option [...]] assembly [[option [...]] assembly] [...]]

InstallUtil executes the installers in each given assembly.
If the /u or /uninstall switch is specified, it uninstalls
the assemblies, otherwise it installs them.

请注意,当您installutil /u在未实现(卸载)安装程序的服务上使用时,其输出中会出现以下消息:

找不到具有 RunInstallerAttribute.Yes 属性的公共安装程序

于 2013-04-30T06:29:59.187 回答