none
VBScript - Windows 7 - Remotely Stop / Start a Service

    Question

  • Source: WinXP.
    Target: Win7.
    Full Admin rights on both machines via a Windows domain login ID added to both Local Admin groups.

    Need to STOP the LanManServer Service on the Windows 7 machine from the WinXP machine via VBS.

    Although this code works against other flavors of Windows, this is not working against the Win 7 machine.

    Any help is appriciated.
    ESP


    strComputer = "aaaaa"
    strService = " 'lanmanserver' "
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name =" & strService & " ")
    For Each objService In colListOfServices
    WSCript.Sleep 1500
    objService.StopService()
    Next


    www.HornyToads.biz
    Monday, October 26, 2009 7:33 PM

Answers

  • Thx for the input, but solved it myself.

    In previous OS's you could simply stop/start a service and the dependencies would simply follow suit. In the case of Win7, you MUST address the dependencies first then go after the target service.

    Here's the code:

    ESP


    strComputer = "aaaaaaa"

    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set objLanManServer = objWMIService.Get("Win32_Service.Name='LanManServer'")

    Set colServiceList = objWMIService.ExecQuery("Associators of " _
        & "{Win32_Service.Name='LanManServer'} Where " _
            & "AssocClass=Win32_DependentService " & _
          "Role=Antecedent" )

    For Each objService in colServiceList
    objService.StopService()   
    Next

    WScript.Sleep 20000
    Return = objLanManServer.StopService()

    WScript.Sleep 10000
    Return = objLanManServer.StartService()

    For Each objService in colServiceList
    objService.StartService()   
    Next


    www.HornyToads.biz
    • Marked as answer by YellowRockyGuy Tuesday, October 27, 2009 9:28 PM
    Tuesday, October 27, 2009 9:28 PM

All replies

  • I don't have access to Win 7, but in general problems stated as 'this is not working' are very hard to troubleshoot.  I think you'll get a quicker/better response if you describe how it fails - error message?  empty set? What?
    Tom Lavedas
    Monday, October 26, 2009 8:36 PM
    Moderator
  • Executing the script you provided, the service(s) are retrieved ok, however the StopService command fails.

    Letting the script write out the exit code for the command, will let us know what went wrong:
    Result = objService.StopService()
    WScript.Echo "Return value: " & Return

    When ran in the above script (on Windows 7), the StopService() command gives us an exit code of 2, translating to Acces Denied ( http://msdn2.microsoft.com/en-us/library/aa393673.aspx )

    Why the impersonation fails? or if something else is needed? I don't know :/ but I hope someone else will enlighten us :)
    Monday, October 26, 2009 10:57 PM
  • Claus has the error correct. Sorry for the lacking info.

    My 'guess' is the Win7 UAC is getting in the way, but turning it off in this environment is not an option. Considering that Stop/Start a service is such a common job in VBS, I was hoping someone else has already overcome this issue.

    It is correct, the impersonation does not allow access on Win7 the same way that XP used to.
    Although full admin access is on both boxes, it is common to go the extra steps to get around the UAC on Win7. But I don't know how to get from the code above to that point of getting around it when dealing with Services.

    Moving to powerscript is not an option either (yet).

    Thx !!!
    ESP


    www.HornyToads.biz
    Tuesday, October 27, 2009 4:56 PM
  • Thx for the input, but solved it myself.

    In previous OS's you could simply stop/start a service and the dependencies would simply follow suit. In the case of Win7, you MUST address the dependencies first then go after the target service.

    Here's the code:

    ESP


    strComputer = "aaaaaaa"

    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set objLanManServer = objWMIService.Get("Win32_Service.Name='LanManServer'")

    Set colServiceList = objWMIService.ExecQuery("Associators of " _
        & "{Win32_Service.Name='LanManServer'} Where " _
            & "AssocClass=Win32_DependentService " & _
          "Role=Antecedent" )

    For Each objService in colServiceList
    objService.StopService()   
    Next

    WScript.Sleep 20000
    Return = objLanManServer.StopService()

    WScript.Sleep 10000
    Return = objLanManServer.StartService()

    For Each objService in colServiceList
    objService.StartService()   
    Next


    www.HornyToads.biz
    • Marked as answer by YellowRockyGuy Tuesday, October 27, 2009 9:28 PM
    Tuesday, October 27, 2009 9:28 PM