Re: Stop / start service by Ratty
Ratty
Thu Jul 31 07:21:59 CDT 2003
Please realise that I am a newcomer and don't know what I'm doing .... however, I might
need something like this myself one day.
This is what I had to do to get MY script to work on a Win2K Pro workstation (there MUST
be a better solution):
Option Explicit
Dim objServiceSet, Service
Dim Debug
Set objServiceset = GetObject("winmgmts:").InstancesOf ("Win32_Service")
Debug = False
For Each Service in objServiceSet
if (Service.Name = "Spooler") AND (Service.State = "Running") Then
DMB( "First loop: " & Service.Name & ": " & Service.State )
Service.StopService
Exit For
End If
Next ' Service
' refresh
Set objServiceSet = Nothing
Set objServiceSet = GetObject("winmgmts:").InstancesOf ("Win32_Service")
For Each Service in objServiceSet
if (Service.Name = "Spooler") Then
DMB( "Second loop: " & Service.Name & ": " & Service.State )
Do While Service.State <> "Stopped"
Loop
Exit For
End If
Next ' Service
Service.StartService
' refresh again
Set objServiceSet = Nothing
Set objServiceSet = GetObject("winmgmts:").InstancesOf ("Win32_Service")
For Each Service in objServiceSet
If (Service.Name = "Spooler") Then
DMB( "Third loop: " & Service.Name & ": " & Service.State )
Do While Service.State <> "Running"
Loop
Exit For
End If
Next ' Service
DMB( "Finished: " & Service.Name & ": " & Service.State )
Set objServiceset = Nothing
Set Service = Nothing
' *************************************************************************************
Sub DMB( xWhat )
' DebugMessageBox
If Debug Then
MsgBox xWhat, 0, "Debug Message"
WriteLog( xWhat )
End If
End Sub
' *************************************************************************************
Sub WriteLog( xWhat )
' function that writes results to the log.txt
Dim LogFile, Fso
Logfile = Wscript.Name & ".log"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set LogFile = Fso.OpenTextFile(LogFile, 8, True)
LogFile.WriteLine( xWhat )
LogFile.Close
Set Fso = Nothing
Set LogFile = Nothing
End Sub
"Fons Reijsbergen" <fons.reijsbergen@kb.nl> wrote in message
news:bgac93$994$1@news.surfnet.nl...
> Hello,
>
> I want to do something like this:
>
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> Set objServiceset = GetObject("winmgmts:").InstancesOf ("Win32_Service")
> for each Service in objServiceset
> if (Service.Name = NameService) AND (Service.State = "Running") then
> Service.stopService
> do while Service.State <> "Stopped"
> Wscript.sleep 1000
> loop
> Service.startService
> do while Service.State <> "Running"
> Wscript.sleep 1000
> loop
> End If
> next
> Set objServiceset = Nothing
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>
> This script stops the Service, but it seems that the Service.State is not
> changing. So the script never start the service again. Why is this and what
> must I do to get this script working, so the service is stopt and started.
>
>
> F.Reijsbergen
> Koninklijke Bibliotheek
> fons.reijsbergen@kb.nl
>
>
>
>