1.    20 Mar 2017 #1
    Join Date : Dec 2016
    Posts : 5
    Windows 10

    Bat or Exe file to restart Computer Browser Service


    About 1 out of every 6 times I turn my laptop on I have to restart the computer browser service to be able to connect to networks. Is there a bat or exe file I can create that I can just click on and run to restart the service instead of having to go into the services/computer browser area to restart each time this happens? Thanks.
      My ComputerSystem Spec
  2.    21 Mar 2017 #2
    Join Date : Mar 2015
    Sydney, NSW
    Posts : 93
    Windows 10

    You should really get to the root of the problem, but since this is a good opportunity to brush up on my scripting I’m going to assume you’ve verified the service’s settings are regular and that you’ve exhausted your options, so here are your suggestions in fruition.

    Batch (.bat)

    Code:
    @echo off
    
    set "service=MyServiceName"
    
    >nul sc query "%SERVICE:"=%"
    if errorlevel 1 (
        if errorlevel 1060 (
            >&2 echo Cannot find any service with service name '"%SERVICE:"=%"'
        ) else (
            >&2 echo Error
        )
        exit /b 1
    )
    
    sc query "%SERVICE:"=%" | find "STATE" | findstr "2 4" >nul
    if errorlevel 1 (
        >nul sc start "%SERVICE:"=%"
        if errorlevel 1 (
            >&2 echo Error starting service
            exit /b 1
        )
    ) else (
        >nul sc stop "%SERVICE:"=%"
        if errorlevel 1 (
            >&2 echo Error stopping service
            exit /b 1
        )
        >nul pathping -n -p 220 -q 1 127.0.0.1
        >nul sc start "%SERVICE:"=%"
    )

    C# (.exe)

    PHP Code:
    using System;
    using System.Windows.Forms;
    using System.ServiceProcess;

    class 
    Program
    {
        static 
    void Main(string[] args)
        {
            
    ServiceController service = new ServiceController("MyServiceName");

            if ((
    service.Status & (ServiceControllerStatus.Running ServiceControllerStatus.StartPending)) == 0)
            {
                try
                {
                    
    service.Start();
                }
                catch (
    Exception e)
                {
                    
    MessageBox.Show(e.Message);
                }
            }
            else
            {
                try
                {
                    
    service.Stop();
                    
    service.WaitForStatus(ServiceControllerStatus.Stopped);
                    
    service.Start();
                }
                catch (
    Exception e)
                {
                    
    MessageBox.Show(e.Message);
                }
            }
        }


    VBScript (.vbs)

    Code:
    strService = "MyServiceName"
    
    Set objSWbemServices = GetObject("winmgmts:\\.\root\cimv2")
    
    Set colServices = objSWbemServices.ExecQuery("SELECT * FROM Win32_Service WHERE Name = '" & strService & "'")
    
    If colServices.Count = 0 Then
        WScript.Echo "Cannot find any service with service name '" & strService & "'"
    End If
    
    On Error Resume Next
    For Each svc In colServices
        If svc.State = "Running" Or svc.State = "Start Pending" Then
            result = svc.StopService
    
            If result <> 0 Then
                If result = 2 Then
                    WScript.Echo "Error stopping service: Access is denied"
                    WScript.Quit 1
                End If
    
                WScript.Echo "Error stopping service: " & Err.Description
                WScript.Quit 1
            End If
    
            WScript.Sleep 200
            svc.StartService
        Else
            svc.StartService
    
            If result <> 0 Then
                WScript.Echo "Error starting service: " & Err.Description
                WScript.Quit 1
            End If
        End If
    Next

    You should replace ‘MyServiceName’ with the name of your service (not to be confused with the service display name, e.g., ‘w32time’ instead of ‘Windows Time’).

    Many services can be started but cannot be stopped without administrative privileges. If you know or suspect this is the case with your service, you may need to run the program as administrator each time.
      My ComputerSystem Spec

 


Similar Threads
Thread Forum
Service is Started on Restart but Service Set to Disabled
Service is Started on Restart but Service Set to Disabled I am new to WIN10, and I find this behavior puzzling. I have set the service "Microsoft Office Click-to-Run Service" to Disabled because I can't stand using OneDrive and OneDrive (Office...
General Support
BSOD service system error on restart and random freeze in games
111543 Started getting random freeze while playing Dishonored 2. Then at restart it would blue screen crash.. any help would be appreciated. Nate
BSOD Crashes and Debugging
BSOD Critical Service Failure - Restart Loop
Hi everyone. I just turned on my computer this morning and got the bsod. I've surfed on different threads to try and solve my problem but nothing worked: - System restore - On the first attempt i got an error saying that windows could not find a...
BSOD Crashes and Debugging
Solved How can I restart Windows Search service?
Hello, I recently did windows updates, did the anniversary update among a few other updates. If you need a full list of all the updates I have done let me know. I want to re-enable windows index service. I need to have all of my documents, notes,...
General Support
I'm getting System Service Exception (asmtxhci.sys) on my computer...
I have been getting the BSOD for the last 3 days, for unbeknowst reason. I went from full Win 7 Ultimate, to Win 10 about 6 or 8 months ago. I didn't have any issues what-so-ever, until 3 days ago. I've taken off IOBit off my computer, checked...
BSOD Crashes and Debugging
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:55.
Find Us
Twitter Facebook Google+ Ten Forums iOS App Ten Forums Android App



Windows 10 Forums