2023年2月14日 星期二

RestartService

 Public Shared Function RestartService(ByVal ServiceName As String) As Boolean

    Dim mySC As ServiceProcess.ServiceController

    Try

        mySC = New ServiceProcess.ServiceController(ServiceName)

        mySC.Stop()

        mySC.Start()

        Return mySC.Status = ServiceProcess.ServiceControllerStatus.Running

    Catch

        Return False

    End Try

End Function



' restart without bothering about success or failure

RestartService("MSSQLSERVER")


'or like this if you are too concerned ;)

If RestartService("MSSQLSERVER") Then

    MsgBox("Service restarted")

Else

    MsgBox("Unable to restart the service.")

End If


----------------------------------------

@ECHO OFF


net stop ServiceName

net start ServiceName


沒有留言:

張貼留言