【问题标题】:Restart a service on Win 8.1 64 with vbs script使用 vbs 脚本在 Win 8.1 64 上重新启动服务
【发布时间】:2014-12-12 08:55:07
【问题描述】:

Synergy 1.6.2 似乎与我使用的另一个实用程序 (ActualTools) 发生冲突,当我等待他们找出是谁的错时,一个简单的解决方法是在出现问题时重新启动 Synergy 服务。为了节省每次都手动执行此操作,我改编了this 脚本。在调用它时,我只是从 StopService 函数中得到“错误 2”——它对应于“用户没有必要的访问权限”。这看起来很奇怪,因为我可以运行compmgmt.msc(就像我登录的身份,而不是管理员身份)并在那里手动重新启动服务,而不会出现任何问题或请求额外的凭据。但是,我不能在命令行中使用net stop Synergy - 我得到系统错误 5(访问被拒绝)。所以,我想这与权限/特权有关。

我尝试过修改该脚本中的这一行:

Set cimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

通过引用特权列表here - 例如,将(Tcb) 添加到安全名字对象。但是我不确定那是正确的方法,而且我无论如何也找不到与服务管理相对应的权限。

那么,在这种情况下,有没有办法通过脚本获得正确的权限来停止/启动服务?作为参考,这里是完整的脚本:

Dim cimv2, oService, Result

'Get the WMI administration object    
Set cimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

'Get the service object
Set oService = cimv2.Get("Win32_Service.Name='Synergy'")

If Not oService.Started Then
    ' the service is Not started
    wscript.echo "Synergy is not started"
    wscript.quit
End If

If Not oService.AcceptStop Then
    ' the service does Not accept stop command
    wscript.echo "Synergy does not accept stop command"
    wscript.quit
End If

'Stop the service
Result  = oService.StopService
If 0 <> Result Then
    wscript.echo "Stop Synergy error: " & Result
    wscript.quit  
End If 

Do While oService.Started And Wait
    'get the current service state
    Set oService = cimv2.Get("Win32_Service.Name='Synergy'")
    Wscript.Sleep 200
Loop   

If oService.Started Then
    ' the service is Not started
    wscript.echo "Synergy is already running."
    wscript.quit 
End If

'Start the service
Result = oService.StartService
If 0 <> Result Then
    wscript.echo "Start Synergy error:" & Result
    wscript.quit  
End If 

Do While InStr(1,oService.State,"running",1) = 0 And Wait 
    'get the current service state
    Set oService = cimv2.Get("Win32_Service.Name='Synergy'")
    Wscript.Sleep 200
Loop   

【问题讨论】:

    标签: vbscript windows-services wmi wsh


    【解决方案1】:

    我假设您已激活 UAC,并希望自动启动此脚本,而不仅仅是手动运行一次。

    据我所知,无法从脚本中获得完全权限。您可以触发 UAC 提升提示,但仍需要手动确认。

    让这个脚本以所有权限自动运行的最简单方法是使用任务计划程序。

    您可以在此处创建在用户登录时运行的计划任务。在底部附近的“常规”选项卡中,您可以指定“以最高权限运行”选项,这足以授予您所需的权限。

    【讨论】:

    • 我将 UAC 设置为“从不通知”,并且我的登录帐户是管理员组的成员。我偶尔会收到类似“您需要提供凭据才能执行此操作”的提示,但在单击“继续”按钮(带有黄色/蓝色盾牌)时,无论我是什么试图做的只是继续而不要求任何进一步的凭据。我总是把它归结为 UAC 的怪异,但也许有些东西坏了......无论如何,我会给任务调度程序一个机会 - 感谢您的指针,我会在测试时回复。跨度>
    • 不会改变你需要先提升的事实。如果您在以具有必要权限的用户身份运行命令时收到“拒绝访问”,那么您在启动 cmd.exe/powershell.exe/任何其他无法提示提升的工具时跳过“以管理员身份运行”部分的可能性很高当你需要的时候。
    • 我遇到了同样的问题,并确认即使我的用户在管理员组中,我的 vbScript 在尝试启动或停止服务时也会出现错误=2。我的 UAC 设置也设置为从不。 以管理员身份打开 cmd 并运行脚本确实有效。
    猜你喜欢
    • 1970-01-01
    • 2019-03-03
    • 2018-01-08
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    相关资源
    最近更新 更多