Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14850
  • Last Modified:

VBScript Loop Every 10 Minutes

What's a quick VBScript loop I can use to loop something every 10 minutes?
0
MDauphinais1
Asked:
MDauphinais1
  • 2
  • 2
  • 2
  • +1
4 Solutions
 
tcicatelliCommented:
This code is not my own, but it might be what you're looking for.

Function GoSleep(seconds)
   '
   ' Check the Windows Script Host Version.
   '
   wsv = WScript.Version
   '
   ' If it's 5.1 or newer, use the built-in Sleep command,
   ' since it might be more reliable or better-performing.
   '
   if wsv >= "5.1" then
      WScript.Sleep(seconds * 1000)
   else
     '
     ' If the version isn't at least 5.1, use a quick-and-dirty
     ' method of determining the elapsed time since the function
     ' was called.
     '
     startTime = Time() ' gets the current time
     endTime = TimeValue(startTime) + TimeValue(elapsed) ' calculates when time is up
     '
     ' While the current time is less than the calculated end time,
     ' add zero to x.
     '
     While endTime > Time()
         '
         ' If there's nothing in here but a comment, the while
         ' doesn't seem to process correctly, so we'll just tell
         ' VBScript that we need it to let Windows do other things
         ' for a little while...
         '
         DoEvents
     Wend
   end if
End Function
0
 
MDauphinais1Author Commented:
ok, so how would I use that to loop?

Say for instance I wanted a message box to appear every 10 minutes (I don't but just as an example):

MsgBox "say something"
GoSleep(600)     <-- (600 for 10 minutes)

Or would I have to put my "message box" into the function itself to get it to loop?
0
 
tcicatelliCommented:
no, gosleep 600 is only 600 milliseconds.  1000 milliseconds = 1 second. So, gosleep 60000 is one minute.  I would guesss gosleep(600000) would be over the maximum limit allowed by the function, so you'd need to place it inside a for loop or do the other calculation where the start time is subtracted from the end time in a while loop.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
chandru_solCommented:
Hi,
Below is the normal loop script for displaying the message box?

Dim Msg

Msg = "Hi"

i = 0

Do While i = 0
Msgbox Msg
Wscript.Sleep (6000)
Loop

You can increase the value = 6000 to match your needs

regards
Chandru
0
 
Robberbaron (robr)Commented:
for a 10 minute interval, i would be using the Windows scheduler service.
CtlPanel / Schedule.

Concern is "does the Sleep command takeup processor usage...." and Does the script ever end ?

use the script timer for less than 1 minute as that is the lowest the scheduler can use.
0
 
chandru_solCommented:
Hi,

Hope this script helps................

strComputer = "Srever-01"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecNotificationQuery("Select * from __instancemodificationevent " _
        & "within 30 where TargetInstance isa 'Win32_Service'")

i = 0

Do While i = 0
    Set objService = colServices.NextEvent
    If objService.TargetInstance.State <> objService.PreviousInstance.State Then
        Set objEmail = CreateObject("CDO.Message")
        objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SMTP Server"
        objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        objEmail.Configuration.Fields.Update
        With objEmail
            .To =  "mail@mail.com"
            .From = "to@mail.com"
            .Subject = "Windows Service status has changed"
            .TextBody = strcomputer & " - " & objService.TargetInstance.Name & " is " & objService.TargetInstance.State _
                    & ". The service previously was " & objService.PreviousInstance.State & " ."
            .Send
'            Msgbox "Mail Sent"
        End With        
    End If
Wscript.Sleep (60000)
Loop

The above script will monitor the serivces in the server for every 10 minutes and will send an email when the service has changed state

regards
Chandru
0
 
MDauphinais1Author Commented:
Thanks guys. I can make something happen with that.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now