The VBScript Network and Systems Administrator's Cafe


June 28, 2008  11:20 PM

VBScript Statements: Explanation of the For … Next Statement

Jerry Lees Jerry Lees Profile: Jerry Lees

The For Next statement is very useful for situations where you need to loop through a piece of code a specific number of times. 
 
You can use Exit For statements to exit out of a For loop, if you need to check for a condition inside the loop. Additionally, The Step keyword at the end of the line that has the for statement will allow you to loop through a For statement in any size of increment. 

Here is an example of a for statement, followed by one with a Step key word:

For x = 1 to 10
     wscript.echo x
next

For x=1 to 20 step 2
    wscript.echo x
next

 

June 26, 2008  1:56 PM

How to associate specific WWW w3wp.exe process ID’s with a IIS application

Jerry Lees Jerry Lees Profile: Jerry Lees

One of the most difficult problems with troubleshooting a web application on a server that has many applications on it is determining which one of the applications is causing an issue. Sometimes that is easy because you see an error message or, in those rare cases where you get an actual screenshot, a URL is given to you to go on. However, often you don’t get such useful information! Most times you just notice a w3wp.exe process either taking up to much memory or using to many CPU cycles (or in some cases none at all), but there’s a vbscript solution for this!

The best part is that you don’t have to write a single line of VBScript code! It’s already been written for you my Microsoft! The only thing you need to do is be sure the applications have a different pool name from one another—which is always a good idea.At a command prompt you can run iisapp.vbs to get application pool information associated with a specific w3wp.exe process, like so:cscript c:\WINDOWS\system32\iisapp.vbsThis will return output similar to the following:

W3WP.exe PID: 17632   AppPoolId: Application-1
W3WP.exe PID: 17532   AppPoolId: Application-2
W3WP.exe PID: 5748   AppPoolId: Application-2
W3WP.exe PID: 14040   AppPoolId: Application-1

That’s it… easy as pie!


June 20, 2008  3:19 PM

VBScript Statements: Explanation of the Erase statement

Jerry Lees Jerry Lees Profile: Jerry Lees

The VBScript Erase statement is used to erase all the values of an array, but keep the array size and dimensions intact.

 This is useful when you re-use an array and you want to ensure the values inside an array have truely been cleared and no old data is left in the array.

For example, the following would print a blank line:

Redim Bob(10)
Bob(1) = “Rickky Bobby”
Erase Bob
Wscript.echo Bob(1)


June 18, 2008  1:14 PM

VBScript Statements: Explanation of the Exit statement

Jerry Lees Jerry Lees Profile: Jerry Lees

The Exit statement in vbscript allows you to exit a block of code, in the case of a subroutine (Exit Sub) or a function (Exit Function), or the entire script.

This statement is assumed at the end of a script, and therefore is not required, but is typically used in conditional statements to exit a section of code when a particular condition is met.

 Below is an example:

 mysub(1)
 mysub(3)
 mysub(2)
 mysub(0)

sub mysub(testvar)
    testvar=testvar + 1
    if testvar =2 then
        exit sub
    else
        wscript.echo testvar
    endif
end sub


June 14, 2008  10:19 PM

VBScript Statements: Explanation of the Do Loop Statement

Jerry Lees Jerry Lees Profile: Jerry Lees

The Do … Loop statement is very useful (like, for … next and Select … case) to execute a block of code more than once. However, where it differs from the other two is that the Do … Loop statement executes the code as long as (while) a condition is true or until a condition becomes true.

Typical uses of a Do … Loop are in cases where you may not know how many times you need to execute the code section because it could vary from situation to situation, like when reading in a file or taking in keyboard input.

In this example of a Do While Loop, we check to see if a variable is less than 10 and if not continue to run the code in the loop. (Notice how when x = 10 the code does not run and at the end x =10?)

x = 0
Do While x> 10
Wscript.Echo(“X = ” & x)
x= x + 1
Loop
Wscript.Echo(“X is now ” & x)

In this example of a Do Until Loop, we check to see if a variable is greater than 10, but use the until condition to run the loop. (Notice how when X = 10 the code does run and at the end x=11?)

