Hey, Scripting Guy! How Can I Open an Alternate Home Page If the Default Home Page Isn’t Available?
Hey, Scripting Guy! In our company we hardcode everyone’s computer to use our intranet site as their Internet Explorer home page. All our users use laptop computers, however, and when they take the machines home with them and try to surf the Web they always start off with a Page Not Found error; that’s because they can’t reach our intranet from home. Is there any way to script things so that, if the intranet isn’t available, they open an alternate home page instead?
Hey, JF. You know, today is April 1st; that is, today is “April Fool’s Day” in the US and many other countries around the world. April Fool’s Day is the day you’re supposed to play pranks on people, and media titans such as Sports Illustrated, the BBC, and the New York Times have all taken their shot at pulling elaborate hoaxes. (Take a peek at this Web site for a list of the 100 greatest hoaxes of all time. We’re not sure if the 1957 Swiss spaghetti harvest is truly the greatest hoax of all time, but it is pretty good.)
The Scripting Guy who writes this column has always wanted to do an April Fool’s edition of the Script Center, but has never followed through on it. He’s not sure what he would do exactly, but probably the Script Center would include a number of articles along the lines of announcing that Microsoft has bought McDonald’s and will soon be coming out with the Vista QuarterPounder with cheese. (“Someone is trying to take a bite out of your hamburger. Do you want to allow this?”) But, like we said, he’s never followed up on that.
Note. Why not? Well, when most of you hear the word “TechNet” you probably think, “Oh, right, TechNet; what a fun-loving, devil-may-care group those guys are!” Surprisingly enough, however, the good folks at TechNet – and at Microsoft in general – don’t take a joke quite as well as you might think. Not that we don’t do funny things here all the time; we do. It’s just that most of them weren’t supposed to be funny.
So, once again, no April Fool’s Day edition of the Script Center. (Will there be such an edition next year? Let’s put it this way: not if the Scripting Son is still enrolled in college.) That’s a bit of a disappointment for the Scripting Guy who writes this column, but most people won’t know the difference. After all, most people who read this column probably think, “It must be April Fool’s Day again. This can’t possibly be a real column about system administration scripting!”
And yet, it is. To prove it to you, here’s a script that can open an alternate home page if the default page (i.e., the company intranet site) isn’t available:
strWebSite = "intranet" Set objShell = CreateObject("Wscript.Shell") Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_PingStatus Where Address = '" & strWebSite & "'") For Each objItem in colItems If objItem.StatusCode = 0 Then objShell.Run "http://intranet.company.com" Else objShell.Run "http://www.microsoft.com/technet/scriptcenter/default.mspx" End If Next
Before we go any further we should note that we know of no way to add an alternate home page to Internet Explorer. Therefore, we decided to cheat a little bit here. This script should be saved as a .VBS file, and users should be instructed to click this script any time they want to start Internet Explorer. And yes, one easy way to take care of that would be to retarget your Internet Explorer start menu shortcuts to make them run this script rather than directly open Internet Explorer.
As for the script itself, we start out by assigning the name of the server that hosts the intranet home page to a variable named strWebSite. Alternatively, we could assign strWebSite the IP address for the home page; in other words, we could do this:
strWebSite = "192.168.1.1"
After assigning a value to strWebSite we create an instance of the Wscript.Shell object; this is the object we’ll use to open a new instance of Internet Explorer. Or – gasp! – a new instance of FireFox or Opera or whatever else your default Web browser might be. (But if you aren’t using Internet Explorer please don’t tell us that; you’ll make the Scripting Editor cry.)
From there we connect to the WMI service on the local computer, then use this line of code (and the Win32_PingStatus class) to ping – in this case – the computer intranet:
Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_PingStatus Where Address = '" & strWebSite & "'")
The Win32_PingStatus class will return a property value – StatusCode – that tells us whether we were able to successfully ping the computer in question. If the StatusCode equals 0 that means our ping attempt succeeded; if the StatusCode is anything but 0 that means that the ping attempt failed. What does all that mean? It means that we can determine whether or not the computer intranet is available to us by doing two things: 1) setting up a For Each loop to loop through the collection of ping data returned by Win32_PingStatus; and, 2) inside that loop checking to see if StatusCode is equal to 0:
If objItem.StatusCode = 0 Then
If the StatusCode is equal to 0 that means we’re on the intranet; consequently, we use the following line of code, the Run method, to open our intranet home page:
If the StatusCode is not equal to 0 that means we aren’t on the intranet. In that case, we open an alternate home page:
As a matter of fact that is the Script Center home page. If you can’t reach your intranet home page do you have to open the Script Center home page? No, of course not – uh, yes, yes you do. In fact, you should always open the Script Center home page before you do anything else. If you don’t, well, who knows what terrible things might happen?
Note. Well, OK, most likely nothing terrible will happen. We just want to get as many page views as we can, one way or the other.
The preceding script works pretty well, and it works pretty fast; that’s due primarily to the fact that the Ping command works pretty fast. However, there are a couple of possible drawbacks to this script. For one, you do have to know the name (or IP address) of the server where your intranet home page resides; it’s not going to work to try and ping an actual Web page (like http://intranet/company/com/default.htm). In addition, the Win32_PingStatus class didn’t exist until Windows XP came around. If you have any computers running Windows 2000, well, they aren’t going to be able to use this script.
Which is why we’re offering a backup plan. This script uses the MSXML2.XMLHTTP object to try to connect to an actual Web page: http://intranet/company/com/default.htm. If the connection succeeds (that is, if the Open method returns a StatusText value equal to OK) we go ahead and open that intranet page. If StatusText turns out to be anything other than OK we open the Script Center home page instead. Here’s what our Plan B script looks like:
On Error Resume Next Set objShell = CreateObject("Wscript.Shell") strURL = "http://intranet.company.com/default.htm" Set objHTTP = CreateObject("MSXML2.XMLHTTP") objHTTP.Open "GET", strURL, FALSE objHTTP.Send If objHTTP.StatusText = "OK" Then objShell.Run "http://intranet.company.com" Else objShell.Run "http://www.microsoft.com/technet/scriptcenter/default.mspx" End If
This script will run on Windows 2000, and it does allow you to connect to a Web page rather than a server. The drawback? It can be considerably slower than the Ping script. If the Ping script takes 10 seconds to complete, the backup script might take 30 seconds to complete. Just something to keep in mind.
We hope you’ll find this useful, JF. Like we said, it’s a bit of a workaround, but it does work.
Because our mission is to educate people, and to keep everyone informed on the hot topics in system administration, we should note that the origins of April Fool’s Day are enshrouded in mystery. One theory suggests that the custom began in France in 1564, when the country decided to start the calendar year at the beginning of January rather than the end of March. Not everyone was enamored with that decision, and a number of people stubbornly clung to the old calendar, continuing to celebrate the “New Year” during the week of March 25 through April 1. Pranksters would play tricks on these old stick-in-the-mud types, primarily by attaching paper fish to their backs.
Those wacky Frenchmen, eh? Of course, in their defense, this was 1564. Truly funny stuff probably hadn’t been invented yet.
By the way, the Vista QuarterPounder with Cheese will still include a hamburger, cheese, onions, pickles, ketchup and mustard. Or at least it will in the US. In Europe the EU will no doubt require us to sell a stripped-down version of the Vista QuarterPounder that will include only the bun; consumers will then be free to shop elsewhere for hamburger, cheese, onions, pickles, ketchup and mustard of their choice.