【问题标题】:sc.exe config "Service Name" obj= "DOMAIN\User" password= "password" not workingsc.exe 配置“服务名称”obj=“域\用户”密码=“密码”不起作用
【发布时间】:2019-09-25 04:12:58
【问题描述】:

我想通过 cmd 为服务设置密码。我有选择

sc.exe config "服务名称" obj= "DOMAIN\User" 密码= "password"

当我执行时,它显示 "[SC] ChangeServiceConfig SUCCESS" 如果我启动服务 我得到了

“Windows 无法在本地计算机上启动 service1 服务。 错误 1069:由于登录失败,服务没有启动。”

我搜索并获得了以下链接 Using SC.exe to set service credentials password fails

我的密码不包含特殊字符。

有什么方法可以做到这一点?

【问题讨论】:

  • 您解决了这个问题吗?我遇到了同样的问题。

标签: windows batch-file com windows-services cmd


【解决方案1】:

首先要检查的是该用户是否有权在该计算机上作为服务登录。如果他这样做了(您可以执行以下程序来检查),只需转到服务(开始菜单 - 键入“服务”,不带引号)。在列表中找到您的服务,然后右键单击它。选择“属性”,然后转到“登录”选项卡。重新输入“密码”和“确认密码”。单击确定。如果您的用户确实有权作为服务登录,则会显示一条消息“帐户 YourDomain\YourUser 已被授予作为服务登录的权限”。只需尝试再次启动该服务,它就会工作。

如果您的用户没有此类权限,您可以使用以下两种方法之一:

1) 开始菜单 - 键入不带引号的“本地安全策略”。打开“本地策略”,然后左键单击“用户权限分配”。在右侧面板上,右键单击“作为服务登录”,然后选择“属性”。单击“添加用户或组”并添加您的用户。单击确定。您可能需要重新启动计算机。

2) 下载并安装“Windows Server 2003 资源工具包工具”(http://www.microsoft.com/en-us/download/confirmation.aspx?id=17657)。打开命令提示符并输入:

ntrights +r SeServiceLogonRight -u MyDomain\MyUser -m \\%COMPUTERNAME%

重新启动您的计算机并尝试再次启动该服务。

在您的用户被授予作为服务登录权限后,您可以通过命令行创建和启动服务。

【讨论】:

  • 调用ntrights后,不用重启电脑,调用“gpupdate /force”也足够了。我发现这对于创建不需要重新启动的脚本很有用。
  • 这是一个旧答案,但请注意,当您使用 services.msc 手动输入密码时,窗口会自动授予您作为服务登录的权限。所以你需要以不同的方式确保你拥有正确的权限..
【解决方案2】:

如果你遇到帐户YourDomain\YourUser已被授予作为服务登录权限,你应该执行powershell脚本链接 AddLogonasaService 这与您的密码无关。这是用户运行服务的权利/许可。

我嵌入了代码供您参考。您也可以引用该 URL。

param($accountToAdd)
 #written by Ingo Karstein, http://blog.karstein-consulting.com
 #  v1.0, 01/03/2014

 ## <--- Configure here

 if( [string]::IsNullOrEmpty($accountToAdd) ) {
    Write-Host "no account specified"
    exit
 }

 ## ---> End of Config

 $sidstr = $null
 try {
    $ntprincipal = new-object System.Security.Principal.NTAccount "$accountToAdd"
    $sid = $ntprincipal.Translate([System.Security.Principal.SecurityIdentifier])
    $sidstr = $sid.Value.ToString()
 } catch {
    $sidstr = $null
 }

 Write-Host "Account: $($accountToAdd)" -ForegroundColor DarkCyan

 if( [string]::IsNullOrEmpty($sidstr) ) {
    Write-Host "Account not found!" -ForegroundColor Red
    exit -1
 }

 Write-Host "Account SID: $($sidstr)" -ForegroundColor DarkCyan

 $tmp = [System.IO.Path]::GetTempFileName()

 Write-Host "Export current Local Security Policy" -ForegroundColor DarkCyan
 secedit.exe /export /cfg "$($tmp)" 

 $c = Get-Content -Path $tmp 

 $currentSetting = ""

 foreach($s in $c) {
    if( $s -like "SeServiceLogonRight*") {
        $x = $s.split("=",[System.StringSplitOptions]::RemoveEmptyEntries)
        $currentSetting = $x[1].Trim()
    }
 }

 if( $currentSetting -notlike "*$($sidstr)*" ) {
    Write-Host "Modify Setting ""Logon as a Service""" -ForegroundColor DarkCyan

    if( [string]::IsNullOrEmpty($currentSetting) ) {
        $currentSetting = "*$($sidstr)"
    } else {
        $currentSetting = "*$($sidstr),$($currentSetting)"
    }

    Write-Host "$currentSetting"

    $outfile = @"
 [Unicode]
 Unicode=yes
 [Version]
 signature="`$CHICAGO`$"
 Revision=1
 [Privilege Rights]
 SeServiceLogonRight = $($currentSetting)
 "@

    $tmp2 = [System.IO.Path]::GetTempFileName()


    Write-Host "Import new settings to Local Security Policy" -ForegroundColor DarkCyan
    $outfile | Set-Content -Path $tmp2 -Encoding Unicode -Force

    #notepad.exe $tmp2
    Push-Location (Split-Path $tmp2)

    try {
        secedit.exe /configure /db "secedit.sdb" /cfg "$($tmp2)" /areas USER_RIGHTS 
        #write-host "secedit.exe /configure /db ""secedit.sdb"" /cfg ""$($tmp2)"" /areas USER_RIGHTS "
    } finally { 
        Pop-Location
    }
 } else {
    Write-Host "NO ACTIONS REQUIRED! Account already in ""Logon as a Service""" -ForegroundColor DarkCyan
 }

 Write-Host "Done." -ForegroundColor DarkCyan

为了设置服务的身份,我使用了一个 vbscript

Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name = 'Servicename'")
For Each objservice in colServiceList   
errReturn = objService.Change( , , , , , ,WScript.Arguments.Item(0),   WScript.Arguments.Item(1)) 
objService.StartService()   
Next

其中 WScript.Arguments.Item(0) 是用户名 arg,WScript.Arguments.Item(1) 是密码。

【讨论】:

【解决方案3】:

问题可能在于它不希望在密码周围加上引号。用户名也是如此。

它可能无法判断引号是否是密码的一部分。

也可能是因为给定帐户没有被授予“作为服务登录”权限。

通常您应该检查安全事件日志,它会给出登录失败的原因。

【讨论】:

  • 我删除了引号。还是一样的问题。
  • @Earnest,该帐户是否有权作为服务登录?你检查过事件日志吗?
  • 该用户有权作为服务登录。 事件日志 service1 服务因以下错误而无法启动:该服务因登录失败而未启动。
  • 这是系统事件日志。安全事件日志呢?
  • 确保您让用户“作为服务登录”(本地政策)。
【解决方案4】:

这对我有用:

sc.exe stop "<my_service>" 4:4:3
sc.exe config "<my_service>" obj= "./<local_acc_name>" password= "<local_acc_pass>"
sc.exe start "<my_service>"

所以,简而言之: 在配置密码和启动之前停止服务可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多