Oct 31 2008 6:17AM GMT
Posted by: Jerry Lees
InternetExplorer.Application,
Web Pages,
web sites,
web tools,
wget
Recently, I needed to write a tool that would scrape the links from a page. To accomplish this I used the Internet Explorer object “InternetExplorer.Application“. We’ll explain it a bit more in a later entry but for now, take a look at the code below:
URL = “http://itknowledgeexchange.techtarget.com/itblogs/“
With CreateObject(”InternetExplorer.Application”)
.Navigate URL
Do Until .ReadyState = 4
Wscript.sleep 10
Loop
for each link in .document.links
Wscript.echo link, link.InnerText
next
‘ Uncomment the three lines below to scrape references to images
‘ for each pix in .document.images
‘ Wscript.echo pix.src
‘ Next
.Quit
End With
Oct 30 2008 12:00AM GMT
Posted by: Jerry Lees
Variable Types,
variant,
integer,
string,
VBScript,
working with variables
In the past I’ve cautioned you on always initializing your variables and encouraged you to always declare your variable types, rather than using the default variant type in VBScript. I’ll be the first to admit I don’t follow my own advice in my examples! However, variants can be dangerous!
Recently I had a situation where if statements were returning completely not correct results and for the life of me I couldn’t figure it out! Here is an example that illustrates what was happening to me.
one = 1
two = 2
three = 3
OneBillion = 1000000000
MyVar = inputbox(”Enter a number”)If one < MyVar Then
WScript.Echo (one & ” is lessthan ” & MyVar)
End If
If two < MyVar Then
WScript.Echo (two & ” is lessthan ” & MyVar)
End IfIf three < MyVar Then
WScript.Echo (three & ” is lessthan ” & MyVar)
End IfIf OneBillion < MyVar Then
WScript.Echo (OneBillion & ” is lessthan ” & MyVar)
End If
The basis of the problem is that I was taking input from a file that was a number– however I was reading the file and it was coming into the script and being used as a string by the variant variable. I then was comparing it to a number and the comparisons were not working 100% correctly. Check it out by entering several numbers into the script. Try entering 0, 1, 2, 3, and 1000000001.
The moral of the story here is to always cast your varables as a specific type or use the cint() function to convert your variant varables to numbers before doing number comparisons with a literal number and a variant variable that is supposed to contain a number.
Oct 24 2008 12:30AM GMT
Posted by: Jerry Lees
Environment Variables,
VBScript,
wscript,
wscript.shell
As an extension of the theme I’ve been blogging about lately of “interfacing with other types of applications” in VBScript, I’d like to share with you a snippet of VBScript code that will allow you to read environment variables from your system. This will allow you to determine a great deal of information, since many applications use environment variables for configuration information– and the OS itself does as well, like the server that logged you on is %logonserver%.
There are two types of environment variables:
- System variables, which are available to every process across the system
- Process variables, which are only available to the process and disappear when the process is completed. These are sometimes referred to a user environment variables.
You can see examples of each of these environment variables being used below via the “Environment” method of the Wscript.Shell object.
Set WshShell = WScript.CreateObject(”WScript.Shell”)
‘create a Process (user profile) Level environment variable object
Set WshProccessEnv = WshShell.Environment(”Process”)
‘create a System Level environment variable object
Set WshSysEnv = WshShell.Environment(”System”)
‘display a system environment variable
Wscript.Echo WshSysEnv(”NUMBER_OF_PROCESSORS”)
‘display a user profile level environment variable
Wscript.Echo WsProcessEnv(”Path”)
Enjoy!
Oct 23 2008 1:43PM GMT
Posted by: Jerry Lees
inputbox,
VBScript,
Input,
User Input
While entering data at the command line for a script to use is sometimes helpful like we discussed in How to use command line arguments in VBScript via the Wscript.Arguments Object. However, if you recall in a later blog entry, Using the windows file dialog box in VBScript to provide file selections and populate script options , I mentioned It’s sometimes just not practical! In that entry we discussed a slick way to use the standard windows functions to have a user select a file– but even that sometimes isn’t good enough. Sometimes you just need the user to enter other stuff into the program.
Well, you can easily do this with in VBScript as well, and it’s just one line of code! You use the InputBox command and it’s that easy. You simply assign the return to a variable of your choosing and specify first a Prompt (or directions) to a user and then you optionally can specify a title for the window that will be displayed. Below is an example that places the return of the InputBox command in a variable called Str, displays a prompt saying “Your Prompt here.” and has “Window Title here” in the title bar.
Str = Inputbox(”Your Prompt here.”, “Window Title here”)
There are other options as well, but they are typically not used in most cases. More information can be obtained from Microsoft in their InputBox Function documentation.
Oct 17 2008 11:11PM GMT
Posted by: Jerry Lees
wscript,
WScript.quit,
errorlevel,
batch commands,
exit,
Error control,
VBScript,
on error
We’ve discussed error handling previously (via the On Error Goto 0 command) in a entry titled, Using error control in a VBScript to recover from odd errors. However, occasionally you need to communicate a condition back to an application or batch file that calls a script as part of its processing. One reason you may need to do this is if you need to call a separate script or application depending on the outcome of the script. You can do this with what is called an error level. Keep in mind this may not have anything to do with an error in the script but can, instead, be used as a method to control how another applicatioin responds to a particular condition.
For example, say you already have a executable that processes a file into a database– but you don’t want to run the executable on a old file because it would cause all sorts of duplicated entries in the database. you might not be able to go back to the person who wrote the executable and have them change it or the executable might not have even been written by the company where you work! You could, however write a script to check the date of a file and if it was created during a time frame return a error level back to a batch file that calls your script and preforms the logic to decide if the executable needed to be ran. Take this batch file for example:
Echo off
cscript Yourscript.vbs
If %errorlevel% == 3 goto runexe
Echo “File to old”
goto EndBatch
:runexe
processingprogram.exe
:EndBatch
This would only run the executable IF your script returned a error level of 3! So you can then preform work and checks never intended by the executable when it was written only if the conditions are correct for it to run.
The line of code in VBScript you would add to your script to communicate back to the batch file would be:
WScript.quit(3)
This will return a error level of 3 back to the batch file and cause the batch file to THEN run your executable.
So, there you have it! A quick and simple method to extend the life of those aging processes that work fine– just need a minor tweak because your environment has changed.
Oct 16 2008 2:53AM GMT
Posted by: Jerry Lees
SOAP,
VBScript,
Web Service,
WSDL,
Web Pages,
web sites,
web tools
Recently I posted a list of web services at a site that were public web services, in the article entitled A Great list of FREE publicly available Web Services. In looking through them I noticed one that would be useful- a WHOIS web service!
Please keep in mind that this script uses a PUBLIC and FREE web service to preform the heart of it’s work… it might not be always available and that is out of both our control. However, the code will work, with minor changes where noted for any other web service.
First some background on a web service. When you consume (use) a web service, you call it like you would any other class or function, with a .functionname after the object is created as an instance.
The MSSOAP.SOAPClient line below creates a SOAP object, and the .MSSOAPINIT creates an instance of your web service in that object. Then, in this case the .whois call actually makes it preform the functions on the remote system. The remote system then returns back the value from the function to you just as if a local function were called.
Pretty cool stuff! So, here is the code to call a web service from VBScript!
dim SOAPClient, Response
‘create the SOAP object
Set SOAPClient = createobject(”MSSOAP.SOAPClient”)
on error resume Next
‘create a instance of the WHIOIS web service
SOAPClient.mssoapinit(”http://www.ecubicle.net/whois_service.asmx?WSDL“)
‘ check for errors… deal with them if they occur by reporting them
if err Then
wscript.echo SOAPClient.faultString
wscript.echo SOAPClient.detail
end If
‘this next line is where we actually CALL the web service
Response = SOAPClient.Whois(”whois.networksolutions.com”, 43, “networksolutions.com”)
‘fix up network solutions’ HTML responses to whois queries. why do they have to be difficult?
Response = replace(Response,”<br/>”,VbCrLf)
Response = replace(Response,”<BR/>”,VbCrLf)
‘ echo the response recieved (since it’s a string)
wscript.echo Response
‘ check for errors… deal with them if they occur by reporting them
if err Then
wscript.echo SOAPClient.faultString
wscript.echo SOAPClient.detail
End If
Oct 10 2008 7:57PM GMT
Posted by: Jerry Lees
WSDL,
SOAP,
Web Service,
Web Pages,
web sites,
web tools,
webmaster
As an IIS Administrator I have spent quite a bit of time administering web sites and web services in my role where I work. Recently, I have been doing some work with Web Services in IIS to actually monitor them since we needed to do more than just simply monitor the Web Service Definition Language (WSDL) page.
For those of you who do this sort of work, you know this is a real challenge sometimes, since the web service can actually be broken– but the WSDL page shows up. So, I had to create something to actually consume the web service so that we could truely test the web service.
Web Services are basically (yes, I’m boiling it down to the bare minimum here…) web sites that accept input via the HTTP protocol, preform work based on those parameters, and return back some value via HTTP. Just like a FUNCTION! This is an exciting technology, becasue it essentially is distributed computing.
This caused me to look for public web services, again, because I was interested in the concept since I first heard about it 4 or 5 years back. I found a decent site that had a list of web services that were available on the internet at Xmethods.net. While they all aren’t free (and they all didn’t appear to be operational) — it did seem like a decent list of sometimes useful stuff to have handy.
Having found this, I can now share the consumption of web services from VBScript with you in another blog posting– Consuming and using a web service from within VBScript to create a WHOIS tool.
Oct 6 2008 4:36PM GMT
Posted by: Jerry Lees
File Dialog,
UserAccounts.CommonDialog,
Development,
VBScript,
tips and tricks,
vbscript tips
Sometimes the command line is great for getting a script running and doing the task a script was intended to do, however, sometimes you simply don’t want to add the overhead of opening a command prompt, navigating to the directory where your script is located, and then remembering the command line options you entered into the script to make it run the last time. Then sometimes you just don’t want to hard code the file name into your script because you don’t want to have to change it every time.
In fact, sometimes your script is intended for users who might not even know how to run a script from the command line! Wouldn’t it be great if you could just have windows display a dialog box to prompt the user for a file to chose as a argument for your script?
Well, you CAN! The following script is a sample of what you can do with the Windows Common Dialog box to enable the user to more easily utilize your scripts– all without knowing about the command line!
There are a few key pieces of the script I want to call out before we dig in and start running the code though. First, the object that preforms the magic here is UserAccounts.CommonDialog, the filter options on the dialog in the drop down to select the file types to display in the browser dialog is the line with .Filter property in it, The initial Directory the dialog uses is specified in the .InitialDirectory Property, and finally the default selection of that drop down is specified in the .FilterIndex Property. Take a look at the code and see how easy it is.
Option Explicit
Dim ObjFSO, InitFSO
‘ create an instance of the File Browser
Set ObjFSO = CreateObject(”UserAccounts.CommonDialog”)
’setup the File Browser specifics
ObjFSO.Filter = “VBScripts|*.vbs|Text Files|*.txt|All Files|*.*”
ObjFSO.FilterIndex = 3
ObjFSO.InitialDir = “c:\”
‘ show the file browser and return the selection (or lack of) to InitFSO
InitFSO = ObjFSO.ShowOpen
If InitFSO = False Then
Wscript.Echo “Script Error: Please select a file!”
Wscript.Quit
Else
WScript.Echo “You selected the file: ” & ObjFSO.FileName
End If
Enjoy!
Oct 3 2008 3:00PM GMT
Posted by: Jerry Lees
HTML,
Microsoft.XmlHttp,
VBScript,
VBScript Objects,
XML,
HTTP,
Web Pages
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:
URL=”http://www.gamersigs.net/“
Set WshShell = WScript.CreateObject(”WScript.Shell”)
Set http = CreateObject(”Microsoft.XmlHttp”)
On Error Resume Next
http.open “GET”, URL, False
http.send “”
if err.Number = 0 Then
WScript.Echo http.responseText
Else
Wscript.Echo “error ” & Err.Number & “: ” & Err.Description
End If
set WshShell = Nothing
Set http = Nothing
Enjoy!