x = 0
Do Until x > 10
Wscript.Wcho(“X = ” & x)
x= x + 1
Loop
Wscript.Echo(“X is now ” & x)


June 13, 2008  2:18 PM

How to find a lost router password for most routers

Jerry Lees Jerry Lees Profile: Jerry Lees

In this installment, I thought I’d take a quick break from VBScript and give you a little networking information I stumbled upon.

Recently, at my current job site we had a situation where the client didn’t know the router password because a series of network administrators had left and the password was simply “lost in the shuffle”. Of course, you all know this is not a good situation to be in if you need to preform network maintenance or want to upgrade a portion of the network somehow.

Of course the only thing we could hope for was that the previous network administrators had NOT been security minded and left the router passwords blank, or that there had not been a console password configured… so started off with trying to figure out what the factory default passwords were for the particular router we were working with at the moment. In our search I found a really awesome resource, that will be invaluable if this ever happens to you here. This site has all the major player’s factory default router passwords in a database, simply select your manufacturer and click find password. Where you are then presented with the known factory default router passwords for specific models from the manufacturer! Awesomeness!

Well, as luck would have it, the last guy didn’t follow standard security practices and this site had the default router password we needed. We were able to get in and get the job done– and change the password when we were done. Plus, after such a scare, the client is sure to not let this happen again. :-)

Now, we got lucky (and so did the client after years of service from the router with default passwords), but what if you need to get into a router and the password has been set to something other than default? Well, this situation is a bit harder– but not impossible. You need to see if you can get onto the console of the router, most major manufacturers have a mechanism for you to plug directly into a router with a serial cable and change settings. Hopefully, this will NOT have a console password– if it does try the defaults, just in case.

Once in, get a dump or output of the configuration. Then you will need to follow the manufacturers method to change the password. Should the console connection not have enough security to change the passwords– you may have to set the router to factory defaults. Be sure if you do this you have the configuration completely and you are prepared to rebuild it. I would not recommend you do this unless you have either done this before or you have 24/7/365 support and have the support folks on the phone while you do it.

Here is my recommendation (and I’ll probably get many security folks commenting differently) , but assuming the router is in a secured place, (a hall closet or the CEO’s office does not count) and locked away so only authorized people can get to it, I almost always leave no password on the console for cases just like this one I’ve mentioned.

Hopefully this helps someone out there and good luck!


June 6, 2008  1:53 PM

A simple way to Write to the registry with VBScript

Jerry Lees Jerry Lees Profile: Jerry Lees

Last time I posted I gave you a function that provided A simple way to read the registry with VBScript, in this posting we’ll use the same Wscript.shell object as last time, just use a different method– the RegWrite Method!

Keep in mind, writing to the registry can be dangerous to the stability of your system. If you do not specifically know you need to write the registry location, and with what value, I recommend not writing to the registry. I have used a made up registry location so running this script should be as safe as I can make it.

This example writes to HKey_Current_User\VBScriptAdmin the key Teststring, which is a REG_SZ (or string) value. You’ll notice that this value likely doesn’t exist before you run the script. The method creates the entire path if it doesn’t exist– which is very nice! (Or not so nice if you typo the path.)

At the end of the script you should see the ReadReg function reads the value back in and writes the value read, which should be Success!.

So, lets look at the code.

Option Explicit

Dim Temp

‘HKEY_CURRENT_USER = HKCU
‘HKEY_LOCAL_MACHINE = HKLM
‘HKEY_CLASSES_ROOT = HKCR
‘HKEY_USERS = HKEY_USERS
‘HKEY_CURRENT_CONFIG = HKEY_CURRENT_CONFIG

Temp = WriteReg(“HKCU\VBSriptAdmin\Teststring”,”Success!”,”REG_SZ”)
Temp = ReadReg(“HKCU\VBSriptAdmin\Teststring”)
WScript.Echo Temp

