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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stop Service, Copy File, Start Service

Status
Not open for further replies.

mefanning

MIS
Apr 26, 2010
19
US
Hello,

I am attempting to use VBScript to import a list of servers, stop a service, copy a replacement exe, then restart the service.

I am almost there, however the service is failing to restart once the file is copied.

Any help is appreciated.

On Error Resume Next

Const ForAppending = 8
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\list.txt",1)

Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service where Name ='SISIDSService'")

For Each objService in colListOfServices
objService.StopService()
Next

'Copy File
objFSO.CopyFile "C:\file.exe" , "C:\Windows\" , OverwriteExisting

Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service where Name ='SISIDSService'")

For Each objService in colListOfServices
objService.StartService()
Next
Loop
 
objFSO.CopyFile "C:\file.exe" , "C:\Windows\" , OverwriteExisting

this line and objFSO i guess has no idea about the target machine, e.g. strComputer. i would suggest you need to update the target path with something to do with strComputer.

also, be careful with the .StopService method, i am not so sure this is a true blocked method call
 
see if this helps

Code:
Option Explicit
Dim WshShell:Set WshShell = Wscript.CreateObject("WScript.Shell")
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, SleepTime
Dim s, d, file
Dim WinDir:Set WinDir = objFSO.GetSpecialFolder(0)
Dim SysDrive:SysDrive = WshShell.ExpandEnvironmentStrings("%SystemDrive%")

Const OverwriteExisting = True
strComputer = "."
SleepTime = 20000
strService = " 'BITS' "

s = SysDrive & "\"
d = WinDir & "\"
file = "proxyaddresses.txt"

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
	If objService.State="Running" Then 
	objService.StopService()
		Call CopyFile
			WSCript.Sleep SleepTime
	objService.StartService()
	End IF
Next
WScript.Quit


Sub CopyFile
	objFSO.CopyFile (s & file) , (d & file), OverwriteExisting
End Sub

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
Thanks everyone. It appears the underlying issue was my pause not long enough for the service to complete stopping. Once I increased to 20000 everything works.


 
hmm, a more robust way would be to check the status of the service you asked to stop, and once it has really stopped then you can over write the file.

the use of Wscript.Sleep is only for gun slingers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top