Skip to main content

You might wonder how a windows script host app like this could possibly be of use.  Well it has many uses like checking a mail server queue and restarting a service that may have hung.  Or a web development application that allows clients to upload files for print projects but no body ever goes out and cleans off the files.  With a little modification to this you could pull off many scheduled tasks to fit your needs.  I wrote this because google failed me in the search for a quick fix, it’s not often but it does happen. Anyway if this saves you some time then great, mission accomplished. The code below checks a specified directory and counts the total files in it;  If the count is over our threshold then take action by restarting a service that is hung.  Finally send an email with all the fun details to my cell phone.

This has a bunch of unnecessary code in it for debugging, feel free to clean it up.
1. Set your SMTP  address for the email function
2. Set nMax – the threshold before we run an event
3. Uncomment out the echos if you want to see it run manually
4. Setup task scheduler to run the .vbs file at some interval
5. This is tested and runs spiffy on win2003 server but should run on anything win

Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
Dim n, SYSDATA, SYSEXPLANATION, nMax, strPath, bIncludeDirsInCount, objMessage
'On Error Resume Next 'optional '--------------------------------------------------------------
'COUNT FILES in specified DIR
'--------------------------------------------------------------
'setup variables
strPath = "\localhostd$exchangemail_incoming" '_incoming dir path?
bIncludeDirsInCount = false 'count dir as a file?
nMax = 100 'how many files needed before we decide to restart?
SYSDATA = ""
SYSEXPLANATION = "Unable to count files"
n = cntFiles( strPath, bIncludeDirsInCount )
If( n < 0 ) Then
WScript.Quit
End If

SYSDATA = n
SYSEXPLANATION = "Number of files=[" & n & "], maximum allowed=[" & nMax & "]"
'wscript.echo SYSEXPLANATION

If( n > nMax ) Then

'SEND EMAIL ON REMOTE SERVER AND RESTART SERVICE
'----------------------------------------------------------
'SEND ALERT EMAIL TO ADMIN THAT THERE HAS BEEN A RESTART
'----------------------------------------------------------

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "ServiceX Was restarted on " & NOW()
objMessage.From = "screwed@server.com"
objMessage.To = "you@domain.com"
objMessage.Cc = "mynumber@mobile.mycingular.net"
'objMessage.Bcc = "myphone@mobile.mycingular.net"
objMessage.TextBody = "My Service HAD TO BE RESTARTED. Summary: " & SYSEXPLANATION

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.myserver.com"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

objMessage.Configuration.Fields.Update

objMessage.Send

'WScript.Echo "Admin alert email sent"

'----------------------------------------------------------
'RESTART SERVICE CODE
'----------------------------------------------------------
'RESTART SERVICE CODE
Dim objShell, intShortSleep, intLongSleep

Set objShell = CreateObject("WScript.Shell")
'Notes must have double quotes if it has spaces in the service name, also leave the single empty space before the service

'get the service name from the ctrl alt del not the service name in services snapin

strService = " ""SERVICE TITLE"""
intShortSleep = 1500
intLongSleep = 5500

' Cmd prompt opened
objShell.Run "cmd"
Wscript.Sleep intShortSleep

' Service stopped with 'Net' command
objShell.SendKeys "NET STOP" & strService
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"
Wscript.Sleep intLongSleep

' Service started with 'Net' command
objShell.SendKeys "NET START" & strService
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"
Wscript.Sleep intLongSleep

' Cmd prompt exited
objShell.SendKeys "Exit"
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"

'Wscript.Echo strService & " service stopped and restarted"
WScript.Quit

Else
'wscript.echo "Good to go no restart needed"
End If

Function cntFiles( strFolder, bIncludeDirInCount )
Dim objFolder, objSubFolders, objFso, o, n

On Error Resume Next

cntFiles = -1

Set objFso = Createobject( "Scripting.FileSystemObject" )
Set objFolder = objFso.GetFolder(strFolder)
If( Err.Number <> 0 ) Then
Exit Function
End If
n = objFolder.files.count
Set objSubFolders = objFolder.SubFolders
For Each o In objSubFolders
n = n + cntFiles( o, bIncludeDirInCount )
'If( bIncludeDirInCount ) Then
'n = n + 1
'End If
Next

Set objSubFolders = Nothing
Set objFolder = Nothing

cntFiles = n
End Function