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

Stop service

Status
Not open for further replies.

TheALL

Technical User
Jul 19, 2004
14
IT
hi
i do a simple vbscript to stop a service and have a simple log out.
It work but not properly.
Infact if service exist and it can be stopped ,in log file i read "Service Messenger Stopped",and all work well
but if not exist or it not stopped... the script finish and i read nothing in log file.

Why?
can tell me where is the error,so i can read "service Not stopped"?
Maybe i must insert a pause for wait response of negative stop?

this is the script
**********************************************************
strOutputFile="C:\shutdown.log"
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
If Err.Number then
Print "Error 0x" & CStr(Hex(Err.Number)) & _
" occurred in opening a filesystem object."
If Err.Description <> "" Then
Print "Error description: " & Err.Description & "."
End If
End If
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, Forappending, True)
If Err.Number then
Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in opening file " _
& objOutputFile
If Err.Description <> "" Then
Print "Error description: " & Err.Description & "."
End If
End If
on error resume next

StrComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = 'Messenger'")
For Each objService in colListOfServices
objService.StopService()
if err.number<>0 then
objOutputFile.Writeline "Service Messenger Not stopped"
else
objOutputFile.Writeline "Service Messenger Stopped"
end if
next

WScript.Echo "Finished"
WScript.Quit(0)
*********************************************
 
Hello TheAll,

The stopservice method return code indicating whether the operation is successful or not and for what reason.
documentation said:
Return Values
Returns one of the values in the following table or any other value to indicate an error.

Return code Description
0 Success.
1 Not supported.
2 Access denied.
3 Dependent services running.
4 Invalid service control.
5 Service cannot accept control.
6 Service not active.
7 Service request timeout.
8 Unknown failure.
9 Path not found.
10 Service already stopped.
11 Service database locked.
12 Service dependency deleted.
13 Service dependency failure.
14 Service disabled.
15 Service logon failed.
16 Service marked for deletion.
17 Service no thread.
18 Status circular dependency.
19 Status duplicate name.
20 Status - invalid name.
21 Status - invalid parameter.
22 Status - invalid service account.
23 Status - service exists.
24 Service already paused.
To control the state of the affair, check the return code rather than runtime error (err object).
Code:
For Each objService in colListOfServices
iret=objService.StopService()
if iret<>0 then
objOutputFile.Writeline "Service Messenger Not stopped. Return code : " & iret
else
objOutputFile.Writeline "Service Messenger Stopped"
end if
next
regards - tsuji
 
personally i prefer the adsi method of service manipulation. i think it looks neater


Set objComputer = GetObject("WinNT://" & WshNetwork.ComputerName & ",computer")
Set objService = objComputer.GetObject("service", "clisvc")
If objService.Status = 4 Then
objService.Stop
End If

or to iterate through services

'objComputer.Filter = Array("Service")
'For Each Service in objComputer
' If LCase(CStr(Service.Path)) = "c:\win\ms\sms\clicomp\hinv\hinv32.exe" Then
' Service.Start
' End If
'Next
 
TheALL,

What is the return code then? or do you mean return 0 but not stopping?

- tsuji
 
no return code it don't write anything, in this case messenger work but for other service don't work write in file log
 
TheALL,

>no return code

Add the following?
[tt]
wscript.echo "Return code : " & iret & vbcrlf & _
"Display name : " & objService.displayname & vbcrlf & _
"Status : " & objService.status & vbcrlf & _
"AcceptStop : " & objService.acceptstop
[/tt]
There are other properties which might be of interest, but start from here see what echo?

- tsuji
 
i find the trouble

Service name and display name.
my SO is in italian and i try to stop the service name and not the display name,so are different and the service can't stop because havn't that name :)
i rechange script

###########################################################
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strOutputFile="D:\shutdown.log"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, Forappending, True)

strComputer = "w91wxp122"
strService = "RasMan"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
strComputer & "\root\cimv2")

Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service " & _
"WHERE Name='" & strService & "'")
'*********For stop display service name*******
'"WHERE displayName='" & strService & "'")
'*********************************************
If colServices.Count = 0 Then
objOutputFile.writeLine strComputer & ",Service not installed."
Else
For Each objService In colServices
if objService.State = "Running" then
iret=objService.StopService()
if iret<>0 then
objOutputFile.Writeline "Service Not stopped. Return code : " & iret
else
objOutputFile.Writeline "Service Stopped"
end if
else
objOutputFile.writeLine objService.SystemName & "," & _
strService & "," & _
objService.StartMode & "," & _
objService.State
end if
Next
End If

WScript.Echo "Finished shutdown"
WScript.Quit(0)
##########################################################


now work perfectly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top