Function WriteReg(RegPath, Value, RegType)
      ‘Regtype should be “REG_SZ” for string, “REG_DWORD” for a integer,…
      ‘”REG_BINARY” for a binary or boolean, and “REG_EXPAND_SZ” for an expandable string

      Dim objRegistry, Key
      Set objRegistry = CreateObject(“Wscript.shell”)

      Key = objRegistry.RegWrite(RegPath, Value, RegType)
      WriteReg = Key
End Function

Function ReadReg(RegPath)
      Dim objRegistry, Key
      Set objRegistry = CreateObject(“Wscript.shell”)

      Key = objRegistry.RegRead(RegPath)
      ReadReg = Key
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!


June 4, 2008  2:57 PM

A simple way to read the registry with VBScript

Jerry Lees Jerry Lees Profile: Jerry Lees

On occasion when administering a group of systems you need to preform certain tasks depending on if something is installed or a particular setting is a certain value and the only way to determine if a system needs attention is to look in the registry. So, In this installment we will look at reading the registry with the Wscript.shell object.

This is a simple task, as you can see from the function below in the example code. However, it’s something that will save you a ton of time in troubleshooting when you have to first check to ensure a particular system has the setting you need to change or that is creating a problem!

In the example code below, I have attempted to keep it pretty generic in an effort to demonstrate the code on as many systems as possible– so it doesn’t do much because I’ve kept my registry checks limited to components I think should be installed on most all systems. However, it does present the concepts and I’ve added the major registry keys’ abbreviations you would use to reference them as comments in the code.

Additionally, remember that you reference the path all the way to the data you want including the value name, if you are specifying a value. If you reference a registry key, but no value, you will get back the (default) value. Both examples are shown below, I encourage you to break out regedit and take a look at the values I referenced in the code below so you can better see what the code is doing. Also, you can look here to learn more about the regread method of the wscript.shell object.

So, lets get to the code!

Option Explicit
Dim Temp

‘HKEY_CURRENT_USER = HKCU
‘HKEY_LOCAL_MACHINE = HKLM
‘HKEY_CLASSES_ROOT = HKCR
‘HKEY_USERS = HKEY_USERS
‘HKEY_CURRENT_CONFIG = HKEY_CURRENT_CONFIG

Temp = ReadReg(“HKLM\System\CurrentControlSet\Services\lanmanworkstation\DisplayName”)
WScript.Echo Temp
Temp = ReadReg(“HKCU\Control Panel\Desktop\Wallpaper”)
WScript.Echo Temp
Temp = ReadReg(“HKCR\.txt\”)
WScript.Echo Temp
Function ReadReg(RegPath)
     Dim objRegistry, Key
     Set objRegistry = CreateObject(“Wscript.shell”)
     Key = objRegistry.RegRead(RegPath)
     ReadReg = Key
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!


May 26, 2008  3:48 PM

VBScript Statements: Explanation of the Option Explicit Statement

Jerry Lees Jerry Lees Profile: Jerry Lees

In VBScript declaring your variables up front is not required (as stated in this sites explanation of the Dim Statement), however, many programmers choose to declare their variables up front to avoid having to track down misspellings in their code. They go further by adding the Option Explicit statement to force them to do so. With Option Explicit turned on and error is thrown when a variable is used before being declared.

Consider the following code, that doesn’t use Option Explicit:

TheFirstVariable = 1
TheSecondVariable = 2
TheThirdVariable = TehFirstVariable + TheSecondVariable

Wscript.echo TheThirdVariable

The output will be 2, when you were expecting 3! To find the error, if you didn’t notice it, read the third line very slowly TheFirstVariable is spelled differently as TehFirstVariable– therefore creating a new variable with a default value of zero. (or empty if used as a string)


May 24, 2008  3:47 PM

VBScript Statements: Explanation of the Dim Statement

Jerry Lees Jerry Lees Profile: Jerry Lees

The DIM statement is used to declare variables prior to being used. To save space in your code, you can declare more than one variable in a single statement by separating each variable with a comma.

For example:

Dim x
Dim x, y, z

The Dim statement is not required in VBScript because VBScript will automatically create variables at the time you first use them.


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: