0
votes

Synergy 1.6.2 seems to conflict with another utility I use (ActualTools), and whilst I wait for them to fight it out for who's fault it is, a simple workaround is to restart the Synergy service whenever the problem occurs. To save doing this by hand every time, I adapted this script. On calling it, I just get 'Error 2' from the StopService function - which corresponds to 'The user did not have the necessary access.' This seems odd, because I can run compmgmt.msc (just as who I log in as, not as administrator) and restart the service manually there without any problems or requests for additional credentials. However, I can't use net stop Synergy from the command line - there I get System error 5 (Access is denied). So, I guess it is something to do with permissions / privileges.

I've tried fiddling with this line from that script:

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

by referring to the list of privileges here - for example, adding (Tcb) to the security moniker. But I'm not sure that's the right way to go, and I couldn't find a privilege corresponding to service management anyway.

So, is there a way to get the correct privileges to stop / start a service via a script in this situation? For reference, here's the full script at the moment:

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   
1

1 Answers

1
votes

I'm assuming you have UAC activated and want to automatically start this script and not just run it once manually.

It is as far as I know not possible to gain full privileges from within the script. You can trigger the UAC elevation prompt but it would still require manual confirmation.

The easiest way to get this script to automatically run with all privileges is using the Task Scheduler.

There you can create a scheduled task that runs at user logon. In the "General" tab near the bottom you can specify the option "Run with highest privileges" which should suffice to grant you the rights you need.