The VBScript Network and Systems Administrator's Cafe:

September, 2008

Sep 27 2008   8:19PM GMT

VBScript Statements: Explanation of the Private Statement



Posted by: Jerry Lees
VBScript Statements, Classes, Private

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:

Private PrvVariable
PrvVariable = 10

Sep 26 2008   7:54PM GMT

VBScript Statements: Explanation of the On Error Statement



Posted by: Jerry Lees
VBScript Statements, Error control, on error

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. 


Sep 20 2008   8:10PM GMT

VBScript Statements: Explanation of the Property Get Statement



Posted by: Jerry Lees
VBScript Statements, Property, Property Get

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

End Property


Sep 19 2008   7:50PM GMT

VBScript Statements: Explanation of the Property Set Statement



Posted by: Jerry Lees
VBScript, VBScript Statements, Property, property set

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:

InstanceOfClass.PropertyName.Set (args)

 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

End Property


Sep 18 2008   7:31PM GMT

VBScript Statements: Explanation of the Property Let Statement



Posted by: Jerry Lees
VBScript, VBScript Statements, Classes, Property, Property Let

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

End Property


Sep 13 2008   11:11PM GMT

VBScript Statements: Explanation of the Function Statement



Posted by: Jerry Lees
VBScript, Functions, VBScript Statements, VBScript Functions

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:

wscript.echo add(2,3)

function add(x , y)

       add = x+4
end Function


Sep 12 2008   6:04AM GMT

How to use command line arguments in VBScript via the Wscript.Arguments Object



Posted by: Jerry Lees
VBScript, vbscript tips, Wscript.Arguments, commandline

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.Quit
end If

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!


Sep 6 2008   11:41PM GMT

VBScript Statements: Explanation of the Public Statement



Posted by: Jerry Lees
Development, VBScript, VBScript Statements, Variable Types, Public

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:

Public TestVariable


Sep 4 2008   4:01PM GMT

Pinging a remote computer from another remote computer using the WMI Win32_PingStatus class in VBScript



Posted by: Jerry Lees
Networking, monitoring, Development, VBScript, Functions

I recently began working on trying to figure out ways to troubleshoot real problems with a VBScript and give me some diagnostic information about the current state of the environment. As I build this script I plan on sharing pieces of it bit by bit with you, my readers. I also wanted to get back to writing some WMI scripts that will help you all do your jobs more efficiently. Lastly, I wanted to begin building a “toolbox” script that you could use to write your own scripts. These are the goals I’m tracking toward in my next series of posts that contain VBScript code. Now onto this script…

The first piece of troubleshooting a problem, in my opinion, is ensuring that a communication path exists between two systems. Duh! However, simply pinging the systems individually from your desktop isn’t a good test… it only tests the your computer can communicate with the two systems, not that they can communicate with each other!

This script simply uses our friend WMI to make a call a remote computer requesting that computer ping another computer. Simple enough, but invaluable because how many times have you had to login to a remote computer to check if it can connect to a system? Now you don’t have to… you can do it from a script!

The script uses the Win32_PingStatus class in WMI. Essentially, it will only work on Windows XP and newer (Sorry, Windows 2000 and older doesn’t support the WMI class we need.) and the user executing the script will likely need to be an administrator on the system that is being called (but not necessarily being pinged). For further information on this class you can reference the Win32_PingStatus documentation.

Now lets get to the script!

 ‘Use a remote computer to ping another remote computer
Option Explicit
 
‘Change the SourceServer and RemoteServer Strings below to servernames or IP addresses for you.
wscript.Echo RemotePing(”SourceServer”, “RemoteServer”)
wscript.Echo “Done!”

Function RemotePing( SourceComputer, DestinationComputer)

 Dim strComputer1, strComputer2
 Dim objWMIService, colItems, objItem

 strComputer1 = SourceComputer
 strComputer2 = DestinationComputer

 On Error Resume Next
  ’ error control block
  Set objWMIService = GetObject(”winmgmts:{impersonationLevel=impersonate}//” & strComputer1 & “\root\cimv2″)
  Set colItems = objWMIService.ExecQuery (”Select * from Win32_PingStatus ” & “Where Address = ‘” & strComputer2 & “‘”)
  For Each objItem in colItems
      If objItem.StatusCode = 0 Then
          RemotePing = strComputer1& “: Reply received from ” & strComputer2 & ” in ” & objItem.ResponseTime & ” ms.”         
      Else
       RemotePing = “Error pinging ” & strComputer2 & ” from ” & strComputer1 & “. The status code returned was :” & objItem.StatusCode
      End If
  Next
 On Error GoTo 0
End function

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!


Sep 2 2008   3:44PM GMT

Google’s new browser, Chrome scheduled for release today



Posted by: Jerry Lees
Google Chrome, tech news

Well, the blogosphere is currently in a frenzied state about the new Google Chrome browser– and there still isn’t a download page for it. It’s gotten to such a point that even searching Google won’t bring up a link for the browser from google itself– which is actually kind of funny if you think about it.

I’ll be posting information to this article as it becomes available throughout the day, so check back often or bookmark the page to get the latest information on thenew Google Chrome Browser.

 It appears that for a time there was a download page, tons of sites link to it but the page simply redirects you to the google home page at this time. I’ve seen some speculation that the response was overwhelming Google’s servers… I doubt that seriously. I’m betting it is simply sheer marketing, generate a frenzy and release it later. The official google blog has a posting yesterday claiming the release to the public was an accident… oops.

 At any rate, while the browser isn’t available to download yet, I did manage to find some Chrome screenshots that were not hosted on google’s site– and are still available to view over at pcworld.

There’s more to come as information becomes available. For now, the best information I’ve seen is that the download is supposed to be available at 2pm EST and will be available at the  Google Chrome Page. but while you wait, you can read the comic they put out that supposedly explains how it works.