Handling Live Web Content in WSH - Part 1 - A Little Error-Handling
(Page 3 of 4 )
So you’re probably asking what those extra If statements are for, right? Those are just to make sure that the VB functions are actually returning usable values. This prevents our script from crashing if an error occurs. Ideally, we would add some error handling, but that’s beyond the scope of this tutorial. More information about error-handling can be found in the Microsoft Windows 2000 Scripting Guide located on Microsoft’s TechNet website.
http://www.microsoft.com/technet/scriptcenter/guide/
Back to our script, we need to finish off our original If statement with the following code.
Else
strIP = "Unavailable"
End If
getIP = trim(strIP)
This code only executes if strHTMLText is empty, meaning that we didn’t get a response from the website. We could issue some kind of alert to the user to indicate there was a problem but we’re just going to assign “Unavailable” to strIP instead. Remember, this is the variable that would hold our IP address had everything worked out correctly.
You now have one of two things. The variable strIP either contains your IP address or it contains the text “Unavailable”. In line 4 we make use of VB’s trim() function to make sure that strIP doesn’t contain any leading or trailing spaces. You can do with this information whatever you like. For our purposes here, we’re just going to end by echoing it back with WSH’s echo method.
Wscript.Echo getIP
Save your work as checkip.vbs and double-click it to see if it works. If you followed along correctly, you should get an alert box that displays your external IP address. If the script returns “Unavailable”, make sure you are connected to the internet and that the site isn’t down.
Okay, let’s recap. We used the XMLHttpRequest object to request a website. That website was captured into a string variable. We used VB's inStr() and mid() functions to cut away everything except the text that we wanted, and then we echoed that back to the user.
Go ahead and congratulate yourself. You’ve just learned how to capture live web content in WSH!
Next: The Final Code >>
More Windows Scripting Articles
More By Nilpo