Restart spooler.
This script is an example for stopping and starting services. In this script the print spooler is restarted on the local computer. To restart the service on a remote computer, call the function with : StopService( servicename, \\computername). To start the script type:
cscript restartspooler.vbs c:\
'***************************************************************************
'** Script: RestartSpooler.vbs
'** Version: 1.0
'** Created: 24-8-2009 20:52
'** Author: Adriaan Westra
'** E-mail:
'** Purpose / Comments:
'** Start and stop services
'**
'**
'** Changelog :
'** 24-8-2009 20:52 : Initial version
'**
'***************************************************************************
Dim Version : Version = "1.0" ' Script version
Dim Author : Author = "A. Westra"
Dim Copyright : Copyright = "©"
Dim Website : Website = "http://www.westphil.nl"
Dim Email : Email = ""
'***************************************************************************
'** Add Error Handling
on error resume Next
'***************************************************************************
'** Make sure the script is started with cscript
If InStr(wscript.FullName, "wscript.exe") > 0 Then
MsgBox "Please run this script with cscript.exe." & Chr(13) & _
"For example : cscript " & WScript.ScriptName & " /?", _
vbExclamation, WScript.ScriptName
WScript.Quit(1)
End If
'***************************************************************************
'** Get commandline parameters
Set Args = Wscript.Arguments
If Args.Count <> 0 Then
DisplayHelp
Wscript.quit(0)
End if
'***************************************************************************
'** Start script
Wscript.Echo "Spooler : " & StopService( "spooler", "")
Wscript.Echo "Spooler : " & StartService( "spooler", "")
'***************************************************************************
'** End script
Wscript.Quit()
'***************************************************************************
'** Sub: DisplayHelp
'** Version: 1.0
'** Created: 24-03-2003 8:22
'** Author: Adriaan Westra
'** E-mail:
'**
'** Purpose / Comments:
'** Display help for script
'**
'** Arguments :
'**
'** Wijzigingslog :
'** 24-03-2003 8:22 : Initiële versie
'**
'***************************************************************************
Sub DisplayHelp()
strComment = string(2,"*")
strCmntLine = String(79, "*")
wscript.echo strCmntline
wscript.echo strComment
wscript.echo strComment & " Online help for " & _
Wscript.scriptname & " version : " & Version
wscript.echo strComment
wscript.echo strComment & " Usage : cscript " & _
Wscript.scriptname
wscript.echo strComment
wscript.echo strComment & " Purpose : Stop and start " & _
"spooler service"
wscript.echo strComment
wscript.echo strComment & " Author : " & Author
wscript.echo strComment & " E-mail : " & Email
wscript.echo strComment
wscript.echo strCmntline
End Sub
'***************************************************************************
'** Function: StopService
'** Version: 1.0
'** Created: 24-8-2009 20:52
'** Author: Adriaan Westra
'** E-mail:
'**
'** Purpose / Comments:
'** Stop service
'**
'** Arguments :
'** strServiceName : name of the service to stop
'** strComputer : Name of the computer where to stop the service,
'** Blank for local computer.
'**
'** Returns : State of the service.
'**
'** Wijzigingslog :
'** 24-8-2009 20:52 : Initiële versie
'**
'***************************************************************************
Function StopService(strServiceName, strComputer)
Set objWMI = GetObject("winmgmts:" & strComputer)
Set objService = objWmi.Get("Win32_Service='" & strServiceName & "'")
objService.stopService
WScript.StdOut.Write( "Stopping " & strServiceName)
For i = 1 to 1000
Set objService = objWmi.Get("Win32_Service='" & strServiceName & "'")
WScript.StdOut.Write(".")
WScript.Sleep(100)
if objService.State = "Stopped" Then
WScript.StdOut.Write(vbCrLf)
StopService = objService.State
exit for
end if
Next
StopService = objService.State
End Function
'***************************************************************************
'** Function: StartService
'** Version: 1.0
'** Created: 24-8-2009 20:52
'** Author: Adriaan Westra
'** E-mail:
'**
'** Purpose / Comments:
'** Start service
'**
'** Arguments :
'** strServiceName : name of the service to start
'** strComputer : Name of the computer where to start the service,
'** Blank for local computer.
'**
'** Returns : State of the service.
'**
'** Wijzigingslog :
'** 24-8-2009 20:52 : Initiële versie
'**
'***************************************************************************
Function StartService(strServiceName,strComputer)
Set objWMI = GetObject("winmgmts:" & strComputer)
Set objService = objWmi.Get("Win32_Service='" & strServiceName & "'")
objService.startService
WScript.StdOut.Write( "Starting " & strServiceName)
For i = 1 to 1000
Set objService = objWmi.Get("Win32_Service='" & strServiceName & "'")
WScript.StdOut.Write(".")
WScript.Sleep(100)
if objService.State = "Running" Then
WScript.StdOut.Write(vbCrLf)
StartService = objService.State
Exit for
end if
next
StartService = objService.State
End Function