Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions

Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions

  • Comments 1
  • Likes

How Can I Automatically Open and Monitor Web Sites?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I enjoyed reading your Quick-Hits Friday article that talked about opening multiple Web sites in different browser tabs, and that got me thinking. Our company has a number of Web sites we need to monitor, and I was wondering if you could modify the script to monitor those sites. What I mean is that I need the script to open the sites, wait a little bit, close the pages, and then open them back up again. I know you can do it, because you are the greatest!

- KB

SpacerHey, Scripting Guy! Answer

Hi KB,

Flattery will get you everywhere. What we need to do is make just a couple of modifications. The first is to create a counter variable for the number of times we want to loop, and another variable for the amount of time we want to wait. As it stands, the script makes 10 loops and waits 10 seconds between loops. We use the same amount of time for displaying the pages and for waiting between loops. You could create two separate variables if you wish. In testing, we discovered that you cannot make this time too short or it will generate errors. So I think that 10 seconds is about as short as you want to make it. Also if the page is slow to load, you should lengthen this time as well.

The rest is the same except for one other detail. We use WMI to kill the iexplore process. We put this code into a subroutine that makes it easy to call. Keep in mind that the script will kill all instances of the iexplore process that are running.

Here is the script:

  TnSc = "http://search.live.com/macros/scripter/searchscriptcenter/?FORM=MACWLG" 
  MSDN = "http://search.live.com/macros/scripter/searchmsdndocs/?FORM=OIJG"
  SC = "http://www.microsoft.com/technet/scriptcenter/default.mspx"
  SR = "http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true"
  CSR = "http://www.microsoft.com/technet/scriptcenter/csc/default.mspx"
  HSG = "http://www.microsoft.com/technet/scriptcenter/resources/qanda/hsgarch.mspx"
  OS = "http://www.microsoft.com/technet/scriptcenter/resources/officetips/archive.mspx"
  intTime = 10000 'wait period in milliseconds
  intLoops = 10   'number of loops to make
  
For i = 1 To intLoops
  Set oIE = CreateObject("InternetExplorer.Application")
  oIE.Visible = True
  oIE.FullScreen = False
  'open a new window
  oIE.Navigate2 Tnsc
  'open url In new tab
  oIE.Navigate2 MSDN, 2048
  oIE.Navigate2 SC, 2048
  oIE.Navigate2 SR, 2048
  oIE.Navigate2 CSR, 2048
  oIE.Navigate2 HSG, 2048
  oIE.Navigate2 OS, 2048
Set oIE = Nothing

WScript.Sleep intTime
subKillExplorer
Next

Sub subKillExplorer
On Error Resume Next
wmiQuery = "Select processID from win32_process where name = 'iexplore.exe'"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem in colItems
    Wscript.Echo "Killing ie: " & objItem.terminate()
Next 
WScript.Sleep intTime
End Sub

Top of pageTop of page

How Can I Monitor Multiple Web Sites on a Single Browser Tab—and Make the Script Run Forever?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I liked your Quick-Hits Friday article that talked about opening multiple Web sites in different tabs, but there is a problem with it. I do not need to open a bunch of different tabs. Instead I have a number of Web sites I need to monitor, and I would like to cycle through them one at a time and display the Web sites on a single tab. In addition, I would like the script to run forever. Can you make some changes to the script to do this?

- JK

SpacerHey, Scripting Guy! Answer

Hi JK,

I am glad you liked the script. It seemed to be something quite popular because we received a number of questions about it. Interestingly enough, I use that script every single day when I am answering e-mail from loyal readers, such as you. In fact, the links that are in the script are the ones I use the most, including the two Scripting Guys search macros. In fact, the Live Search macros have been so useful to me that I have created several new ones that I use all the time.

To modify the script, what we need to do is to store the Web sites in an array. To declare a static array, we use the DIM command; we give it a variable and a number indicating how many items we will store. Because we know in advance how many items we wish to store, we can use this syntax. To make it run forever, we use a Do…Loop. To walk through an array, we use the For…Next construction seen here:

