Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to restart a service on a remote server

Status
Not open for further replies.

farooqrcr

Programmer
Sep 5, 2008
4
IN
Hi All,

I am looking at a script which can restart a microsoft service on a remote windows 2003 server. I should be able to execute the script from the comfort of my notebook, thus by entering all required information like username, password and domain name within the script.

Please let me know is there any script to do the above functionality. Thanks in advance.

Best Regds
Farooq
 
Hi All,

I got the below script which is working fine on my local machine, but same functionality i want to have on a remote server, ie if i click from my desktop , i should be able to stop/start the service on a remote server.

Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
strComputer = "computerName"
intSleep = 15000
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"
'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "
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
objService.StopService()
WSCript.Sleep intSleep
objService.StartService()
Next
WScript.Echo "Your "& strService & " service has Started"
WScript.Quit
' End of Example WMI script to Start / Stop services


your help greatly appreciated.

Best Regds
Farooq
 
If you copied this script to the remote server then you could use Remote Console to execute the script on that server.
 
strComputer = "computerName"
Isn't computerName the name of the remote server ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi DTracy

I have not copied the script on remote server, i want to run the script on a local machine which should restart a service on a remote server.

and strComputer = "computerName" is just a example code which i got and working fine if i give local computer name(ie my laptop), if i give the server name then i should give domain/username and password as well, i dont know how to connect to remote server using vbscript as this is not my core competency.

I am eagerly looking for a local script which should connect to a remote server(within network) and restart one particular service.(microsoft services).

Best Regds
Farooq
 
Launch the script as a domain administrator.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
One thing you've to check (apart from wmi being corrupted or ill-installed which warrant a re-installation) is to verify the default security policy of the win2003 as domain controller. Look at its "User Rights Assignment" setting. In the "Impersonate a client after authentication" policy, the setting must include the "SERVICE". Usually it should be included. If for some reason it is not, you have to add it back.
 
Hi everybody
I am trying to use same above script. But i have list of server where a particular service needs to be restarted.List of server provided in text file. How can i achieve this ?

 
i found a script a little while ago.. and did some modification .. I hope this can help ( You have to be domain admin.

Code:
'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2007
'
' NAME: 
'
' AUTHOR: DSI , TTC
' DATE  : 05/08/2008
'
' COMMENT: 
'
'==========================================================================

Dim strCredentials, arrCredentials, strUser, strPassword, strNamespace, strLocalComputer, strComputer, strService, objService

On Error Resume Next

Const WbemAuthenticationLevelPktPrivacy = 6
Const ForReading = 1

Function PingStatus(strComputer)

    On Error Resume Next
    strWorkstation = "."
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strWorkstation & "\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
      ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")
    For Each objPing In colPings
        Select Case objPing.StatusCode
            Case 0 PingStatus = "Success"
            Case 11001 PingStatus = "Status code 11001 - Buffer Too Small"
            Case 11002 PingStatus = "Status code 11002 - Destination Net Unreachable"
            Case 11003 PingStatus = "Status code 11003 - Destination Host Unreachable"
            Case 11004 PingStatus = _
              "Status code 11004 - Destination Protocol Unreachable"
            Case 11005 PingStatus = "Status code 11005 - Destination Port Unreachable"
            Case 11006 PingStatus = "Status code 11006 - No Resources"
            Case 11007 PingStatus = "Status code 11007 - Bad Option"
            Case 11008 PingStatus = "Status code 11008 - Hardware Error"
            Case 11009 PingStatus = "Status code 11009 - Packet Too Big"
            Case 11010 PingStatus = "Status code 11010 - Request Timed Out"
            Case 11011 PingStatus = "Status code 11011 - Bad Request"
            Case 11012 PingStatus = "Status code 11012 - Bad Route"
            Case 11013 PingStatus = "Status code 11013 - TimeToLive Expired Transit"
            Case 11014 PingStatus = _
              "Status code 11014 - TimeToLive Expired Reassembly"
            Case 11015 PingStatus = "Status code 11015 - Parameter Problem"
            Case 11016 PingStatus = "Status code 11016 - Source Quench"
            Case 11017 PingStatus = "Status code 11017 - Option Too Big"
            Case 11018 PingStatus = "Status code 11018 - Bad Destination"
            Case 11032 PingStatus = "Status code 11032 - Negotiating IPSEC"
            Case 11050 PingStatus = "Status code 11050 - General Failure"
            Case Else PingStatus = "Status code " & objPing.StatusCode & _
               " - Unable to determine cause of failure."
        End Select
    Next

End Function


strNamespace = "root\cimv2"

Set objPassword = CreateObject("ScriptPW.Password") 
Set objNetwork = CreateObject("Wscript.Network")
strLocalComputer = objNetwork.ComputerName
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFileRead = objFSO.OpenTextFile("c:\HQ_DEV.TXT", ForReading)

strUser = InputBox("Enter Username","Username")
'WARNING PASSWORD IN CLEAR TEXT
strPassword = InputBox("Enter password","password")


Do Until objFileRead.AtEndOfStream
    ToClean = objFileread.ReadLine
    strComputer = Replace(ToClean, " ", "")
    	
	strPingstatus = ""
	strPingstatus = PingStatus(strComputer)
				
	If strPingStatus = "Success" Then
	
		
		Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
		Set objWMIService = objwbemLocator.ConnectServer _
		(strComputer, strNamespace, strUser, strPassword)
		objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
		
		' =====================================================================
		'Step 4. List running services
	
		Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service")
	
		' =====================================================================
		'Step 5. User enters service name to be restarted
		'Enter the name of the service you wish to restart.
		strserviceName= "TSM_ACCEPTOR_"
		strService = strServiceName & strComputer
		If strService = "" Then
			WScript.Echo "No service name was given or you clicked Cancel"
			WScript.Quit
		End If
	
		'Step 6. Restart service
	
		RestartServices
		
	End if
Loop

Sub RestartServices()
'Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("ASSOCIATORS OF {Win32_Service.Name='" & strService & "'} WHERE AssocClass=Win32_DependentService Role=Antecedent")
For Each objService in colServiceList
WScript.Echo "Stopping: " & objService.DisplayName
intResult = objService.StopService()
WScript.Echo "Result: " & intResult
Next

WScript.sleep 10000

Set colServiceList = objWMIService.ExecQuery("Select * FROM Win32_Service WHERE Name='" & strService & "'")
For Each objService in colServiceList
WScript.Echo "Stopping: " & objService.DisplayName
intResult = objService.StopService()

'Step 7. Echo confirmatiom message that service stopped

WScript.Echo "Result: " & intResult

WScript.Sleep 10000
intResult = objService.StartService()

'Step 8. Echo confirmatiom message that service started

WScript.Echo "Starting: " & objService.DisplayName
WScript.Echo "Result: " & intResult
Next

End Sub

' =====================================================================
' End
' =====================================================================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top