The VBScript Network and Systems Administrator's Cafe:

VBScript Objects

Mar 10 2009   2:01AM GMT

Writing scripts that use speech with the Windows Speech API



Posted by: Jerry Lees
undocumented windows, SAPI.SpVoice, Sound, Speech API, Office Agent, VBScipt, VBScript Objects

I’ve been looking for ways to make a script speak the information it needs to relay to the user just for fun and, unfortunately the only scripts I could find out there were extremely elaborate onces that use the Microsoft Office Agents. While they fit the bill they were annoying and required Microsoft office components to be installed… that just wouldn’t do.

Then I found the Windows Sound API. It doesn’t require anything at all to be installed and it works fine. The script below is simple and easy to implement in your code. Check it out:

strText = “Hello, there…. can you write code?”

Set objVoice = CreateObject(”SAPI.SpVoice”)
objVoice.Speak strText

Oct 3 2008   3:00PM GMT

How to retrieve HTML web pages with VBScript via the Microsoft.XmlHttp object



Posted by: Jerry Lees
HTTP, HTML, XML, VBScript, VBScript Objects, Microsoft.XmlHttp, 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!


Aug 1 2008   10:00PM GMT

VBScript Statements: Explanation of the Set Statement



Posted by: Jerry Lees
VBScript, VBScript Statements, Objects, set, VBScript Objects, working with objects, set command, working with variables

The set statement is used to assign any value to any type of variable. For the most part this command is optional. However, it needs to be used when you are assigning an object reference to a variable, such as when you use a variable to hold the return of a CreateObject function for later reference in your code.

An example of the set command would look like:

Option explicit
Dim objDictionary
Set objDictionary = CreateObject(”Scripting.Dictionary”)


Jul 10 2008   3:23AM GMT

How to use the VBScript Scripting.Dictionary object to store data to make key decisions.



Posted by: Jerry Lees
VBScript, Scripting.Dictionary, VBScript Objects, VBScript Functions, randomize, rnd, Random Numbers

I’ve been writing lately on individual topics and thought it was time for a little fun, while introducing you to a new object in the process… the Scripting.Dictionary object! (and random number generation in the process)

The Scripting.Dictionary object is a quite useful object. It will let you store data in a categorized manner through it’s use of a key when you add data to the object. It works very much like a multi-dimensional array would, except that it is much easier to retrieve a specific type of data.

You place the data into the object via the .add method and retrieve a specific item via it’s key. The key can either be numeric or a string. In the example below, I’ve chosen a number. Now for the example… a script so useful that I’m certain it will become a key part of your decision making process at work… The Magic Eight Ball! Now, before you laugh not everyone has a magic eight ball (I have two) and that’s a shame because they really do help with making decisions.

Just run the script below several times and ask it these questions:

Will anyone notice if I reboot the exchange server now?
Will the network survive while the Network Guys upgrade the switches?
Will the servers survive another patch Tuesday?
Does my boss appreciate me enough to give me a $10,000 raise?

And now for the example code you’ve been waiting for…

 Option explicit
Dim objDictionary
Set objDictionary = CreateObject(”Scripting.Dictionary”)

objDictionary.Add 1, “All signs point to NO”
objDictionary.Add 2, “Outlook not likely”
objDictionary.Add 3, “All signs point to YES”
objDictionary.Add 4, “Maybe”
objDictionary.Add 5, “You want what… When?”
objDictionary.Add 6, “Definitely”
objDictionary.Add 7, “Reply Hazy, try again later”
objDictionary.Add 8, “I could tell you, but I’d have to kill you”
objDictionary.Add 9, “Sure, why not.”
objDictionary.Add 10, “One word Answer. Snowball… you figure it out.”

Dim Answer
Randomize

Do
 Answer = int(Rnd(1)*10)
Loop While (Answer < 1)
WScript.Echo objDictionary.Item(Answer)

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!