Recently, I had a situation where I had to pull down a HTML page to compare it to a known copy of the page. Certainly, IE or FireFox– or Google’s Chrome would have done the trick and I could have viewed the source. But that would require me to do work every time we needed to check the page against the known good source.
Instead I wrote a script to pull the HTML source and echo the response to the console (or a messag box if you are not using cscript to execute the script). While not a full blown HTTP QA script it does do the job of getting the HTTP responses from the server and is certainly a core part of any QA script anyone would write.
Basically the script uses the Microsoft.XMLHTTP object to preform all the HTTP calls and retrieve the HTML page. It sounds scary, but if you look at the script below I think you’ll find that it really is quite easy to accomplish. So, here is the script’s code:
Set WshShell = WScript.CreateObject(“WScript.Shell”)
Set http = CreateObject(“Microsoft.XmlHttp”)
On Error Resume Next
http.open “GET”, URL, False
if err.Number = 0 Then
Wscript.Echo “error ” & Err.Number & “: ” & Err.Description
set WshShell = Nothing
Set http = Nothing
Recently, I had a situation at the office where I needed to check a web.config file for the existence of a setting. Not a hard problem– just open the file up an look for it, however, that becomes a little harder when your talking about 30 servers. So, I scripted it using the replace function!
The replace function in VBScript is an invaluable tool in very many scripts, especially when you want to simply check for the existence of a string in another string or completely replace a string with another string. Its simplest syntax is fairly straightforward as well, it is:
NewString = Replace (Searchedstring, FindString, Replacewithstring)
This one function allows you to completely change a file by searching for the value in the string(s) read into a variable then saving the new variable into the file. Or you can do a comparison on the new variable with the old one to see if they are equal, if they aren’t the string was found and replaced.
All in all, Replace is an invaluable tool to put in your scripting arsenal.
The Private statement is used to declare a variable as being Private inside a script. If used outside of a class it has little effect and the variable can be used throughout the script, however, if used inside a class statement it will only be able to be used inside the class statement itself.
The use of this statement works exactly like using Dim, except that the initialization of the variable must happen on a second line of code. Below is an example:
PrvVariable = 10
The on error statement allows you to somewhat control how the scripting engine handles errors in the scripts you write. It allows you to turn error handling on and off for the scripting engine so that you can handle them in your script.
The Statement essentially has two ways it is used. First to turn off the script engine error checking:
On Error Resume Next
The second is to turn it on, thereby allowing the script engine to stop execution of a script when an error is encountered. Like so:
On Error Goto 0
One word of caution, It is very tempting to simply place “On error resume next” in a complex script block and not worry about annoying hard to find errors. However, It is generally better to try to find the source of the errors since ignoring the errors can cause unintended consequences– or at the very least difficult to find issues in another piece of code.
In general, if you use On Error Resume Next you should always check for errors and handle them in your code somehow.
Within a VBScript class the Property block allows you to Get the value of the property in the class. This statement works in conjunction with the Property Let and Property Get statements and can only be used inside a class inside VBScript– and no where else in the script.
By default the Property Get statement is public but you can declare it to be private if you need to do so. Since this statement is used in a block, it is required that you end the block with the End Property statement. This statement works a little differently than the other two property statements, however, because it returns a value. The returning of a value works very much like a function, in that, you set the Name of the property to the value you want to return.
The block of code will look something like this:
Property Get PropertyName
‘ code goes here
ProptertyName = value
Within a VBScript class the Property block allows you to set the value of the property in the class. This statement works in conjunction with the Property Let and Property Get statements and can only be used inside a class inside VBScript– and no where else in the script.
This would in turn be called in VBScript in your function after you create an instance of the class like so:
By default the Property Set statement is public but you can declare it to be private if you need to do so. Since this statement is used in a block, it is required that you end the block with the End Property statement. The block of code will look something like this:
Property Set PropertyName
‘ Property setting code goes here
Within a VBScript class the Property block allows you to set the name of the property in the class. This statement works in conjunction with the Property Set and Property Get statements and can only be used inside a class inside VBScript– and no where else in the script.
By default the Property Let statement is public but you can declare it to be private if you need to do so. Since this statement is used in a block, it is required that you end the block with the End Property statement. The block of code will look something like this:
Property Let PropertyName
‘ code goes here
The Function statement creates a function and allows you to list the arguments (if any) which are to be passed into the function.
The major difference between a function and a subroutine, is that a function can return a value. To return a value you use the function name as if it were a already declared variable and simply set the name of the function to the value you want to return. However, you do not not have to assign or Set a return value.
Functions can be declared Private or Public (the default). You can drop out of a function, without exiting the script entirely by using the Exit statement. You can call other functions or subroutines from within a function (nesting). You must end every function with End Function or you will get an error message.
Here is an example of a function and it’s use:
function add(x , y)
add = x+4end Function
One thing that comes up frequently in my need to script tasks, is the ability to run a script from the command line, or as a scheduled job with parameters passed to it from the calling application. In a “set it and forget it” script it’s not such a big deal to have arguments passed to it, simply because you are likely to only use the script for one task… it will do it’s work and (hopefully) you never have to deal with it again. These are the best scripts! Ones where you save time every day when they run, and if they break you have to try to not only remember what they did, but where you scheduled them.
The first time I wrote such a beast I changed the script several times until I had a couple copies doing the same task on different directories. I then tried to create a script that ran against all the directories, but that too became a pain when I wanted to run it as a one off or run it on a different schedule.
Then I figured out how to add command line arguments to my scripts! What a powerful and time saving piece of information that was to figure out!
Command line arguments in VBScript are really very simple, once you have the concepts down and know what object to use. Which, of course, is the Wscripts.Arguments object! With this object you can pass any number of arguments and parse them in your script or add them into a variable– or anything you want, the sky’s the limit!
Enough rambling on though, lets get to the code that will save you a ton of time!
args = WScript.Arguments.Count
If args < 1 then
WScript.Echo “usage: args.vbs argument [argument] [argument] [argument] ”
WScript.Echo “You entered ” & args & ” Arguments, the first was “_
& chr(34) & WScript.Arguments.Item(0) & Chr(34)
As always, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems– I’ve provided the code for download, plus example output files from my final tests for you. You’ll find the code and other files available for download from my website’s (www.websystemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!
The VBScript Public statement creates a variable, function, subroutine, or class that is available outside the bounds of the scope it was created in and it functions very much like a global variable, except that it can be declared or initialized inside a non0-global code block.
An example would be: