Previous Section Table of Contents Next Section

Basic Arithmetic

You use VBScript's mathematical operators for most basic math. VBScript's operators should look pretty familiar and self-explanatory:

  • Addition: +

  • Subtraction: -

  • Multiplication: *

  • Division: /

  • Order of evaluation: ( )

  • Exponentiation: ^ (usually located in the 6 key on your keyboard)

Normally, you just assign the results of such an operation to a variable or a statement like MsgBox, as in the following examples.


myVar = 1 + 2

MsgBox myVar

myVar = myVar + (myVar * .03)

MsgBox myVar

myVar = myVar^2

MsgBox myVar

VBScript evaluates expressions from left to right, performing exponentiation first, then multiplication and division, then addition and subtraction. Anything in parentheses is evaluated first, starting at the innermost level of nested parentheses. To put that in context, consider these similar-looking expressions, which all generate a different result due to the order of evaluation.


myVar = 1 * 2 + 1

MsgBox myVar

myVar = 1 + 1 * 2

MsgBox myVar

myVar = (1 + 1) * 2

MsgBox myVar

myVar = ((1 + (1 * 2))

MsgBox myVar

You might be wondering why all this math stuff should be important to you. After all, you're trying to administer services, not launch space shuttles. You might not use lots of math in your scripts, but you're likely to use some. For example, suppose you want to write a quick function that generates unique passwords for users. You could use the function when creating new user accounts or when resetting user accounts. Listing 7.1 shows what the function might look like.

graphics/arrow.gif Making Up a Password

You can include this function in another script, and then call it. This function takes a user name or another unique string and generates a unique password to go with it. The password generated is based partially upon the current system time, so it'll be different even for the same user if you use it multiple times.

Listing 7.1. MakePW.vbs. This script is intended to be included within another script.

Function MakePW(sUserName)

 Dim vTemp, vPasswd, vLetter, vValue

 For vTemp = 1 To Len(sUserName)

  vLetter = Mid(sUserName, vTemp, 1)

  vValue = Asc(vLetter) * vTemp

  vValue = vValue - DatePart("n",Now)

  vPasswd = vPasswd & CStr(vValue)

 Next

 MakePW = Right(vPasswd, 8)

End Function

The script can be used as-is within your other scripts. You might call it by using a statement like sNewPassword = MakePW("JohnD").

graphics/arrow.gif Making Up a Password-Explained

In Chapter 4, I introduced the concept of functions and subroutines as a means of modularizing your code. I explained them further in Chapter 5. MakePW is a custom function that encapsulates a certain piece of functionality. It's declared with the initial Function statement, and I've followed the function declaration with some variable declarations.


Function MakePW(sUserName)

 Dim vTemp, vPasswd, vLetter, vValue

Next, I use a For…Next loop. It will execute the loop contents once for each letter in the user name.

For more details on For…Next, skip ahead to "Loops" in Chapter 10.


For vTemp = 1 To Len(sUserName)



Next

Within the loop, several functions are used, including some mathematical operations. First, I use the Mid() function to extract the current letter of the user's name. Then, I use the Asc() function to convert that letter to its ASCII value (65 for A, 66 for B, and so forth). I multiply the ASCII value by the value of vTemp, which helps to obfuscate the password-generation scheme.

Next, I get the seconds from the system clock by using the DatePart() function and contract that from the value. This provides a pseudorandomness to the password. Finally, I convert the value to a string and append it to a variable named vPasswd.


vLetter = Mid(sUserName, vTemp, 1)

vValue = Asc(vLetter) * vTemp

vValue = vValue - DatePart("n",Now)

vPasswd = vPasswd & CStr(vValue)

Finally, I assign the rightmost eight characters of vPasswd to the function's name. This returns the rightmost characters to whatever called the function, completing the function's task.


 MakePW = Right(vPasswd, 8)

End Function

This isn't the most amazing password ever created, and it's all numbers with no letters or symbols, but it provides a useful example of how math functions can be used within an administrative script.

    Previous Section Table of Contents Next Section