【问题标题】:Installing a windows service on remote machine using given username使用给定的用户名在远程机器上安装 Windows 服务
【发布时间】:2009-07-21 12:54:31
【问题描述】:

在远程机器上安装用 C# 编写的 Windows 服务(以标准方式)的最佳方法是什么,我需要提供它应该运行的用户名和密码?

作为集成测试的一部分,我将从 MSBuild 运行它。

编辑:我没有 msi,也不想创建一个。

【问题讨论】:

    标签: c# .net msbuild windows-services


    【解决方案1】:

    您可以使用SC 命令。

    sc.exe \\remotecomputer create newservice binpath= C:\Windows\System32\Newserv.exe start= auto obj= DOMAIN\username password= pwd

    (注意等号后面的空格很重要)

    Creates a service entry in the registry and Service Database.
    SYNTAX: 
    sc create [service name] [binPath= ] <option1> <option2>...
    CREATE OPTIONS:
    NOTE: The option name includes the equal sign.
     type= <own|share|interact|kernel|filesys|rec>
           (default = own)
     start= <boot|system|auto|demand|disabled>
           (default = demand)
     error= <normal|severe|critical|ignore>
           (default = normal)
     binPath= <BinaryPathName>
     group= <LoadOrderGroup>
     tag= <yes|no>
     depend= <Dependencies(separated by / (forward slash))>
     obj= <AccountName|ObjectName>
           (default = LocalSystem)
     DisplayName= <display name>
     password= <password> 
    

    【讨论】:

    • 看起来很有希望,但我必须为此付费吗?
    • 它包含在 Windows 中,可能在 Resource Kit 中,启动命令提示符并键入 sc 以查看它是否已安装。
    • 请注意 - 如果从 powershell 运行它,它是 sc.exe 而不是 sc。 sc 是 Powershell 中 Set-Content 的别名。只是一个小问题
    • 在传递带有特殊字符的复杂密码时要小心,因为即使密码周围的双引号也无法拯救您,stackoverflow.com/questions/10296162/…
    【解决方案2】:

    从 Powershell 调用的 WMI 调用的 Installutil 是一种方法。

    【讨论】:

      【解决方案3】:

      我们曾经使用PsExec 在远程机器上做所有事情。 这时候我看到另一个解决方案叫PowerShell Remoting,但我自己没有尝试过。

      【讨论】:

        【解决方案4】:

        可能值得您查看我编写的一个实用程序,该实用程序可让您在本地或远程计算机上安装/卸载/启动/停止 Windows 服务。如果您有需要添加的功能,请告诉我(评论/github 问题或 PM,我很乐意提供)。

        ServiceConfigurator on GitHub

        正如@Raj More 建议的那样,它使用从 .NET 调用的 WMI。

        【讨论】:

        • 出于好奇,它和SC有什么不同?
        猜你喜欢
        • 1970-01-01
        • 2011-03-19
        • 1970-01-01
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        • 2010-09-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多