For i = 0 To ubound(WebPage)-1
   WScript.Echo "page " & i
    oIE.Navigate2 WebPage(i)
    WScript.Sleep intTime
Next

The ubound function tells us how many elements are in the array. We could have hard coded a value here, but because it is likely you will be changing the number of Web pages, I did not want to have a hard-coded value that would trip you up. That was all the changes we really made. Here is the script:

Dim WebPage(3)
  WebPage(0) = "http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true" 
  WebPage(1) = "http://www.microsoft.com/technet/scriptcenter/resources/qanda/hsgarch.mspx"
  WebPage(2) = "http://www.microsoft.com/technet/scriptcenter/default.mspx"
  
  intTime = 10000 'wait period in milliseconds
  
  
Do
  Set oIE = CreateObject("InternetExplorer.Application")
  oIE.Visible = True
  'open a new window
  For i = 0 To ubound(WebPage)-1
   WScript.Echo "page " & i
    oIE.Navigate2 WebPage(i)
    WScript.Sleep intTime
  Next
  
Set oIE = Nothing

WScript.Sleep intTime
subKillExplorer
Loop

Sub subKillExplorer
On Error Resume Next
wmiQuery = "Select processID from win32_process where name = 'iexplore.exe'"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem in colItems
    objItem.terminate()
Next 
WScript.Sleep intTime
End Sub

Top of pageTop of page

How Do I Monitor a Text Log File for New Entries Containing Specific Words?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I am new to scripting. I was unable to locate exactly what I was looking for on your Web site. Very informative though. I need a script that will monitor a text log file for new entries or lines that contain the word “Global” or “Remote”. Thanks.

- KH

SpacerHey, Scripting Guy! Answer

Hi KH,

We welcome you to the wonderful world of scripting. You are about to embark on a journey that can easily grow to consume all your free time, occupy your every waking moment, and invade your every thought and dream. When I was walking hand in hand with the scripting wife along a beautiful sandy beach at sunset in Aruba, where I was earlier in the year, I was, of course, thinking of how I could preserve the moment and make it last forever. So I hurried back to the room and wrote a script. Sorry, I digress.

I am not entirely certain I understand exactly what you are asking. When you mention monitoring, that makes me think of looking at something on a rather continuous basis. This kind of scripting is rather difficult and is actually not best done for long periods of time using a script. We can monitor a file for changes using this type of technique: http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr05/hey0404.mspx.

When the file is changed, we could open the file and look for the word “Global” or “Remote.” To do this, you need to use regular expressions, which are another rather advanced kind of scripting. In fact, regular expressions become their own language and are areas in which people specialize. This article talks about searching a text file for a particular word: http://www.microsoft.com/technet/scriptcenter/resources/qanda/may08/hey0505.mspx.

If this is something that must run continuously, what you really need to do is to write a service to do this. If you insist on doing this via script, the second best thing to do would be to create a permanent event consumer via WMI. This can be done by scripting, but is very difficult to do as well. This article talks about the process involved in doing this: http://www.microsoft.com/technet/scriptcenter/guide/sas_wmi_tjin.mspx?mfr=true.

Because you are new to scripting, let me point you to some resources that may be of interest to you.This is the VBScript Getting Started section of the Script Center; it has a number of good links to help you get started: http://www.microsoft.com/technet/scriptcenter/hubs/start.mspx.

You may also wish to consider learning Windows PowerShell. In fact, Windows PowerShell is the future of Microsoft scripting. If I were just starting out, I think I would focus on learning Windows PowerShell and be ahead of the game, rather than learning VBScript which is currently in maintenance mode at Microsoft. This means there is no new development work being done on VBScript other than a few bug fixes from time to time. In fact, VBScript has not changed since the release of Windows XP. Here is the Windows PowerShell hub; if you decide to learn Windows PowerShell, download and install version 1.0, which is the current released product: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx.

Hope this helps, and again welcome aboard. See you next week.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Scripting guy,

    Could you please help me in learning VB scripting? I want to write a script which can accept user's credentials in a window and go to the sites using those credentials to fetch data into a spread sheet.