【问题标题】:how to set windows service username and password through commandline如何通过命令行设置windows服务用户名和密码
【发布时间】:2016-04-11 22:41:37
【问题描述】:

使用sc命令我们可以查询、启动、停止windows服务。
例如:

sc query "windows service name"

sc config命令改变了服务的配置,但是不知道怎么用。

谁能告诉我如何为任何 Windows 服务设置用户名和密码?

【问题讨论】:

    标签: windows service command-line


    【解决方案1】:

    这行得通:

    sc.exe config "[servicename]" obj= "[.\username]" password= "[password]"
    

    每个 [括号] 项目都替换为真正的参数。 (保留引号,但不要保留括号。)

    请记住:

    • 上述示例中的间距很重要。 obj= "foo" 是正确的; obj="foo" 不是。
    • '.'是本地计算机的别名,您可以根据需要指定一个域(或您的本地计算机名称)。
    • 在服务启动之前不会验证密码
    • 如上所述,引用您的参数。有时您可以不加引号,但祝您好运。

    【讨论】:

    • Andrew ,它进行了细微的更改。 obj= "domain\username" obj= "LocalSystem" 我们必须在 obj= 和用户名之间有空格。 userid 的验证是好的。我们必须在密码=和实际密码之间有空格。密码未经验证,这有点痛苦。要将服务重新设置为具有密码的 LocalSystem,我们提供了一些虚拟密码以使其工作。比如 sc config "servicename" obj="LocalSystem" password="notused"
    • 当我尝试在 PowerShell 中运行此命令时,出现如下错误:“Set-Content:找不到接受参数 'obj=' 的位置参数。”我使用 CMD 解决了这个问题:cmd.exe -e "sc config Service obj= user password= pass"
    • 这也不起作用。 SC config service obj= "service name" password= "password" 没有正确设置密码——因此,服务不能开始了。
    • @Jeremy 在 powershell 中,“sc”是 Set-Content 的别名。改用& sc.exe,不会有冲突。
    • 如果服务已经设置了“与桌面交互”,您将收到错误代码 87。要解决此问题:sc.exe config "ServiceName" obj= "USER@DOMAIN" password= "MyPassword" TYPE= own (support.microsoft.com/en-us/kb/264663)
    【解决方案2】:

    在 PowerShell 中,“sc”命令是 Set-Content cmdlet 的别名。您可以使用以下语法解决此问题:

    sc.exe config Service obj= user password= pass
    

    指定 .exe 扩展名,PowerShell 会绕过别名查找。

    HTH

    【讨论】:

    • 它对我不起作用。我想知道这个方法是否支持不同于字母数字的其他字符。
    • 也可以在 sc 命令行前面敲一个“cmd /c”,让 PowerShell 在 CMD 中运行它
    猜你喜欢
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2021-11-29
    • 2017-08-17
    • 2018-02-07
    • 2018-12-08
    • 2021-05-18
    • 2014-01-01
    相关资源
    最近更新 更多